我们在平时项目中经常会用到Git进行版本管理。在项目版本迭代的过程中需要通过Git命令进行代码合并,下面提供集合场景来说明下不同场景下如果使用Git命令来合并代码。
git假设现在有两个分支:一个是 deving表示正在开发的分支;一个是 test 表示线上的最新分支 。现在deving正在开发A、B、C三个功能,现在需要将A功能先发一个版本出来。此时可以这样操作:
1)通过git log查看
$ git log
commit 7aac8430d0accd91791e143b5e0f1f5633f99834
Author: wenjing.liu
Date: Thu Feb 14 15:51:43 2019 +0800
add c function
commit 7a6f5c2633d3867bb8a0737046137725ff638920
Author: wenjing.liu
Date: Thu Feb 14 15:51:05 2019 +0800
add b function
commit b9a3b9b1346eb327104ff072d1af6c6e15b8357c
Author: wenjing.liu
Date: Thu Feb 14 15:50:03 2019 +0800
add a function
2)切换到test分支
$ git checkout test
Switched to branch 'test'
Your branch is up-to-date with 'origin/test'.
执行git cherry-pick commit id
$ git cherry-pick b9a3b9b1346eb327104ff072d1af6c6e15b8357c
[test dee8467] add a function
Date: Thu Feb 14 15:50:03 2019 +0800
1 file changed, 171 insertions(+)
create mode 100644 build/4.0/android/AFunction.java
通过git status查看,执行完这个命令之后,代码还是在本地,需要在执行git push将代码提交到服务器即可。
1$ git status
On branch test
Your branch is ahead of 'origin/test' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
$ git push
87fef7d..dee8467 test -> test
在test分支上git log,发现在deving分支上提交的内容已经完完全全的复制到test分支
$ git log
commit dee8467acaa5a3e6dea9a9779a1560b43cc70a20
Author: wenjing.liu
Date: Thu Feb 14 15:50:03 2019 +0800
add a function
需要注意的是,在执行提交的过程中是会出现冲突,出现冲突的时候,解决冲突,在重新提交即可。
$ git cherry-pick 8d8eff14fdb0f71bd6d96332a17cb1b054ad6aa0
error: could not apply 8d8eff1... modify a function at deving
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'
liuwenjingMacBook-Pro:generate-code j1$ git status
On branch test
Your branch is up-to-date with 'origin/test'.
You are currently cherry-picking commit 8d8eff1.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: build/4.0/android/AFunction.java
no changes added to commit (use "git add" and/or "git commit -a")
解决完冲突之后,重新提交即可。
$ git add build/4.0/android/AFunction.java
$ git status
On branch test
Your branch is up-to-date with 'origin/test'.
You are currently cherry-picking commit 8d8eff1.
(all conflicts fixed: run "git cherry-pick --continue")
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Changes to be committed:
modified: build/4.0/android/AFunction.java
$ git commit -m "fix conflict"
$ git push
最后git log查看
$ git log
commit c6f7e1bca48fc8ba3fdad5be1815035dc8fd6f87
Author: wenjing.liu
Date: Thu Feb 14 16:33:39 2019 +0800
fix conflict
commit 3cb6a1a2b97e405400f1a076250d2fd3c5960075
Author: wenjing.liu
Date: Thu Feb 14 16:31:48 2019 +0800
modify a function
commit dee8467acaa5a3e6dea9a9779a1560b43cc70a20
Author: wenjing.liu
Date: Thu Feb 14 15:50:03 2019 +0800
add a function
在deving分支的代码已经完全复制过来,包括提交记录以及提交时间
上一个场景中在切换分支的时候,本地还有一些没有提交到Head的代码,是不能进行切换分支的。而这些代码会导致项目编译不通过或者没有自测,又不能提交到服务器,那么怎么办呢?
这就需要用到git stash 命令了。该命令首先会将代码先本地缓存下,等切换完代码完成其他任务之后,在切换到开发分支上将代码还原即可,同样也可以将这些缓存的代码,还原到任何分支上。
1)git status查看本地有未修改的代码
$ git status
On branch deving
Your branch is up-to-date with 'origin/deving'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: build/4.0/android/AFunction.java
no changes added to commit (use "git add" and/or "git commit -a")
2)执行git stash
$ git stash save afunction
Saved working directory and index state On deving: afunction
HEAD is now at 8d8eff1 modify a function at deving
注意这个afunction就是给这次缓存起了一个别名,方便在后面还原的时候找到该次缓存,执行git status,发现本地已经没有需要修改的代码了。
$ git status
On branch deving
Your branch is up-to-date with 'origin/deving'.
nothing to commit, working directory clean
当执行完其他的操作,或者要提取缓存的代码的时候,执行下面的命令
3)先查看在释放
先git stash list查看有几个缓存,找到这次需要提取的缓存
$ git stash list
stash@{0}: On deving: afunction
可以通过下面两种方式还原:
$ git stash apply stash@{0}
On branch deving
Your branch is up-to-date with 'origin/deving'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: build/4.0/android/AFunction.java
no changes added to commit (use "git add" and/or "git commit -a")
最后面的stash@{0}就是上面的那个list对应的前面的编号,也可以直接通过git stash pop,但是这种方式仅仅只能提取stash@{0}对应的记录,如果这里要提取其他编号下的stash,必须通过git stash apply命令。
将一个分支的代码全部合并到另外一个分支上。
git假设现在有两个分支:一个是 deving表示正在开发的分支;一个是 test 表示线上的最新分支 ,现在要把deving上面的代码全部合并到test分支上。
1)切换到test分支
在切换到test分支的时候,首先要保证deving上面的所有分支都push到服务器
$ git checkout test
Switched to branch 'test'
Your branch is up-to-date with 'origin/test'
2)git merge 需要合并的到该分支的分支名
$ git merge deving
Auto-merging build/4.0/android/AFunction.java
CONFLICT (add/add): Merge conflict in build/4.0/android/AFunction.java
Automatic merge failed; fix conflicts and then commit the result.
有冲突的话,解决完冲突直接提交即可。如果没有冲突直接执行git push就可以了。
上面的三种场景是我在开发过程中遇到的三种情况。总结记录下。