
从零开始使用git
第三篇:git撤销操作、分支操作和常见冲突
第三篇:从零开始使用git第三篇:git撤销操作、分支操作和常见冲突
1.撤销操作
官方文档:Git 基础 - 撤消操作
任何时候,你都有可能需要撤销刚才所做的操作。接下来,我们会介绍一些基本的撤销操作相关的命令。
请注意,有些撤销操作是不可逆的,所以请务必谨慎小心。
(1)修改最后一次提交
有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了,想要撤销刚才的提交操作,可以使用--amend选项重新提交
git commit --amend
(2)取消已经暂存的文件
举个例子,有两个修改过的文件,我们想要分开提交,但不小心用git add .全加到了暂存区域。该如何撤销暂存其中的一个文件呢?
git status的命令的输出已经告诉了我们该怎么做:
git add .
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: README.txt
modified: benchmarks.rb
取消暂存benchmarks.rb文件:
git reset HEAD benchmarks.rb
Unstaged changes after reset:
M benchmarks.rb
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: README.txt Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: benchmarks.rb
这条命令看起来有些古怪,先别管,能用就行。现在benchmark.rb文件又回到了之前已经修改未暂存的状态
(3)取消对文件的修改
如果觉得刚才对benchmark.rb的修改完全没有必要,该如何取消修改,回到修改之前的版本呢?
git status同样提示了具体的撤销方法
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: benchmarks.rb
抛弃文件修改的命令:
git checkout -- benchmarks.rb
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) modified: README.txt
这条命令有些危险,所有对文件的修改都没有了。
记住,任何已经提交到Git的都可以被恢复。即便在已经删除的分支中的提交。或者用--amend重新改写的提交都可以被恢复。
2.分支操作
(1)在Git中,HEAD是一个指向你正在工作中的本地分支的指针(将HEAD想象为当前分支的别名)
(2)Git的实现与项目复杂度无关,它永远可以在几毫秒的时间内完成分支的创建和切换。
摘自Git官方文档:Git 分支
(留坑)
3.常见冲突及解决
(留坑...)
更多参考git官方文档:https://git-scm.com/book/zh/v1/