常用的 git 命令

更新 : 2019-03-02 

写一个常用流程比较清楚 :  

项目开始 :

git clone …

git add file

git commit -m “whatever”

git push

git rebase -i –root 

第一个改成 reword 其余的改成 fixup

然后输入总 message, 比如 live 

git push origin HEAD –force

 

之后开始维护和加功能 : 

git checkout -b feature

git add feature.txt

git commit -m “s”

git push –set-upstream origin feature  (第一次 push 要 set upstream)

现在假设我们想用 2 个 folder 来维护 2 个版本 

git checkout master 回到 master 

git clone -b feature url..

假设 master 有新东西, 我们去 master local folder 修改然后 push 

在去开 feature local folder, 

我不是很确定是不是一定要跑这 2 个

checkout master

git pull 

然后是

git rebase -i master

git push origin HEAD –force

做好了以后去 master forlder 

git checkout feature 

git pull 

git checkout master

git merge feature

git rebase -i xxx (从 feature 开始 rebase) 

git push origin HEAD –force

结束. 

维护 2 个 local folder 最重要的是要知道, 你在 feature 改东西, 回到 master forlder 是不知道的, 必须先 checkout 然后 pull 才可以做 merge 之类的. 

分开 folder 不是必要的, 你可以简单通过 checkout 来维护 2 个版本. 我只是比较喜欢分开而已. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

refer : 

https://github.com/geeeeeeeeek/git-recipes
https://www.clock.co.uk/insight/deleting-a-git-commit
https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git
https://segmentfault.com/a/1190000009658888
https://git-scm.com/download/gui/windows

 

1. git clone <url> 

把项目克隆到本地

 

2. git clone <url> -b <BranchName>

克隆某个分支

 

3.git log

查看历史 commit, q 表示退出

 

4.git show, git status

查看当前的情况

 

5.git branch <BranchName>

创建新的 branch 

 

6.git checkout <BranchName>

切换到某个 branch, 我们可以随时穿梭在多个 branch 之间开发. 本地文档会立即转换. checkout 前应该确保当前 branch 的修改都 commit 了.

 

7. git checkout -b <BranchName>

切换 branch 假如当前没有这个 branch 就创建一个.

 

8. git branch -d <BranchName>

删除 branch, -D 表示强制删除. 

 

9.git reset 

git reset –hard <hash>~2

git reset –soft head~3

git reset –mixed <hash>

reset 是用于删除历史计入的. 凡事重写 history 都要小心. 

如果会影响到其它人请通知大家做好 commit, 然后你才重写, 重写后叫大家 re-clone. 这样就安全了. 当然如果整个 branch 就你一个人, 那你想怎样都行.

要解释 hard, soft, mixed , 我们先定义 working -> state -> commit, 这个是我们开发的流程. 

–hard 表示回退到某次 commit, 把往后所有的 working, state, commit 都 clear 干干净净. 

使用场景 : 发现做错了, 很想重头来过的时候. 

–mixed 表示回退到某次 commit, 之后的 commit | state 都变成 working.

使用场景 : 比如我们修改了很多次代码, commit 了很多次. 后来我们发现,其实这么多次也就只是为了 fix 一个 bug. 

那么我们可以使用 reset –mixed. 回到我们第一次的 commit, 然后往后的 commit 都变成了 working. 这时我们再 commit 一次就把之前那么多次的事儿一起 commit 了. 

–soft 表示回退到某次 commit, 之后的所有 commit 都变成 state 

使用场景 : 不清楚.

<hash>~<number> <hash> 就是某次 commit 的 hash, ~<number> 是表示前面第几个. 

比如 :

git reset –hard xxxxx~2 意思是退回到 xxxx commit 前的 2 个 commit 

git reset head~2 意思是退回到 head (通常就是最新那个) commit 前的 2 个 commit. 也就是删除前 2 次的 commit.

 

10. git checkout <hash> 

回到那个情况 (只是为了查看, 不应该修改哦) git checkout <BranchName> (回到最新的)

 

11. git rebase 

这个也是可以修改历史计入, 通常用于我们开发完成后,想要 merge to master 前,我们希望这个 branch 的历史计入美美, 这样 merge to master, master 才干净.

git rebase -i 

修改全部历史 commit

这时 git 会开启编辑器, 我们会看到从开始到现在的所有 commit, 按照它的指示做, 我们可以合并 commit, 修改 commit message, 删除 commit 等等. 

关闭编辑器代表完成, 建中我们可以通过 git rebase –abort 取消这次的 rebase.

git rebase -i head~<number> 
git rebase -i <hash>~<number>

我们不一定要 rebase 所有的 commit, 也是可以通过 <hash>~<number> 去定位. 只处理后一部分的 commit.

注 : git rebase 和 reset 都不应该发生代码冲突 (conflict) . 因为 reset 的目的是移除, rebase 的目的是整理. 

 

12. git push origin HEAD –force

强制同步到云端. 通常是因为我们修改了历史计入才需要强制.

 

13. git merge <BranchName>

git checkout master -> git merge feature-branch 通常开发好新功能就是这样的命令. 意思是把 feature-branch 合并到 master 里面.

 

14. git rebase -i <BranchName>

这个 rebase 和刚才的不同. 这个是当上游的分支有新的 commit 时,我们下游的分支需要做的 rebase.

比如 : 有 master 和 feature 

feature 已经开发了 2 个 commit 了. 这时 master 有 bug 做了一个 fix bug 的 commit. 

那现在 feature 也需要 fix 掉这个 bug. 如果我们使用 merge master to feature 的话, 我们的 history 看上去就不太好看. 

我们更希望时间可以倒退, 早点发现这个 bug, 在还没有开始 feature branch 之前 bug 就 fix 好了. 

要把历史计入修改成这样,我们需要 

git checkout feature -> git rebase -i master 

如果有 error 就试试 git rebase -i origin/master

这时有开启编辑器, 我们可以直接关掉, 如果遇到代码冲突 (这个是很有可能会发生的), 我们可以通过 vs code 查看对比. 

修改好文件后, 我们通过 git add <FileName> 把修改好的 file add 进去, 然后 git rebase –continue 继续执行. 

git 会一个个检查, 直到所有的冲突都没有. 就算完成了. 

这个 rebase 的过程中,我们有能力去修改 commit message 或者做合并, 但是我不建议大家这样做, 因为我们的目前是 merge master to feature,并不是要整理 feature 的 hsitory. 

 

15. git revert <hash>

这个和 reset 有点像,区别在于它是通过新开一个 commit 去执行这个倒退. 

这样的好处是没有修改到之前的历史计入. 

也是一样可以使用 head~<number> 

这个回滚通常会有代码冲突. 

比如 commit 1 -> commit 2 -> coomit 3 -> revert to commit 1 

这时就好像我硬生生的 commit 1 场景下的代码直接覆盖进来, 肯定是有冲突的嘛. 

然后我们就修改解决冲突. 然后 git add <FileName> -> git commit 

如果建中遇到麻烦, 也可以 git revert –abort 取消操作. 

 

16. git branch 

查看目前在哪一个 branch 中.

 

17. git rebase -i –root

git reset 无法去掉第 1 个 commit, 可以用这个替代.

 

注意 : 上面有好几个命令都用到 head <hash>~<number> 这样的定位法, 定位之后是否会使用当前的 commit 或者是前一个 commit, 大家自己去测试.

比如 git reset –hard xxx 那请问 xxx 这个 commit 是保留还是保留它的前一个 ? 我不常用, 没记着.

 

    原文作者:兴杰
    原文地址: https://www.cnblogs.com/keatkeat/p/9716497.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