niksrb
niksrb

Reputation: 459

The right way to push changes to git

I've got two branches. First branch, let's call it branch_A is created from master. Several commits and pushes were there and after that I have created another branch branch_B from branch_A. Now, after few pushes here, I have realized that I need to fix something on branch_A, but I'll also need those changes on branch_B.

What would be the steps? Should I checkout branch_A, make those changes and push them, and after checkout branch_B and do a pull? Would it be pull origin branch_A or pull origin branch_B?

Upvotes: 1

Views: 180

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522719

If you haven't yet pushed branch_B, then I suggest doing a rebase:

# from branch_B
git rebase branch_A

This will rewrite branch_B to bring in the latest changes from branch_A, including whatever fix you needed to make in the latter branch. After the rebase is completed, branch_B will appear to have been created on top of a branch_A that never had the problem you fixed.

Note that after the rebase, you will have to force push the branch_B:

git push --force origin branch_B

The reason for the force push is that you have rewritten the history of that branch.

If branch_B has already been pushed and is shared by someone other than you, then cherry picking the fix commit might make the most sense:

# from branch_B
git cherry-pick abc123

Here, replace abc123 with the commit hash from the fix you made in branch_A.

Upvotes: 1

Related Questions