如何修复提交到错误的 Git 分支?

发布于 2024-09-03 09:14:33 字数 65 浏览 7 评论 0原文

我刚刚对错误的分支做出了完美的承诺。 如何撤消主分支中的最后一次提交,然后进行相同的更改并将它们放入我的升级分支中?

I just made a perfectly good commit to the wrong branch.
How do I undo the last commit in my master branch and then take those same changes and get them into my upgrade branch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

幻想少年梦 2024-09-10 09:14:33

如果您尚未推送更改,您还可以进行软重置:

git reset --soft HEAD^

这将恢复提交,但将提交的更改放回到索引中。假设分支彼此相对最新,git 将允许您签出另一个分支,然后您可以简单地提交:

git checkout branch
git commit -c ORIG_HEAD

-c ORIG_HEAD 部分很有用再次输入提交消息。

If you haven't yet pushed your changes, you can also do a soft reset:

git reset --soft HEAD^

This will revert the commit, but put the committed changes back into your index. Assuming the branches are relatively up-to-date with regard to each other, git will let you do a checkout into the other branch, whereupon you can simply commit:

git checkout branch
git commit -c ORIG_HEAD

The -c ORIG_HEAD part is useful to not type commit message again.

指尖微凉心微凉 2024-09-10 09:14:33

这个话题晚了四年,但这可能对某人有帮助。

如果您忘记在提交之前创建一个新分支并在 master 上提交所有内容,无论您做了多少次提交,以下方法都会更容易:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

现在您的 master 分支等于 origin/master 和 all新提交位于 my_feature 上。请注意,my_feature 是本地分支,而不是远程分支。

4 years late on the topic, but this might be helpful to someone.

If you forgot to create a new branch before committing and committed all on master, no matter how many commits you did, the following approach is easier:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

Now you have your master branch equals to origin/master and all new commits are on my_feature. Note that my_feature is a local branch, not a remote one.

掩饰不了的爱 2024-09-10 09:14:33

如果您有一个干净的(未修改的)工作副本

要回滚一个提交(请确保记下下一步的提交哈希值):

git reset --hard HEAD^

将该提交拉入另一个分支:

git checkout other-branch
git cherry-pick COMMIT-HASH

如果您已修改或未跟踪的更改

另请注意 < code>git reset --hard 将杀死您可能拥有的任何未跟踪和修改的更改,因此,如果您有那些您可能更喜欢的更改:

git reset HEAD^
git checkout .

If you have a clean (un-modified) working copy

