git branch/tag切换与合并

  几个关于分支应用的命令:

git branch <分支名称> -- 创建分支
git tag -- 给某一次固定的提交做标记
git checkout <分支名称> -- 分支之间进行切换
git stash -- 切换分支之前保存本地修改
git merge -- 合并分支

  简单应用:

当提交的次数比较多的时候,如果想查找历史的提交:

git log --oneline --graph --decorate --all
==================================
$ git log --oneline --decorate --graph --all
* 244afc0 (HEAD -> master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

新建分支,在分支上修改,再查看历史的提交记录:

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git branch test

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git checkout test
A       d
Switched to branch 'test'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* 244afc0 (HEAD -> test, master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git rm a
rm 'a'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git add .

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git commit -m "delete file a"
[test c9b3fab] delete file a
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a => d (100%)

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* c9b3fab (HEAD -> test) delete file a
* 244afc0 (master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

如果相对当前的commit打个tag,不用hash就是默认当前commit,用历史的hash(5-7位足够,git会自动补全)就是对历史某次commit进行tag:

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* c9b3fab (HEAD -> test) delete file a
* 244afc0 (master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git checkout master
Switched to branch 'master'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag "v0" 8612d29

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git log --oneline --decorate --graph --all
* c9b3fab (test) delete file a
* 244afc0 (HEAD -> master) create c
* 8612d29 (tag: v0) create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

该种tag是轻量级的,也可以用annotated tag,方便共享,用git tag可以方便查看所有的tag

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag -a "INITIAL_ANNOTATED_COMMIT" 8612d29

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag
INITIAL_ANNOTATED_COMMIT
v0

git checkout tag名称 可以切换到那个tag,但是会处于“detached HEAD”状态,commit之后的历史记录会被清除(暂时的理解是,这种单个commit上的提交跟分支根本不是一个维度),所以需要这么做时,常见的做法是加参数 -b来创建并切换到新的分支

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git checkout v0
Note: checking out 'v0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 8612d29... create a and b

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo ((INITIAL_ANNOTATED_COMMIT))
$ git checkout v0 -b new_fixed_tag
Switched to a new branch 'new_fixed_tag'

 当做过一些具体的操作之后,我们会发现,在切换分支之前,必须要把可以提交的代码都提交了,但如果我们希望保持修改之前的状态是不是就只能放弃本次修改呢?其实,我们可以用git stash 将本次修改存放起来,工作区与暂存区都与原先的状态一致,可以切换到新的分支,当我们返回这个分支时,又可以根据保存的名称将之前修改的内容复原出来。此部分略过。

 另外,merge以及它的放弃也略过。

  

原文地址:https://www.cnblogs.com/bruceChan0018/p/5804372.html