简体   繁体   English

Git clean分支只保存我对master的更改和更改

[英]Git clean branch to save only my changes and changes from 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? 那么我怎样才能清理它,所以我只做了一些改变,并且从主分支改变了?

You can create a new branch from master and then cherry-pick the changes you have made into the new branch. 您可以从master创建一个新分支,然后挑选您对新分支所做的更改。

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 ing single commits can be tedious, when you have a lot of them. cherry-pick单一提交可能是乏味的,当你有很多。 Since git 1.7.2+ cherry-pick can handle commit ranges. 由于git 1.7.2+ cherry-pick可以处理提交范围。

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

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. 正如EOL在评论中指出的那样,如果存在冲突,cherry-pick依次应用每个补丁并等待用户提交。 In this case, resolve the conflicts and do a git cherry-pick --continue to automatically move to the next commit. 在这种情况下,解决冲突并执行git cherry-pick --continue继续自动移动到下一个提交。 Or use git cherry-pick --abort to abort the whole operation. 或者使用git cherry-pick --abort中止整个操作。

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>

See the git cherry-pick manual page for further details. 有关更多详细信息,请参阅git cherry-pick手册页

Edit: included info about git cherry-pick --continue , which EOL mentioned in the comments. 编辑:包含关于git cherry-pick --continue ,EOL在评论中提到。


Update 更新

You mentioned you want to cherry-pick only those commit you created. 你提到你想要挑选你创建的那些提交。 This can be done with this little bash script: 这可以通过这个小的bash脚本来完成:

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

If your changes are all made on top of the upstream branch (ie you don't have any merge commits from upstream mixed in with your changes), you can just rebase it on top of master. 如果您的更改都是在上游分支之上进行的(即您没有任何来自上游的合并提交与您的更改混合在一起),您可以在master之上重新定义它。

git branch backup # always make a backup first ;) git branch backup#总是先备份;)
git rebase --onto master <latest commit from wrong upstream branch> git rebase --onto master <来自错误的上游分支的最新提交>

Assuming that your branch is up-to-date with respect to the upstream, that's just 假设您的分支机构与上游相关,那就是最新的

git rebase --onto master <wrong upstream branch> git rebase --onto master <错误的上游分支>

Afterwards, you might want to change the tracking branch of your current branch to master: 之后,您可能希望将当前分支的跟踪分支更改为master:

git branch --set-upstream <your branch> origin/master git branch --set-upstream <你的分支> origin / master

(or just git branch -u origin/master with git >=1.8) (或者只是git branch -u origin / master with git> = 1.8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM