gitbranch切换分支:基本完整的关于Git分支branch的操作
gitbranch切换分支:基本完整的关于Git分支branch的操作git pull然后查看branch 的版本信息cd app拉取pull 代码到最新客户端操作:下面部分是简单的说明首先clone代码 git clone git@10.211.55.3:/home/git/repos/app.git然后进入代码的目录
Git 使用背景
项目中要用到dev或者其他分支开发完代码,需要将该分支合并到master的需求
操作步骤
下面以dev名称为lex为分支名为例来操作一遍
客户端操作:下面部分是简单的说明
首先clone代码
  git clone git@10.211.55.3:/home/git/repos/app.git
    
然后进入代码的目录
cd app
    
拉取pull 代码到最新
git pull
    
然后查看branch 的版本信息
 git branch -a
    
然后新建branch
  git branch lex
    
然后切换到新的分支lex
git checkout lex
    
核对本地和远程分支情况:
1 远程新建了一个分支,本地没有该分支
git checkout --track origin/lex
    
2 本地新建了一个分支 lex,但是在远程没有
git push --set-upstream origin lex
    
在本地分支修改代码或者增加文件之类的
当前分支所有代码提交
 git add .
    
编写提交备注
 git commit -m "add text.txt" 
    
提交代码至远程分支lex
  git push origin lex
    
切换当前分支至主干(master)
 git checkout master
    
如果多人开发建议执行如下命令 拉取最新的代码
git pull origin master
    
合并(merge)分支代码
  git merge lex
    
merge完成后,查看是否有冲突
git status
    
最后切换回原开发分支
 git checkout lex
    
一个简化操作如下:
check and build new branch for dev:
1 git clone git@10.211.55.3:/home/git/repos/app.git
2 git checkout -b lex
3 git push -u origin lex
    
git checkout -b|-B <new_branch> [<start point>]
           Specifying -b causes a new branch to be created as if git-branch(1) were called and then checked out.
           In this case you can use the --track or --no-track options  which will be passed to git branch. As a
           convenience  --track without -b implies branch creation; see the description of --track below.
git-push - Update remote refs along with associated objects
-u | --set-upstream
    
这部分是操作过程供参考:
➜  Downloads pwd
/Users/lex/Downloads
➜  Downloads mkdir Project
➜  Downloads cd Project
➜  Project git clone git@10.211.55.3:/home/git/repos/app.git
Cloning into 'app'...
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
remote: Counting objects: 14  done.
remote: Compressing objects: 100% (9/9)  done.
remote: Total 14 (delta 3)  reused 0 (delta 0)
Receiving objects: 100% (14/14)  done.
Resolving deltas: 100% (3/3)  done.
➜  Project ls
app
➜  Project cd app
app git:(master) git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
(END)
➜  app git:(master) git branch -v
* master e6b46a6 add php2 file
(END)
➜  app git:(master) git pull
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only
You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase  --no-rebase 
or --ff-only on the command line to override the configured default per
invocation.
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
Already up to date.
新建branch lex
➜  app git:(master) git branch lex
再次查看branch 版本信息
➜  app git:(master) git branch -a
  lex
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
(END)
➜  app git:(master) git branch -v
  lex    e6b46a6 add php2 file
* master e6b46a6 add php2 file
(END)
切换到新的分支 lex
➜  app git:(master) git checkout lex
Switched to branch 'lex'
➜  app git:(lex) git branch -a
* lex
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
(END)
➜  app git:(lex) git branch -v
* lex    e6b46a6 add php2 file
  master e6b46a6 add php2 file
(END)
参考这部分                                          
1 如果远程新建了一个分支,本地没有该分支。
可以利用 git checkout --track origin/branch_name ,这时本地会新建一个分支名叫 branch_name ,会自动跟踪远程的同名分支 branch_name。
git checkout --track origin/branch_name
2 如果本地新建了一个分支 branch_name,但是在远程没有。
这时候 push 和 pull 指令就无法确定该跟踪谁,一般来说我们都会使其跟踪远程同名分支,所以可以利用 git push --set-upstream origin branch_name ,这样就可以自动在远程创建一个 branch_name 分支,然后本地分支会 track 该分支。后面再对该分支使用 push 和 pull 就自动同步。
git push --set-upstream origin branch_name
                                          
我的情况是本地有,远程没有
➜  app git:(lex) git push --set-upstream origin lex
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
Total 0 (delta 0)  reused 0 (delta 0)  pack-reused 0
To 10.211.55.3:/home/git/repos/app.git
 * [new branch]      lex -> lex
Branch 'lex' set up to track remote branch 'lex' from 'origin'.
查看远程和本地所有的分支
➜  app git:(lex) git branch -a
* lex
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/lex
  remotes/origin/master
(END)
可以看到当前使用的分支 *lex
现在我在这个分支下测试:
创建一个text.txt文件并给予当前时间
➜  app git:(lex) date  %F %T >> text.txt
➜  app git:(lex) ✗ cat text.txt
2021-02-21 17:52:19
当前分支所有代码提交
➜  app git:(lex) ✗ git add .
# 编写提交备注
➜  app git:(lex) ✗ git commit -m "add text.txt"
[lex 641fff6] add text.txt
 1 file changed  1 insertion( )
 create mode 100644 text.txt
 # 提交代码至远程分支lex
 ➜  app git:(lex) git push origin lex
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
Enumerating objects: 4  done.
Counting objects: 100% (4/4)  done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2)  done.
Writing objects: 100% (3/3)  286 bytes | 286.00 KiB/s  done.
Total 3 (delta 1)  reused 0 (delta 0)  pack-reused 0
To 10.211.55.3:/home/git/repos/app.git
   e6b46a6..641fff6  lex -> lex
切换当前分支至主干(master)
➜  app git:(lex) git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
# 如果多人开发建议执行如下命令 拉取最新的代码
➜  app git:(master) git pull origin master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only
You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase  --no-rebase 
or --ff-only on the command line to override the configured default per
invocation.
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
From 10.211.55.3:/home/git/repos/app
 * branch            master     -> FETCH_HEAD
Already up to date.
合并(merge)分支代码
➜  app git:(master) git merge lex
Updating e6b46a6..641fff6
Fast-forward
 text.txt | 1  
 1 file changed  1 insertion( )
 create mode 100644 text.txt
 
 # merge完成后可执行如下命令,查看是否有冲突
➜  app git:(master) git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit  working tree clean
提交代码至主干(master)
git push origin master
➜  app git:(master) git push origin master
  ___                 ___
 (o o)               (o o)
(  V  ) ALex CentOS (  V  )
--m-m-----------------m-m--
Total 0 (delta 0)  reused 0 (delta 0)  pack-reused 0
To 10.211.55.3:/home/git/repos/app.git
   e6b46a6..641fff6  master -> master
   
最后切换回原开发分支
➜  app git:(master) git checkout lex
Switched to branch 'lex'
Your branch is up to date with 'origin/lex'
➜  app git:(lex) pwd
/Users/lex/Downloads/Project/app
    
综上。


git branch -a
查看远程和本地所有的分支
git branch dev
本地建立分支dev
git push origin dev:dev
将本地分支dev关联到远程的dev分支
git checkout dev
切换到dev分支下




