git 拉取后将已提交(但未推送)的更改移动到新分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5066041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:09:24  来源:igfitidea点击:

moving committed (but not pushed) changes to a new branch after pull

git

提问by Dave Sherohman

I've done a fair bit of work ("Your branch is ahead of 'origin/master' by 37 commits.") which really should have gone into its own branch rather than into master. These commits only exist on my local machine and have not been pushed to origin, but the situation is complicated somewhat in that other devs have been pushing to origin/masterand I've pulled those changes.

我做了很多工作(“你的分支在 37 个提交之前领先于 'origin/master'。”)这些工作确实应该进入它自己的分支而不是master. 这些提交只存在于我的本地机器上,并没有被推送到origin,但情况有些复杂,因为其他开发人员一直在推送,origin/master而我已经取消了这些更改。

How do I retroactively move my 37 local commits onto a new branch? Based on the docs, it appears that git rebase --onto my-new-branch masteror ...origin/mastershould do this, but both just give me the error "fatal: Needed a single revision". man git-rebasesays nothing about providing a revision to rebaseand its examples do not do so, so I have no idea how to resolve this error.

如何将我的 37 个本地提交追溯移动到新分支?根据文档,似乎git rebase --onto my-new-branch master...origin/master应该这样做,但两者都给我错误“致命:需要一次修订”。 man git-rebase没有说提供修订版,rebase并且它的示例没有这样做,所以我不知道如何解决这个错误。

(Note that this is nota duplicate of Move existing, uncommited work to a new branch in Gitor How to merge my local uncommitted changes into another Git branch?as those questions deal with uncommitted changes in the local working tree, not changes which have been committed locally.)

(请注意,这不是将现有的未提交的工作移动到 Git 中的新分支如何将我的本地未提交的更改合并到另一个 Git 分支的副本因为这些问题涉及本地工作树中未提交的更改,而不是已提交的更改已在本地提交。)

回答by Mark Longair

This should be fine, since you haven't pushed your commits anywhere else yet, and you're free to rewrite the history of your branch after origin/master. First I would run a git fetch originto make sure that origin/masteris up to date. Assuming that you're currently on master, you should be able to do:

这应该没问题,因为您还没有将提交推送到其他任何地方,并且您可以在origin/master. 首先,我会运行 agit fetch origin以确保它origin/master是最新的。假设您目前在master,您应该能够执行以下操作:

git rebase origin/master

... which will replay all of your commits that aren't in origin/masteronto origin/master. The default action of rebase is to ignore merge commits (e.g. those that your git pulls probably introduced) and it'll just try to apply the patch introduced by each of your commits onto origin/master. (You may have to resolve some conflicts along the way.) Then you can create your new branch based on the result:

...这将重播所有提交不在的origin/masterorigin/master。rebase 的默认操作是忽略合并提交(例如,您的git pulls 可能引入的那些),它只会尝试将您的每个提交引入的补丁应用到origin/master. (在此过程中,您可能需要解决一些冲突。)然后您可以根据结果创建新分支:

git branch new-work

... and then reset your masterback to origin/master:

...然后将您的master背部重置为origin/master

# Use with care - make sure "git status" is clean and you're still on master:
git reset --hard origin/master

When doing this kind of manipulating branches with git branch, git reset, etc. I find it useful to frequently look at the commit graph with gitk --allor a similar tool, just to check that I understand where all the different refs are pointing.

在使用git branch,git reset等进行这种操作分支时,我发现经常使用gitk --all或类似工具查看提交图很有用,只是为了检查我是否了解所有不同的 ref 指向的位置。

Alternatively, you could have just created a topic branch based on where your master is at in the first place (git branch new-work-including-merges) and then reset masteras above. However, since your topic branch will include merges from origin/masterand you've not pushed your changes yet, I'd suggest doing a rebase so that the history is tidier. (Also, when you eventually merge your topic branch back to master, the changes will be more obvious.)

或者,您可以根据您的 master 所在的位置 ( git branch new-work-including-merges)创建一个主题分支,然后master按上述方式重置。但是,由于您的主题分支将包含来自的合并origin/master并且您尚未推送您的更改,因此我建议您进行 rebase,以便历史记录更整洁。(此外,当您最终将主题分支合并回 master 时,更改将更加明显。)

回答by Stachu

If you have a low # of commits and you don't care if these are combined into one mega-commit, this works well and isn't as scary as doing git rebase:

如果您的提交数量很少,并且您不在乎这些是否合并为一个大型提交,那么这很有效,并且不像这样做那样可怕git rebase

unstage the files (replace 1 with # of commits)

取消暂存文件(用提交数量替换 1)

git reset --soft HEAD~1

create a new branch

创建一个新分支

git checkout -b NewBranchName

add the changes

添加更改

git add -A

make a commit

提交

git commit -m "Whatever"

回答by NiRmaL

I stuck with the same issue. I have found easiest solution which I like to share.

我坚持同样的问题。我找到了我喜欢分享的最简单的解决方案。

1) Create new branch with your changes.

1)使用您的更改创建新分支。

