本文介绍了Git Clean分支仅保存我的更改和主控的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分支,我在其中做了一些更改,但是最初我是错误地从错误的分支中创建了它,所以我不想在其中进行许多不同的更改.那么,如何清理它,使我只完成所做的更改以及从master分支进行更改?

I have a branch, where I did some changes, but I'd originally mistaken and create it from wrong branch, so I have many different changes I don't want to have in it. So how can I clean it so I only have changes I've done and changes from master branch?

推荐答案

您可以从master分支创建一个新分支,然后将您所做的更改挑选到新分支中.

You can create a new branch from master and then cherry-pick the changes you have made into the new branch.

找到要保存的每个提交的提交哈希.然后:

Find the commit hashes for every commit you want to save. Then:

git checkout master
git checkout -b <new branch name>
git cherry-pick <commit hash> # for every commit you want to save

cherry-pick当您有很多提交时,单次提交可能很乏味.由于git 1.7.2+ cherry-pick可以处理提交范围.

cherry-picking single commits can be tedious, when you have a lot of them. Since git 1.7.2+ cherry-pick can handle commit ranges.

git cherry-pick <first commit to save>^..<last commit to save>

正如EOL在评论中指出的那样,cherry-pick依次应用每个补丁,并在出现冲突时等待用户提交它.在这种情况下,请解决冲突并执行git cherry-pick --continue以自动移至下一个提交.或使用git cherry-pick --abort中止整个操作.

As EOL pointed out in the comments, cherry-pick applies each patch in turn and waits for the user to commit it, if there are conflicts. In this case, resolve the conflicts and do a git cherry-pick --continue to automatically move to the next commit. Or use git cherry-pick --abort to abort the whole operation.

现在检查您当前的分支.如果一切正常,则可以删除以前的混乱分支:

Now inspect your current branch. If everything worked well, you can delete the previous messed-up branch:

git branch -D <old messed up branch name>

请参见 git cherry-pick手册页以获得更多详细信息.

See the git cherry-pick manual page for further details.

编辑:包含有关git cherry-pick --continue的信息,EOL在评论中提到了该信息.

included info about git cherry-pick --continue, which EOL mentioned in the comments.

更新

您提到您只想挑选那些您创建的提交.这可以通过以下bash脚本来完成:

You mentioned you want to cherry-pick only those commit you created. This can be done with this little bash script:

author_name="Your Git Author Name"
start_commit="earliest commit hash to cherry-pick"
end_commit="latest commit hash to cherry-pick"

git rev-list --reverse --topo-order "$start_commit^..$end_commit" | while read rev
do
  commit_author_name=`git log --pretty=format:"%an%n" -n 1 $rev`
  if [[ $commit_author_name == *"$author_name"* ]]; then
    git cherry-pick $rev || break
  fi
done

这篇关于Git Clean分支仅保存我的更改和主控的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:43