版本控制指南
# learn git branch
练习网址: https://learngitbranching.js.org/?locale=zh_CN
# 主要
# 基础篇
# 1. git commit
Git 仓库中的提交记录保存的是你的目录下所有文件的快照,就像是把整个目录复制,然后再粘贴一样,但比复制粘贴优雅许多!
Git 希望提交记录尽可能地轻量,因此在你每次进行提交时,它并不会盲目地复制整个目录。条件允许的情况下,它会将当前版本与仓库中的上一个版本进行对比,并把所有的差异打包到一起作为一个提交记录。
Git 还保存了提交的历史记录。这也是为什么大多数提交记录的上面都有父节点的原因 —— 我们会在图示中用箭头来表示这种关系。对于项目组的成员来说,维护提交历史对大家都有好处。
关于提交记录太深入的东西咱们就不再继续探讨了,现在你可以把提交记录看作是项目的快照。提交记录非常轻量,可以快速地在这些提交记录之间切换!
# 2. git branch
Git 的分支也非常轻量。它们只是简单地指向某个提交纪录 —— 仅此而已。所以许多 Git 爱好者传颂:
早建分支!多用分支!1这是因为即使创建再多的分支也不会造成储存或内存上的开销,并且按逻辑分解工作到不同的分支要比维护那些特别臃肿的分支简单多了。
在将分支和提交记录结合起来后,我们会看到两者如何协作。现在只要记住使用分支其实就相当于在说:“我想基于这个提交以及它所有的父提交进行新的工作。”





# 3. git merge





git branch bugFix
git checkout bugFix
git commit
git checout master
git commit
git merge bugFix
1
2
3
4
5
6
7
2
3
4
5
6
7
# 4. git rebase






git checkout -b bugFix
git commit
git checkout master
git commit
git checkout bugFix
git rebase master
1
2
3
4
5
6
7
2
3
4
5
6
7
# 4. git stash (暂存)
git stash save "message"
git stash apply stash@{0}
git stash pop
git stash list
1
2
3
4
2
3
4
# 高级篇
# 1. 分离 HEAD







git checkout c4
1
2
2
# 2. 相对引用 (^)








git checout bugFix^
1
# 3.相对引用2(~)








git checkout HEAD~1
git branch -f master c6
git branch -f bugFix HEAD^
1
2
3
4
2
3
4
# 4.撤销变更







git reset HEAD~1 // 或者git reset HEAD^
git checkout pushed
git revert HEAD
1
2
3
4
2
3
4
# 移动提交记录
# 1.Git Cherry-pick






git cherry-pick c3
git cherry-pick c4
git cherry-pick c7
1
2
3
4
2
3
4
# 2. 交互式 rebase








git rebase -i HEAD~4
1
2
2

# 杂项
# 1. 只取一个提交记录




git rebase -i HEAD~3 // 不使用c3、c2
git branch -f master bugFix
## 或者
git checkout master
git cherry-pick bugFix // 或者git cherry-pick c4
1
2
3
4
5
6
7
2
3
4
5
6
7
# 2.提交的技巧 #1 -



git rebase -i HEAD~2 #修改C2和C3的顺序
git commit --amend
git rebase -i HEAD~2 #修改C3'和C2''顺序
git branch -f master
1
2
3
4
5
2
3
4
5
# 3.提交的技巧 #2 -





git checkout master
git cherry-pick newImage
git commit --amend
git cherry-pick caption
1
2
3
4
5
2
3
4
5
# 4.Git Tag






git tag v0 c1
git tag v1 c2
git checkout c2
1
2
3
4
2
3
4
# 5.Git Describe






git commit
1
2
2
# 高级话题
# 1. 多次 Rebase


git rebase master bugFix
git rebase bugFix side
git rebase side another
git branch -f master another
1
2
3
4
5
2
3
4
5
# 2. 两个父节点











git branch bugWork HEAD~^2~
1
2
2
# 3. 纠缠不清的分支


git checkout one
git cherry-pick c4 c3 c2
git checkout two
git cherry-pick c5 c4 c3 c2
git branch -f three c2
1
2
3
4
5
6
2
3
4
5
6

# 远程
# Push & Pull —— Git 远程仓库!
# 1. git clone






git clone
1
2
2
# 2. 远程分支






git commit
git checkout o/master
git commit
1
2
3
4
2
3
4
# 3. git fetch







git fetch
1
2
2
# 4. git pull







git pull
1
2
2
# 5. 模拟团队合作







git clone
git fakeTeamwork 2
git commit
git pull
1
2
3
4
5
2
3
4
5
# 6.git push





git commit
git commit
git push
1
2
3
4
2
3
4
# 7. 偏离的提交历史

















git clone
git fakeTeamwork 1
git commit
git pull --rebase
git push
1
2
3
4
5
6
2
3
4
5
6
# 8.锁定的Master(Locked Master)




git reset --hard o/master
git checkout -b feature C2
git push origin feature
1
2
3
4
2
3
4
# 关于 origin 和它的周边 —— Git 远程仓库高级操作
# 1. 推送主分支





git fetch
git rebase o/master side1
git rebase side1 side2
git rebase side2 side3
git rebase side3 master
git push
1
2
3
4
5
6
7
2
3
4
5
6
7
# 2. 合并远程仓库




git checkout master
git pull origin master
git merge side1
git merge side2
git merge side3
git push origin master
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3. 远程追踪












git checkout -b side o/master
git commit
git pull --rebase
git push
## 或者
git branch -f side o/master
git checkout side
git commit
git pull --rebase
git push
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 4. git push 的参数








git push origin master
git push origin foo
1
2
3
2
3
# 5. git push 的参数2








git push origin foo:master
git push origin master^:foo
1
2
3
2
3
# 6.git fetch 的参数














git fetch origin master^:foo
git fetch origin foo:master
git checkout foo
git merge master
1
2
3
4
5
2
3
4
5
# 7.没有 source 的 source







git pull origin :bar
git push origin :foo
1
2
3
2
3
# 8. git pull 的参数








git pull origin bar:foo
git pull origin master:side
1
2
3
2
3

答案:http://element-ui.cn/article/show-70520.aspx
编辑 (opens new window)
上次更新: 2024/06/15, 15:12:25