git checkout -b mybranch

2) (Optional) Push new branch code on remote server.

2)(可选)在远程服务器上推送新的分支代码。

git push origin mybranch

3) Checkout back to master branch.

3)结帐回到主分支。

git checkout master

4) Reset master branch code with remote server and remove local commit.

4) 使用远程服务器重置 master 分支代码并删除本地提交。

git reset --hard origin/master

回答by Andriy

One more way assume branch1 - is branch with committed changes branch2 - is desirable branch

另一种方法假设 branch1 - 是提交更改的分支 branch2 - 是理想的分支

git fetch && git checkout branch1
git log

select commit ids that you need to move

选择您需要移动的提交 ID

git fetch && git checkout branch2
git cherry-pick commit_id_first..commit_id_last
git push

Now revert unpushed commits from initial branch

现在从初始分支恢复未推送的提交

git fetch && git checkout branch1
git reset --soft HEAD~1

回答by Andreas B

Alternatively, right after you commit to the wrong branch, perform these steps:

或者,在您提交到错误的分支后,立即执行以下步骤:

  1. git log
  2. git diff {previous to last commit} {latest commit} > your_changes.patch
  3. git reset --hard origin/{your current branch}
  4. git checkout -b {new branch}
  5. git apply your_changes.patch
  1. git log
  2. git diff {previous to last commit} {latest commit} > your_changes.patch
  3. git reset --hard origin/{your current branch}
  4. git checkout -b {new branch}
  5. git apply your_changes.patch

I can imagine that there is a simpler approach for steps one and two.

我可以想象,对于第一步和第二步,有一种更简单的方法。

回答by Tim Keating

What about:

关于什么:

  1. Branch from the current HEAD.
  2. Make sure you are on master, not your new branch.
  3. git resetback to the last commit before you started making changes.
  4. git pullto re-pull just the remote changes you threw away with the reset.
  1. 从当前 HEAD 分支。
  2. 确保你在master,而不是你的新分支。
  3. git reset回到您开始更改之前的最后一次提交。
  4. git pull重新提取您在重置时丢弃的远程更改。

Or will that explode when you try to re-merge the branch?

或者当您尝试重新合并分支时会爆炸吗?

回答by A Kok

Here is a much simpler way:

这是一个更简单的方法:

  1. Create a new branch

  2. On your new branch do a git merge master- this will merge your committed (not pushed) changes to your new branch

  3. Delete you local master branch git branch -D masterUse -Dinstead of -dbecause you want to force delete the branch.

  4. Just do a git fetchon your master branch and do a git pullon your master branch to ensure you have your teams latest code.

  1. 创建一个新分支

  2. 在您的新分支上执行git merge master- 这会将您提交的(未推送的)更改合并到您的新分支

  3. 删除你的本地主分支git branch -D master使用-D而不是-d因为你想强制删除分支。

  4. 只需git fetch在你的主分支上做一个,在你的主分支上做一个git pull,以确保你的团队拥有最新的代码。

回答by user1429980

A simpler approach, which I have been using (assuming you want to move 4 commits):

我一直在使用的一种更简单的方法(假设您要移动 4 个提交):

git format-patch HEAD~4

(Look in the directory from which you executed the last command for the 4 .patchfiles)

(查看您对 4 个.patch文件执行最后一条命令的目录)

git reset HEAD~4 --hard

git checkout -b tmp/my-new-branch

Then:

然后:

git apply /path/to/patch.patch

In whatever order you wanted.

以您想要的任何顺序。

回答by Sergey Kabashnyuk

  1. Checkout fresh copy of you sources

    git clone ........

  2. Make branch from desired position

    git checkout {position}git checkout -b {branch-name}

  3. Add remote repository

    git remote add shared ../{original sources location}.git

  4. Get remote sources

    git fetch shared

  5. Checkout desired branch

    git checkout {branch-name}

  6. Merge sources

    git merge shared/{original branch from shared repository}

  1. 结帐您来源的新副本

    git clone ........

  2. 从所需位置制作分支

    git checkout {position}git checkout -b {branch-name}

  3. 添加远程仓库

    git remote add shared ../{original sources location}.git

  4. 获取远程资源

    git fetch shared

  5. 结帐所需的分支

    git checkout {branch-name}

  6. 合并源

    git merge shared/{original branch from shared repository}

回答by Sebastian

For me this was the best way:

对我来说,这是最好的方法:

  1. Check for changes and merge conflicts git fetch
  2. Create a new branch git branch my-changesand push to remote
  3. Change upstream to new created branch git master -u upstream-branch remotes/origin/my-changes
  4. Push your commits to the new upstream branch.
  5. Switch back to previous upstream git branch master --set-upstream-to remotes/origin/master
  1. 检查更改和合并冲突 git fetch
  2. 创建一个新分支git branch my-changes并推送到远程
  3. 将上游更改为新创建的分支 git master -u upstream-branch remotes/origin/my-changes
  4. 将您的提交推送到新的上游分支。
  5. 切换回之前的上游 git branch master --set-upstream-to remotes/origin/master