Git Difftool Commands
The following commands will setup P4Merge as the application to show changes.
|
Use git difftool to run the P4Merge merge utility to resolve show changes.
Git Mergetool Commands
The following commands will setup P4Merge as the application to solve merge conflicts.
|
Use git mergetool to run the P4Merge merge utility to resolve merge conflicts. It is typically run after git merge.
虽然看上去不错,但使用时发现有的文件定位不准,但meld很准确,无语。
beyond compare4配置
#!/bin/sh
git config --global diff.tool bc4
git config --global difftool.prompt false
git config --global difftool.bc4.cmd '"/usr/local/bin/bcomp" "$LOCAL" "$REMOTE"'
git config --global merge.tool bc4
git config --global mergetool.prompt false
git config --global mergetool.bc4.cmd '"/usr/local/bin/bcomp" "$LOCAL" "$REMOTE" "$BASE" "$MERGED"'
git config --global mergetool.bc4.trustexitcode true
mac 安装 meld
1. brew install Caskroom/cask/xquartz
2 . brew install pygtk
3. brew cask install caskroom/cask/meld
shell 使用 git mergetool --tool meld (mergetool 时现在不支持文件夹,但是difftool 时是支持的)
文件夹比较
git difftool --tool=meld --dir-diff
文件比较
git difftool --tool=meld
非常好用。
1. git add 的使用:
git add -A 提交所有变化
git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
git add -i 看暂存状态,修改暂存的状态。
s(1) 当前的状态
u (2)是增加
r (3)是取消
d (6)是删除
其它的看意思就可以了。
如果把.DS_Store提交了怎么处理?
git add -i
2: binary nothing scannerios/Module/Personal/.DS_Store
调用删除命令
git rm --cache scannerios/Module/Personal/.DS_Store
以上就把多出来的.DS_Store删除了。
2. 如果想一步操作可以加上 -a 这样就对更改的项,不用先add, 再commit了,如果没增加还是要先add操作的。
git commit -a -m "new change"
3. 比较最近的修改:
git difftool HEAD HEAD^
1.常用操作是: git log --oneline (查看历史版本)再运行:git difftool a4da80a~ a4da80a (具体比较,用比较工具比较直观)这是比较历史版本。
2.如何比较本地和服务器的差异? 用 git difftool -d -g(目录的形式出现比较) 或直接是 git difftool -g (直接出比较结果)。
3.如何比较已在暂存中的不同?
git difftool --cached -g
4. 如果不想要现在的修改,想把没有提交的文件回退一下(这是危险的操作,如果没有暂存是没问题的,如果有就要小心了!)
git checkout -- 文件名字
如:
git checkout -- a.txt // 这样在没有commit 时,这时就还原到了上次的原子了。
5. 全部放弃本地的提交并把目录也删除了:
1. 撤销工作区的修改 (如果暂存区有这个文件,工作区文件被缓冲区文件替换,但如果是reset 工作区的文件不会被替换,但会丢弃暂存中的文件,以暂存区文件为主,如果暂存区没有就以上次的commit中的文件来替换本地文件。所以正确的做法是先看暂存冲有没有文件,如果有先删除暂存区的再运行就恢复到上次的commit版本中的信息了,这个地方有坑。。。)
git checkout --filename ( git checkout . ) (如果单用这和条是有可能有问题的,所以最好小心!!!)
只想恢复修改的文件到上次的状态用:
git reset .
git checkout .
git clean -xdf
2. 撤销缓存区的修改 (commit add 可以加入新加的也可能增加的是修改的文件,要取消就可以有两种情况)
(工作区的文件不会被替换,只会丢弃缓冲中的文件)
git reset filename (是修改过的)
git rm --cached filename
(增加的)执行上面命令后,将修改丢回了工作区
3. 撤销版本库的修改
git log:找到要回退到的版本号(hash id)
git reset hash id
如果用git reset --hard 会改变工作目录与历史恢复出来的是一样的。
如果用 git revert hash id 的话,会回退版本,并且当前的内容要自己来合并,好像是hash前一个版本的位置与reset 时不太一样的少一个版本的位置,不合并。
例: a.txt
1 2 3 4
对应了四次提交,第一次是1 每二次加2 第三次加3 第四次加4
17:17 $ git lb
* 2a2039b - (HEAD -> master) 4 (14 minutes ago) <zimin.yzm>
* 40eb366 - 3 (15 minutes ago) <zimin.yzm>
* c4b2e78 - 2 (15 minutes ago) <zimin.yzm>
* a576005 - 1 (16 minutes ago) <zimin.yzm>
- 运行:git reset --hard 40eb366
17:31 $ cat a.txt
1
2
3
还原到原来的状态。
- git revert 40eb366
- 看一结果:
17:26 $ cat a.txt
1
2
<<<<<<< HEAD
3
4
=======
>>>>>>> parent of 40eb366... 3
看一下结果,是不太一样的,位置不太一样并且没有合并代码。
4. 强力清除本地所有文件及修改:(暂存清空,本地清空,获取上次的版本到本地,删除多了的文件)
git stash && git stash drop
git clean -xdf
6. 在git 里边索索(-p 显示函数 -n 显示行号 --heading 格式更好认识)
git grep -p -n --break --heading yangzm
结果:
n.txt
6=public int a add(int a, int b){
7: print("yangzm");
yangzm:git yangziminyangzimin$
7. 中断一次合并:
git status -sb
##master
UU hello.rb
git merge --abort
git status -sb
##master
8. 如果合并出错,想再来一次
git reset --hard HEAD
这里会清目录中的所有的内容。
9. rebase 出错后的处理
先git reflog 查看历史所有日志
从上到下,越往后,head数字越大的,就是越早的操作,找到你rebase之前的commit操作
然后执行git reset --hard HEAD@{8},就回到你rebase错误之前的commit地方
然后重新rebase分支,对于已经push到远程个人分支的代码同样适用
10. 如何在shell中显示git提示符:
3 4 5 6 | cd ~ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt --depth=1 # add to ~/.bash_profile GIT_PROMPT_ONLY_IN_REPO=1 source ~/.bash-git-prompt/gitprompt.sh |
.bash_profile 里边加的东东:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
source ~/.git-completion.bash
GIT_PROMPT_ONLY_IN_REPO=1
source ~/.bash-git-prompt/gitprompt.sh
Mac版git命令自动补全:
1.
brew install bash-completion
2.
brew info bash-completion
里边显示的就是我上边的内容。
Add the following line to your ~/.bash_profile:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
3. 这时是下载 git-completion.bash 文件方到~ 目录下边,并在.bash_profile 里写加载的用处。
从github上clone git的源码到本地:(clone整个项目可能会比较慢,可以直接去这里复制文件内容)
git clone https://github.com/git/git.git
找到”contrib/completion/”目录下的git-completion.bash,将该文件拷贝到~/目录下下并重命名为.git-completion.bash:
cp git-completion.bash ~/.git-completion.bash
在~/.bash_profile 文件中追加如下内容:
source ~/.git-completion.bash
重启终端,大功告成,现在git能够使用tab键自动补全命令了,enjoy it!
11 常用快捷配置: ~/.gitconfig
[alias]
com = commit
br = branch
st = status
ck = checkout
ll = log --pretty=oneline
lb = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
12. git rebase 大致介绍
本文git rebase的应用, git rebase的主要作用是可以修改远程的提交历史。
在介绍git rebase的使用之前,我们先看一下git rebase 的风险。 在你git rebase的分支上,不允许有其他操作。也就是说你在git rebase的时候一定要保证这个分支只会被你操作。
主要功能如下:
- 合并远程的多个提交到一个提交中(这个最常用)
- 删除远程某次提交
- 修改远程某次提交的 comment
- 修改远程最近一次提交的内容 (使用 git commit --amend)
其实git rebase最常用的是合并远程几个commit到一个commit中,和修改远程的comment等。想必每个新手都有被老大拉过去指着,git上乱七八糟的commit狂骂的经历吧。
想必每个人都有为了修复一个bug重复提交七八次,每一次可能只是为了加一行日志吧。看着远程各种commit,我们一定会开始挠头了,怎么才能修改已经提交的commit呢。下面我们就开始使用git rebase 开始神奇之旅。
具体命令如下所示:
git rebase -i commitID [--onto branch]
git rebase --continue | --skip | --abort | --quit | --show-current-patch
下面我们就开始具体的实际操作了。
实验一、合并 最近的几次提交到一个提交中
我们假设git的远程分支上有如下4次提交
61b035c tmp commit 4
990255b tmp commit 3
b4f96c5 tmp commit 2
3e45203 tmp commit 1
我们目的是要把commit 1, commit 2, commit 3, commit 4 合并到一个commit中去。
看了之前 git reset章节的兄弟们,可能会说了,我们可以直接使用 git reset --soft 到 commit 1之前的那个提交,然后修改以后再提交不就可以了。
其实是可以的,如果你只是想合并最近的几次提交的话,可以使用git reset。
首先git rebase 开启 rebase
#注意 git rebase 是 左开右闭的,所以要合并 commit 1 到 4,那么要选择commit 1之前的提交或者 3e45203~1
git rebase -i 3e45203~1
上面命令执行以后会显示如下信息, 会罗列出来四个commit。 同时有如下几种命令可以选择
pick 3e45203 tmp commit 1
pick b4f96c5 tmp commit 2
pick 990255b tmp commit 3
pick 61b035c tmp commit 4
# Rebase eb02cda..990255b onto eb02cda (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
其中前四行使我们的origin branch的要进行 rebase 的四次提交。 下面的被注释掉是git rebase的一些命令,我们详细剖析这些命令。
- pick/p 需要提交的commit
- reword/r 修改comment
- squash/s 合并到前一个提交中
- drop/d 丢掉某条commit
其他的不常用就不列出来了。
继续操作
我们这次实验是为了合并4次提交,所以我们保留commit 1 然后其他的都换成 s如下所示:
pick 3e45203 tmp commit 1
s b4f96c5 tmp commit 2
s 990255b tmp commit 3
s 61b035c tmp commit 4
然后 保存退出vim模式, 会跳到如下的vim界面。这个界面是让我们设置最终合并commit的 comment。
# This is a combination of 3 commits.
# This is the 1st commit message:
tmp commit 1
# This is the commit message #2:
tmp commit 2
# This is the commit message #3:
tmp commit 3
# This is the commit message #4:
tmp commit 4
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Nov 19 16:50:54 2018 +0800
#
# interactive rebase in progress; onto eb02cda
# Last commands done (3 commands done):
# squash b4f96c5 tmp commit 2
# squash 990255b tmp commit 3
# No commands remaining.
# You are currently rebasing.
#
# Changes to be committed:
# new file: other-project/dubbo-app/src/main/java/customRpc/reflect/test
我们可以注释掉之前的comment,然后加上自己新的comment。新的comment如下所示。
# This is a combination of 3 commits.
# This is the 1st commit message:
#tmp commit 1
# This is the commit message #2:
#tmp commit 2
# This is the commit message #3:
#tmp commit 3
# This is the commit message #4:
#tmp commit 4
合并以后新的提交comment
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Nov 19 16:50:54 2018 +0800
#
# interactive rebase in progress; onto eb02cda
# Last commands done (3 commands done):
# squash b4f96c5 tmp commit 2
# squash 990255b tmp commit 3
# No commands remaining.
# You are currently rebasing.
#
# Changes to be committed:
# new file: other-project/dubbo-app/src/main/java/customRpc/reflect/test
#
然后我们保存退出vim以后执行如下命令
git push -f
然后就会发现 远程分支上没有 commit 1 , 2, 3, 4只有 一个提交 "合并以后新的提交comment"
实验二、如何使用 git rebase 合并任意连续的提交
上面讲解了如何合并最近的几次提交,下面我们继续讲解,如何合并远程的任意的几个连续的commit。
我们先模拟如下场景。
如下所示的 git log
16edbc8 (HEAD -> master, origin/master, origin/HEAD) tmp commit 4
4ed15d3 tmp commit 3
7947d54 tmp commit 2
0345dfe tmp commit 1
我们本次目的是合并 commit 3 和commit 2
首先
git rebase -i 0345dfe~1
然后我们把 commit3 的 pick改为 s。这样 commit3 会跟commit2合并到一个commit上去。
pick 0345dfe tmp commit 1
pick 7947d54 tmp commit 2
s 4ed15d3 tmp commit 3
pick 16edbc8 tmp commit 4
然后退出vim,会跳转如下界面。这个界面是让我们设置,commit3和commit2合并以后的 comment信息的。
#tmp commit 2
# This is the commit message #2:
#tmp commit 3
commit2 和commit3 合并以后的提交
然后我们执行如下命令
git push -f
最终我们看一下效果怎么样。
git log --oneline
f57268d (HEAD -> master, origin/master, origin/HEAD) tmp commit 4
5eb4329 commit2 和commit3 合并以后的提交
0345dfe tmp commit 1
ok到此,已经成功了。
实验三、 修改 任意一次提交的comment
我们想修改远程任意一次commit的comment的话,可以如下操作。
首先假设我们有如下三个commit在远程分支上。
git log --oneline
f57268d (HEAD -> master, origin/master, origin/HEAD) tmp commit 4
5eb4329 commit2 和commit3 合并以后的提交
0345dfe tmp commit 1
我们把 5eb4329 的comment 修改为 "commit 2,3"
首先
git rebase 0345dfe~1
然后会自动进入如下的vim界面,我们 把第二行的pick改为 r
pick 0345dfe tmp commit 1
pick 5eb4329 commit2 和commit3 合并以后的提交
pick f57268d tmp commit 4
改为如下所示。
pick 0345dfe tmp commit 1
r 5eb4329 commit2 和commit3 合并以后的提交
pick f57268d tmp commit 4
然后自动跳转到如下界面
commit2 和commit3 合并以后的提交
我们修改comment信息为我们想要的如下所示。
commit 2,3
然后
git push -f
最终我们看一下效果。
30c78d7 (HEAD -> master, origin/master, origin/HEAD) tmp commit 4
fac397f commit2,3
0345dfe tmp commit 1
实验四、 删除远程的任意一个提交
下面讲解如何删除远程的某一次提交。
还是老样子我们先模拟如下场景。
假设我们有如下四个commit。
8eda049 (HEAD -> master, origin/master, origin/HEAD) tmp commit 4
dbe4329 tmp commit 3
20899d5 tmp commit 2
cfab502 tmp commit 1
具体每个提交修改的内容如下:
- cfab502 在test文件中增加 "提交1" 这一行
- 20899d5 在test文件中增加 "提交2" 这一行
- dbe4329 在test文件中增加 "提交3" 这一行
- 8eda049 在test文件中增加 "提交4" 这一行
我们想 删除 提交2这次commit, 也就是删除20899d5这个commit。也就是test文件中不要 "提交2"这一行。
执行如下命令
git rebase -i cfab502~1
然后会自动跳转到如下的vim界面
pick cfab502 tmp commit 1
pick 20899d5 tmp commit 2
pick dbe4329 tmp commit 3
pick 8eda049 tmp commit 4
然后我们把第二行的pick改为d
pick cfab502 tmp commit 1
d 20899d5 tmp commit 2
pick dbe4329 tmp commit 3
pick 8eda049 tmp commit 4
然后会自动跳转到如下报错界面,这个时候不要慌。 认真看清楚报错内容,大概意思是:
兄弟不好意思,您要删除commit2这个提交,但是这样会跟commit3产生冲突,因为 commit3的提交中有要用到commit2的内容(因为commit2中 有一行 "提交2"这一行会在 commit3和commit4中出现)
Auto-merging other-project/dubbo-app/src/main/java/customRpc/test
CONFLICT (content): Merge conflict in other-project/dubbo-app/src/main/java/customRpc/test
error: could not apply dbe4329... tmp commit 3
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply dbe4329... tmp commit 3
知道了原因,我们就心里面有数了,因为我们要删除commit2这个提交,而 commit2这个提交干的事情是在test文件中增加了 "提交2"这一行内容。 因此我们要把这一行在commit3和commit4中都要删除。这样才能解决掉冲突。
直到了原因以后,我们就直到怎么解决bug了。就是把 "提交2"这一行删除掉就好。如下所示
git status
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: other-project/dubbo-app/src/main/java/customRpc/test
no changes added to commit (use "git add" and/or "git commit -a")
我们看一下具体的冲突
cat other-project/dubbo-app/src/main/java/customRpc/test
<<<<<<< HEAD
提交1
=======
提交1
提交2
提交3
>>>>>>> dbe4329... tmp commit 3
解释上面的冲突:
应为我们有四个提交, 其中 commit1内容是
提交1
commit3 的内容是
提交1
提交2
提交3
因为我们删除了commit2, 所以在合并的时候出现上面的冲突。 根据需要我们要删除 "提交2"这一行。所以解决冲突以后的test文件如下所示。
解决冲突以后的test文件如下所示
提交1
提交3
然后继续如下操作
git add .
git commit -m "tmp commit 3(after delete commit 2)"
git rebase --continue
不出意外,会继续冲突,因为 我们删掉了 test文件中"提交2"这一行,肯定会跟 commit4冲突。
error: could not apply 8eda049... tmp commit 4
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 8eda049... tmp commit 4
我们同样的方法解决如下冲突
cat other-project/dubbo-app/src/main/java/customRpc/test
提交1
<<<<<<< HEAD
提交3
=======
提交2
提交3
提交4
>>>>>>> 8eda049... tmp commit 4
删除 "提交2"这一行,改成如下所示
提交1
提交3
提交4
然后继续执行如下命令。
git add .
git commit -m "tmp commit 4(after delete commit 2)"
git rebase --continue
git push -f
然后我们看一下效果
git log --oneline
7e48ef7 (HEAD -> master, origin/master, origin/HEAD) tmp commit 4(after delete commit 2)
6efaff4 tmp commit 3(after delete commit 2)
cfab502 tmp commit 1
然后查看删除commit2以后的最终的test文件如下是所示, 我们成功的删除了commit2中的 "提交2"这一行数据了。
提交1
提交3
提交4
13. git如何删除远程的错误提交,如何回复误删的远程某次提交
远程分支
commit3: add test3.c
commit2: add test2.c
commit1: add test1.c
本地分支
commit4: add test4.c
#如何删除远程某次错误提交,如何删除本地提交本地缓存中的某次错误提交?
将 local repository的 commit4 撤销到 staging(也就是保留修改文件)
git reset --soft commit3
将 local repository的 commit4 撤销到 working directory(也就是删除 commit4)
git reset --hard commit3
将remote repository 的commit3撤销到 staging(也就是保留修改文件)
git reset --soft commit2
git push -f origin develop(master)
将remote repository 的commit3 working directory(也就是删除 commit3)
git reset --hard commit2
git push -f origin develop(master)
如何恢复不小心删除的远程或者本地的某次提交?
git reset --hard commit3 后悔,恢复方法(最好在30天以内完成)
git reflog
#找到如下 db79fb8 HEAD@{1}: commit: commit3
git reset --hard db79fb8
强制删除远程commit2后悔,恢复方法(最好在30天以内完成)
git reflog
#找到如下 db79fb8 HEAD@{1}: commit: commit3
git reset --hard db79fb8
git push
14. git commit --amend 修改最后一次提交
本文讲解如何使用git commit --amend 的两个作用。
应该经常会有提交一个commit以后,觉得漏什么东西,然后不得不再次提交,其实两个提交应该是一个提交才对。这样我们可以使使用git commit --amend来操作。
- 修改最近一次提交的comment
- 修改最近一次提交的commit的内容
实验一 修改最近一次提交的comment
我们当然可以使用 git reset 或者 git rebase来操作,不过都没有使用git commit --amend方便。
模拟如下场景
git log --oneline
30cda92 (HEAD -> master, origin/master, origin/HEAD) tmp commit 4 (amend)
4a2286c tmp commit 3
cfab502 tmp commit 1
我们要修改 30cda92 的comment 为 "amend 后的comment", 只要执行下面命令就可以简单实现。
git commit --amend -m "amend 后的comment"
git push -f
然后看一下效果
git log --oneline
81d8cdb (HEAD -> master, origin/master, origin/HEAD) amend 后的comment
4a2286c tmp commit 3
cfab502 tmp commit 1
实验二 我们要修改 最近一次提交的内容
我们接着上面的场景, 比如我们最后一次提交遗漏了一个文件的提交。那么我们可以进行如下操作
git add .
git commit --amend -m "amend 后的comment(add new file)"
git push -f
就可以了
15 改写提交
在cherry-pick,您可以从其他分支复制指定的提交,然后导入到现在的分支。
主要使用的场合:
- 把弄错分支的提交移动到正确的地方
- 把其他分支的提交添加到现在的分支
16 文件删除
git clean -n -d -x 询问删除(-n 是用来演练用的看看什么可以删除)
git clean -d -x -f (-f 强制删除操作)来真实删除删除
git clean -x -i 询问式删除
17 忽略文件夹
1. 在想要增加到暂存区的文件夹里边新建 .gitignore (windows里不要带.txt).
2. 在里边如果不想增加 .vs目录下边的所有文件,则写入: .vs/ 就可以了。
18 查看远程提交
git 查看远程仓库的log
原创白鲸入海 最后发布于2018-08-27 17:03:56 阅读数 11197 收藏
展开
git查看本地仓库的log很简单 :git log 就行
查看 远程仓库也很简单:git log 远程仓库名
查看所有分支:git branch -a
songchong@srv-pad-compile5:~/3505/ATS350B$ git branch -a
ble
* ble_use_name_attach_ble
remotes/origin/gl5118b_dev_v1.9
remotes/origin/gl5118b_dev_v1.9_patch
remotes/origin/tb_gl5118b_dev_v1.9_ble
remotes/origin/tb_gl5118b_dev_v1.9_for_duoshi
remotes/origin/tb_gl6151_dev
remotes/origin/tb_minisdk_for_lexin
remotes/origin/upstream_master
remotes/origin/upstream_v1.9
远程分支为:remotes/origin/tb_gl5118b_dev_v1.9_ble
查看远程分支log: git log remotes/origin/tb_gl5118b_dev_v1.9_ble
19 revert 测试 一次revert 多个
macbook@airdeMacBook-Air test % ls
a.txt b.txt c.txt d.txt
macbook@airdeMacBook-Air test % git reflog
024dba0 (HEAD -> master) HEAD@{0}: commit: 4
dcf67fa HEAD@{1}: commit: 3
cd2101d HEAD@{2}: commit: 2
3b82a1c HEAD@{3}: commit (initial): 1
macbook@airdeMacBook-Air test % git revert -n HEAD@{0} HEAD@{2}
macbook@airdeMacBook-Air test % ls
a.txt c.txt
先git commit -m"生效"
git rebase -i HEAD@{2} (这里 把要合的写成s :wq 退出)
结果:
macbook@airdeMacBook-Air test % git log
commit cd2101de25adba5c9925ce0ce657f058c6cbbd54 (HEAD)
Author: zimin.yzm <zimin.yzm@alibaba-inc.com>
Date: Thu Mar 26 01:56:08 2020 +0800
2
commit 3b82a1c807a2fdfda573670c81607626e1ee2b1d
Author: zimin.yzm <zimin.yzm@alibaba-inc.com>
Date: Thu Mar 26 01:55:51 2020 +0800
1
这个就是我想要的。
20 git错误merge后回滚
说明:
- 原文链接: 使用GIT不小心merge后的回滚操作 - 不骑乌骓 - 博客园
- 说明:虽然有四个方法,但我们项目中使用方法一和二比较合适,不会产生新的commit,并且简单粗暴
方法一,新分支覆盖
①首先两步保证当前工作区是干净的,并且和远程分支代码一致方法一,删除远程分支再提交
$ git co currentBranch
$ git pull origin currentBranch
$ git co ./
②备份当前分支(如有必要)
$ git branch currentBranchBackUp
③恢复到指定的commit hash
$ git reset --hard resetVersionHash //将当前branch的HEAD指针指向commit hash
④删除当前分支的远程分支
$ git push origin :currentBranch
$ //或者这么写git push origin --delete currentBranch
⑤把当前分支提交到远程
$ git push origin currentBranch
方法二,强制push远程分支(多人开发时不能用,其他人代码出错)
①首先两步保证当前工作区是干净的,并且和远程分支代码一致
②备份当前分支(如有必要)
③恢复到指定的commit hash
$ git reset --hard resetVersionHash
④把当前分支强制提交到远程
$ git push -f origin currentBranch
方法三,从回滚位置生成新的commit hash
①首先两步保证当前工作区是干净的,并且和远程分支代码一致
②备份当前分支(如有必要)
③使用git revert恢复到指定的commit hash,当前分支恢复到a>3版本(见下图)
a)此方法会产生一条多余的commit hash&log,其实1c0ce98和01592eb内容上是一致的
b)git revert是以要回滚的commit hash(1c0ce98)为基础,新生成一个commit hash(01592eb)
$ git revert resetVersionHash
④提交远程分支
$ git push origin currentBranch