如何隐藏我以前的提交? [英] How to stash my previous commit?

查看:74
本文介绍了如何隐藏我以前的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的git log上存在以下情况:

commit 111  <-- need to push it to the repository

commit 222  <-- need to stash this one

...

如您所见,我只需要将最后一次(没有先前的)提交推送到存储库中.

As you can see, I need to push only last (without previous) commit to repository.

我该怎么办? git revert --soft commit_hash对我有帮助吗?

How can I do it? git revert --soft commit_hash will help me?

推荐答案

如果您尚未将任何提交推送到远程存储库,则可以使用交互式变基对提交进行重新排序"并存储(新的)最新提交仅更改.

If you've not pushed either commit to your remote repository, you could use interactive rebasing to 'reorder' your commits and stash the (new) most recent commit's changes only.

假设您已将当前分支的尖端(在示例中提交111)检出,请执行以下操作:

Assuming you have the tip of your current branch (commit 111 in your example) checked out, execute the following:

git rebase -i HEAD~2

这将打开您的默认编辑器,列出最近的2次提交并为您提供一些说明.请谨慎对待此处的操作,因为这将有效地重写"存储库的历史记录,并且如果您不小心的话,可能会丢失工作(如有必要,请首先对整个存储库进行备份).例如,我估计下面的提交哈希/标题

This will open your default editor, listing most recent 2 commits and provide you with some instructions. Be very cautious as to what you do here, as you are going to effectively 'rewrite' the history of your repository, and can potentially lose work if you aren't careful (make a backup of the whole repository first if necessary). I've estimated commit hashes/titles below for example

pick 222 commit to be stashed
pick 111 commit to be pushed to remote

# Rebase 111..222 onto 333
#
# 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
#
# 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

重新排序两个提交(它们被列为最旧=>最新),如下所示:

Reorder the two commits (they are listed oldest => newest) like this:

pick 111 commit to be pushed to remote
pick 222 commit to be stashed

保存并退出,这时git将进行一些处理以重写您已更改的两个提交.假设没有问题,您应该颠倒两个变更集的顺序.可以通过git log --oneline -5确认,它将输出最新的优先顺序.

Save and exit, at which point git will do some processing to rewrite the two commits you have changed. Assuming no issues, you should have reversed the order of your two changesets. This can be confirmed with git log --oneline -5 which will output newest-first.

这时,您可以简单地对最近的提交进行软重置,并保存您的工作更改:

At this point, you can simply do a soft-reset on the most recent commit, and stash your working changes:

git reset --soft HEAD~1
git stash

值得一提的是,只有当您之前没有将这些更改中的任何一项推送到远程设备时,此选项才真正可行,否则可能会对使用存储库的每个人造成问题.

It's important to mention that this option is only really viable if you have not previously pushed any of these changes to your remote, otherwise it can cause issues for everyone using the repository.

这篇关于如何隐藏我以前的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