在Git中恢复由SHA哈希提交? [重复]

本文翻译自:Revert to a commit by a SHA hash in Git? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I'm not clear on how git revert works. 我不清楚git revert是如何工作的。 For example, I want to revert to a commit six commits behind the head, reverting all the changes in the intermediary commits in between. 例如,我想在头部后面恢复提交六次提交,恢复中间提交之间的所有更改。

Say its SHA hash is 56e05fced214c44a37759efa2dfc25a65d8ae98d . 说它的SHA哈希是56e05fced214c44a37759efa2dfc25a65d8ae98d Then why can't I just do something like: 那我为什么不能做以下事情:

git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d

#1楼

参考:https://stackoom.com/question/7wzT/在Git中恢复由SHA哈希提交-重复


#2楼

This might work: 这可能有效:

git checkout 56e05f
echo ref: refs/heads/master > .git/HEAD
git commit

#3楼

The best way to rollback to a specific commit is: 回滚到特定提交的最佳方法是:

git reset --hard <commit-id>

Then: 然后:

git push <reponame> -f

#4楼

Should be as simple as: 应该如此简单:

git reset --hard 56e05f

That'll get you back to that specific point in time. 那会让你回到那个特定的时间点。


#5楼

Updated: 更新:

This answer is simpler than my answer: https://stackoverflow.com/a/21718540/541862 这个答案比我的答案简单: https//stackoverflow.com/a/21718540/541862

Original answer: 原始答案:

# Create a backup of master branch
git branch backup_master

# Point master to '56e05fce' and
# make working directory the same with '56e05fce'
git reset --hard 56e05fce

# Point master back to 'backup_master' and
# leave working directory the same with '56e05fce'.
git reset --soft backup_master

# Now working directory is the same '56e05fce' and
# master points to the original revision. Then we create a commit.
git commit -a -m "Revert to 56e05fce"

# Delete unused branch
git branch -d backup_master

The two commands git reset --hard and git reset --soft are magic here. 两个命令git reset --hardgit reset --soft在这里很神奇。 The first one changes the working directory, but it also changes head (the current branch) too. 第一个更改工作目录,但它也会更改head(当前分支)。 We fix the head by the second one. 我们通过第二个修复头部。


#6楼

It reverts the said commit, that is, adds the commit opposite to it. 它恢复了所述提交,即添加与其相反的提交。 If you want to checkout an earlier revision, you do: 如果要签出早期版本,请执行以下操作:

git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值