To rollback one commit (make sure you note the commit's hash for the next step):

git reset --hard HEAD^

To pull that commit into a different branch:

git checkout other-branch
git cherry-pick COMMIT-HASH

If you have modified or untracked changes

Also note that git reset --hard will kill any untracked and modified changes you might have, so if you have those you might prefer:

git reset HEAD^
git checkout .
我还不会笑 2024-09-10 09:14:33

如果您已经推送了更改,则需要在重置 HEAD 后强制执行下一次推送。

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

警告:硬重置将撤消工作副本中任何未提交的修改,而强制推送将用本地分支的当前状态完全覆盖远程分支的状态。

以防万一,在 Windows 上(使用 Windows 命令行,而不是 Bash)它实际上是四个 ^^^^ 而不是一个,所以它是

git reset --hard HEAD^^^^

If you already pushed your changes, you will need to force your next push after resetting the HEAD.

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

Warning: a hard reset will undo any uncommitted modifications in your working copy, while a force push will completely overwrite the state of the remote branch with the current state of the local branch.

Just in case, on Windows (using the Windows command line, not Bash) it's actually four ^^^^ instead of one, so it's

git reset --hard HEAD^^^^
牵强ㄟ 2024-09-10 09:14:33

我最近做了同样的事情,我不小心将更改提交给了 master,而我本应该提交给其他分支。但我没有推动任何东西。

如果您只是提交了错误的分支,并且此后没有更改任何内容,也没有推送到存储库,那么您可以执行以下操作:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

注意:在上面的示例中,我使用 git reset HEAD~1 倒回 1 次提交。但如果你想倒回 n 次提交,那么你可以执行 git reset HEAD~n。

另外,如果您最终提交了错误的分支,并且在意识到提交了错误的分支之前还编写了更多代码,那么您可以使用 git stash 来保存正在进行的工作:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

注意:我使用了这个网站作为参考
https: //www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/

I recently did the same thing, where I accidentally committed a change to master, when I should have committed to other-branch. But I didn't push anything.

If you just committed to the wrong branch, and have not changed anything since, and have not pushed to the repo, then you can do the following:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes. 
git reset HEAD~1 

// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash

// create other-branch (if the other branch doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// take the temporary commit you created, and apply all of those changes to the new branch. 
//This also deletes the temporary commit from the stack of temp commits.
git stash pop

// add the changes you want with git add...

// re-commit your changes onto other-branch
git commit -m "some message..."

NOTE: in the above example, I was rewinding 1 commit with git reset HEAD~1. But if you wanted to rewind n commits, then you can do git reset HEAD~n.

Also, if you ended up committing to the wrong branch, and also ended up write some more code before realizing that you committed to the wrong branch, then you could use git stash to save your in-progress work:

// save the not-ready-to-commit work you're in the middle of
git stash 

// rewind n commits
git reset HEAD~n 

// stash the committed changes as a single temp commit onto the stack. 
git stash 

// create other-branch (if it doesn't already exist)
git branch other-branch

// checkout the other branch you should have committed to.
git checkout other-branch

// apply all the committed changes to the new branch
git stash pop

// add the changes you want with git add...

// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."

// pop the changes you were in the middle of and continue coding
git stash pop

NOTE: I used this website as a reference
https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/

太傻旳人生 2024-09-10 09:14:33

对于错误分支上的多次提交

如果对您来说只有大约 1 次提交,那么还有很多其他更容易的重置解决方案可用。对我来说,我有大约 10 个提交是我不小心在 master 分支上创建的,而不是(我们称之为 target),而且我不想丢失提交历史记录。

在这种情况下,使用此答案作为参考,您可以应用以下 4 步流程 -

  1. 创建一个新的临时分支temp from master
  2. temp 合并到最初用于提交的分支,即 target
  3. 撤消 master 上的提交
  4. 删除临时分支temp

以下是上述步骤的详细信息:

  1. master创建一个新分支(我不小心在其中提交了很多更改)

    git checkout -b temp
    

    注意:-b 标志用于创建新分支
    为了验证我们是否正确,我会快速执行 git 分支 以确保我们位于 temp 分支和 git 日志 > 检查我们的提交是否正确。

  2. 将临时分支合并到最初用于提交的分支,即target
    首先,切换到原始分支,即 target (如果没有,您可能需要 git fetch

    git 签出目标
    

    注意:不使用 -b 标志
    现在,让我们将临时分支合并到我们当前签出的分支中 target

    git 合并温度
    

    如果有的话,您可能需要处理一些冲突。成功合并后,您可以推送(我会)或继续下一步。

  3. 使用此答案作为参考撤消master上的意外提交,首先切换到大师

    git checkout master
    

    然后使用下面的命令将其全部撤消以匹配远程(或者如果需要,可以使用适当的命令来匹配特定的提交)

    git reset --hard origin/master
    

    同样,我会在前后执行 git log 只是为了确保预期的更改生效。

  4. 擦除证据,即删除临时分支。为此,首先,您需要检查 temp 合并到的分支,即 target (如果您留在 master 上并执行下面的命令,您可能会收到错误:分支“temp”未完全合并),所以让我们

    git 签出目标
    

    然后删除这次事故的证据

    git 分支 -d temp
    

For multiple commits on the wrong branch

If, for you, it is just about 1 commit, then there are plenty of other easier resetting solutions available. For me, I had about 10 commits that I'd accidentally created on master branch instead of, let's call it target, and I did not want to lose the commit history.

In this case, using this answer as a reference, you can apply the following 4-step process -

  1. Create a new temporary branch temp from master
  2. Merge temp into the branch originally intended for commits, i.e. target
  3. Undo commits on master
  4. Delete the temporary branch temp.

Here are the above steps in detail:

  1. Create a new branch from the master (where I had accidentally committed a lot of changes)

    git checkout -b temp
    

    Note: -b flag is used to create a new branch
    Just to verify if we got this right, I'd do a quick git branch to make sure we are on the temp branch and a git log to check if we got the commits right.

  2. Merge the temporary branch into the branch originally intended for the commits, i.e. target.
    First, switch to the original branch i.e. target (You might need to git fetch if you haven't)

    git checkout target
    

    Note: Not using -b flag
    Now, let's merge the temporary branch into the branch we have currently checkout out target

    git merge temp
    

    You might have to take care of some conflicts here, if there are. You can push (I would) or move on to the next steps, after successfully merging.

  3. Undo the accidental commits on master using this answer as a reference, first switch to the master

    git checkout master
    

    then undo it all the way back to match the remote using the command below (or to a particular commit, using the appropriate command, if you want)

    git reset --hard origin/master
    

    Again, I'd do a git log before and after just to make sure that the intended changes took effect.

  4. Erase the evidence, i.e. delete the temporary branch. For this, first, you need to checkout the branch that the temp was merged into, i.e. target (If you stay on master and execute the command below, you might get an error: The branch 'temp' is not fully merged), so let's

    git checkout target
    

    and then delete the proof of this mishap

    git branch -d temp
    
微凉 2024-09-10 09:14:33

因此,如果您的情况是您已提交给 master 但打算提交给 another-branch (该分支可能已经存在,也可能不存在),但您还没有推送然而,这很容易解决。

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

现在,您对 master 的所有提交都将位于 another-branch 上。

源自:http://haacked.com/archive/2015/06 /29/git-migrate/

So if your scenario is that you've committed to master but meant to commit to another-branch (which may or not may not already exist) but you haven't pushed yet, this is pretty easy to fix.

// if your branch doesn't exist, then add the -b argument 
git checkout -b another-branch
git branch --force master origin/master

Now all your commits to master will be on another-branch.

Sourced with love from: http://haacked.com/archive/2015/06/29/git-migrate/

不回头走下去 2024-09-10 09:14:33

详细说明这个答案,以防您有多个提交需要移动,例如develop新分支

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started

To elaborate on this answer, in case you have multiple commits to move from, e.g. develop to new_branch:

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started
甜`诱少女 2024-09-10 09:14:33

如果您遇到此问题并且您有 Visual Studio,则可以执行以下操作:

右键单击您的分支并选择查看历史记录

在此处输入图像描述

右键单击​​“提交”想要回去。并根据需要恢复或重置。

输入图片此处描述

If you run into this issue and you have Visual Studio, you can do the following:

Right-click on your branch and select View History:

enter image description here

Right-click on commit you want to go back to. And Revert or Reset as needed.

enter image description here

生死何惧 2024-09-10 09:14:33

在常见情况下,您忘记在提交之前从 master 切换到 feature 分支:

git checkout -B feature
git branch -f master origin/master

origin/master 替换为您想要的提交 < code>master 指向的分支。例如,如果您希望它指向 HEAD 之后的 3 个提交,请使用 HEAD~3,如果您希望它指向提交,请使用 a1b2c3d用那个哈希。

这个想法是在当前提交上重新创建 feature 分支并切换到它。然后使 master 分支指向与 origin/master 相同的提交。

一般情况

在一般情况下,您希望在 feature 分支上重播在 master 上完成的提交,如下图所示:

A---B---C---D  $old_master                   A---B---C---D  master
    |        \                                   |        \
    |         G---H---I  master <- HEAD  =>      |         G---H---I
    |                                            | 
    `-E---F  feature                             `-E---F---G'--H'--I' feature <- HEAD

然后挑选您在 feature 上所做的提交使用以下命令在您的 feature 分支上使用 code>master 。将 $old_master 替换为进行更改之前 master 所指向的提交的哈希值。

git checkout feature
git cherry-pick $old_master..master
git branch -f master $old_master

如果需要,请使用 git stash --include-untracked 存储本地更改,然后使用 git stash pop 取消存储它们。

无需重写历史记录

除了挑选 feature< 上的更改之外,您还需要 git 恢复 更改,而不是将 master 分支重置为过去。 /代码> 分支。

git checkout feature
git cherry-pick $old_master..master
git checkout master
git revert $old_master..
git checkout feature

确保不要将您的提交git mergefeature中。如果您尝试将 feature 分支合并回 master,这些更改将被忽略,因为我们刚刚恢复了它们。

结果如下所示,其中 rGrHrIG的恢复提交>HI 分别:

A---B---C---D             rG--rH--rI  master
    |        \           /
    |         G---H---I-`
    | 
    `-E---F---G'--H'--I' feature <- HEAD

In the common case where you forgot to switch from master to your feature branch before commiting:

git checkout -B feature
git branch -f master origin/master

Replace origin/master with the commit you want your master branch to point to. For example, use HEAD~3 if you want it to point 3 commits behind HEAD, or a1b2c3d if you want it to point to the commit with that hash.

The idea is to recreate the feature branch on the current commit and switch to it. Then make the master branch point to the same commit as origin/master.

General case

In the general case where you want to replay the commits done on master on your feature branch, like the following diagram:

A---B---C---D  $old_master                   A---B---C---D  master
    |        \                                   |        \
    |         G---H---I  master <- HEAD  =>      |         G---H---I
    |                                            | 
    `-E---F  feature                             `-E---F---G'--H'--I' feature <- HEAD

Then cherry-pick the commits you made on master on your feature branch using the following commands. Replace $old_master by the hash of the commit master was pointing to before you made your changes.

git checkout feature
git cherry-pick $old_master..master
git branch -f master $old_master

If needed, stash your local changes using git stash --include-untracked, and then unstash them later using git stash pop.

Without rewriting history

Instead of resetting the master branch into the past, you need to git revert the changes, in addition to cherry-picking the changes on the feature branch.

git checkout feature
git cherry-pick $old_master..master
git checkout master
git revert $old_master..
git checkout feature

Make sure not to git merge your commits into feature. Those changes will be ignored if you try to merge your feature branch back into master, because we just reverted them.

Here's what the result would look like, with rG, rH and rI the revert commits of G, H and I respectively:

A---B---C---D             rG--rH--rI  master
    |        \           /
    |         G---H---I-`
    | 
    `-E---F---G'--H'--I' feature <- HEAD
一紙繁鸢 2024-09-10 09:14:33

保留更改并取消提交
简单的解决方案是运行此命令。

git reset HEAD^

这将取消提交代码,同时保留您的更改

Keep changes and Uncommit
The simple solution is to run this command.

git reset HEAD^

This will uncommit the code, while keeping your changes

送舟行 2024-09-10 09:14:33

对我来说,这是通过恢复我推送的提交,然后挑选该提交到另一个分支来解决的。

git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH

您可以使用 git log 来查找正确的哈希值,并且您可以随时推送这些更改!

For me, this was solved by reverting the commit I had pushed, then cherry-picking that commit to the other branch.

git checkout branch_that_had_the_commit_originally
git revert COMMIT-HASH
git checkout branch_that_was_supposed_to_have_the_commit
git cherry pick COMMIT-HASH

You can use git log to find the correct hash, and you can push these changes whenever you like!

似梦非梦 2024-09-10 09:14:33

如果您想要应用更改的分支已经存在(例如分支 develop),请按照 fotanus 下面,然后:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

显然,如果您愿意,您可以使用 tempbranch 或任何其他分支名称来代替 my_feature

另外,如果适用,请延迟存储弹出(应用),直到合并到目标分支后。

If the branch you wanted to apply your changes to already exists (branch develop, for example), follow the instructions that were provided by fotanus below, then:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

And obviously you could use tempbranch or any other branch name instead of my_feature if you wanted.

Also, if applicable, delay the stash pop (apply) until after you've merged at your target branch.

-黛色若梦 2024-09-10 09:14:33

为此,我对错误的分支进行了大约 6 次提交,并在注意到之前将它们推送到了远程。我按照以下步骤操作,他们提供了帮助:

  1. 我做了一个 git log 来查看我所做的提交:

    <前><代码> git log --oneline

  2. 从以前的提交和推送列表中,我选择了我需要做头部的提交,即列表中带有“head”标题的第一个提交因为它代表了您所做的新提交的完整列表。保存 ID。
    ![我的代码示例](这里我演示了提交列表和我选择的提交列表。最上面的一个因为它是我最后一次提交的头部)

  3. 然后我将此分支重置回使用前的 6 次提交;

     git reset --hard HEAD~6
    
  4. 我签出到新分支,我打算放置我错误提交的提交;

     git checkout -b newbranch
    
  5. 使用

     git reset --hard < commit_id > 
    

将整个提交列表带到您的新分支。

  1. 然后使用

    <前><代码> git push --force

对您的远程存储库进行所有这些更改

For this, I had made about 6 commits to the wrong branch and pushed them to the remote before noticing. I followed these steps and they helped:

  1. I did a git log to view the commits I had made with:

     git log --oneline
    
  2. From the list of previous commits and push, I chose the commit that I needed to make the head ie the first commit with the "head" title in the list as it represents the whole list of new commits you made. Save the id.
    ![example of my code](here I demonstrate the list of commits and the one I chose. The top one as it was the head of my last commits)

  3. Then I reset this branch back to the 6 commits before using;

     git reset --hard HEAD~6
    
  4. I checkout to the new branch I intend to put the commits I had mistakenly commited;

     git checkout -b newbranch
    
  5. Use

     git reset --hard < commit_id > 
    

to bring the entire list of commits to your new branch.

  1. Then use

     git push --force 
    

to make all these changes to your remote repo as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文