【发布时间】:2011-11-08 22:36:06
【问题描述】:
我创建了一个新文件(包含大量代码和辛勤工作)并将其提交到我的 git 存储库的主分支。片刻之后,我意识到我可能应该为该文件创建一个分支。我以为我可以撤销 master 的提交并在分支上再次提交,但我认为我做事的顺序很愚蠢。出于某种原因,我先创建了分支,然后以 is it possible to revoke commits? 为指导撤销了提交。
这基本上是我所做的:
echo "Lots of code and hard work" > newfile.txt
git commit -m "thoughtless commit" newfile.txt
# Oh wait, I should've branched!
git branch thoughtful
git checkout thoughtful
# Hmm, I didn't mean for that last commit to be in master
git checkout master
# get the previous commit's hash
oldhead=$(git log | awk 'BEGIN{commitcount=0; } /^commit/{ if (commitcount++ == 1) { print $2 }; }')
git reset --hard "$oldhead"
git checkout thoughtful
所以现在我显然废弃了 master 上的提交(事实上,master 上不再存在“newfile.txt”),但它仍然存在于 >周到(我猜是reflog)。
有人能解释一下我在这里做了什么吗,为什么不需要提交 thoughtful?
【问题讨论】:
-
为了将来参考,您的
reset命令可以缩写为git reset --hard HEAD^,而不需要oldhead。