开发者

pushing multiple unrelated commits to remote git repo

开发者 https://www.devze.com 2023-03-17 22:28 出处:网络
I have a situation where I am working on two different projects on a particular branch. I have finished project \"A\", committed the change, and pushed the change up to the remote. This change \"A\" i

I have a situation where I am working on two different projects on a particular branch. I have finished project "A", committed the change, and pushed the change up to the remote. This change "A" is currently under review so it hasn't been merged yet to the branch. Meanwhile I have started working on an unrelated change for another project in the same branch, call this change "B". I have finished coding this change as well and I'm ready to commit and push this.开发者_JAVA技巧 But I'm not sure how this should be done since change "A" hasn't been merged yet. How can I push these two changes "A" and "B" as separate changes that don't depend on each other?


First you need to identify the commit that will be the common parent of Change A and B. As you have described it, this is the parent of commit A. Use git log to find the hash for the commit before A. Note that you can just type the first few letters/numbers of the commit hash. Let's pretend it is "parent_commit"

Now checkout this commit, which we are going to use as the head of a new branch for Change B.

git checkout parent_commit

Next create a new branch from it, where we will commit the B changes.

git checkout -b changeB

Finally, use git add and git commit to commit your Change B on this new branch. When done, do a git push to push your commit B from your new branch that is independent of change A. Alternatively, if you have already committed Change B, then use git cherry-pick commitB then git pull.

Now you will have your original branch with A, and a new branch with B, sharing the history up to the parent of A.

Closing remark: if you had already committed B and made a branch for it (and had it checked out) you could have done the rebase one-liner mentioned in the other answer:

git rebase --onto master commit_A_sha


Essentially the same result as Shelhamer's response; but you can use git rebase --onto to achieve the same result in a single command. Locate the SHA id of the 'old' parent (A in your example), and the SHA id of the new parent (say master). Then assuming you have got the branch in question (B) checked out, run:

git rebase --onto <new parent> <old parent>

This will essentially 'pick-up' everything on branch B from A onwards and will move it so it's based off the master. Then just git push as normal.

0

精彩评论

暂无评论...
验证码 换一张
取 消