niksrb
niksrb

Reputation: 459

Git pushing to a branch which has new branches from itself

I have created new branch from master and then another branch from new one:

master --> A --> B

Later I found out that some things should be fixed on branch A, so I went back there and maid those changes. Can I push those changes to branch A and later merge them with branch B (I know that I could make those changes on branch B directly, but I have made a mistake checking out branch A and now changes are here...) or should I create another branch from branch A and push those changes to that branch and later merge it with branch B?

master --> A --> B 
           |         
            ---> C 

If both ways are possible, what would be the difference between them, is one way better then the other?

Upvotes: 1

Views: 191

Answers (3)

user
user

Reputation: 977

You can make changes in branch A and then rebase branch B. Here is the information on how to rebase : https://git-scm.com/book/en/v2/Git-Branching-Rebasing

Upvotes: 0

Prithi Pal Thakur
Prithi Pal Thakur

Reputation: 11

I am assuming that you have

  1. created the first branch i.e master.
  2. created a new branch A by selecting the master branch and take checkout using the command git checkout -b A
  3. created another branch B by selecting the A branch and take checkout using the command git checkout -b B

Later, you want to change the code in branch A. Yes, you can push the changes, to do so, you should select the branch A by git command git checkout A. Make the changes and commit in branch A.

If you want to merge the changes of branch A in branch B, follow the steps:

  1. Select the branch B using the git command git checkout B
  2. Merge with branch A using the git command git merge A

You can find the current branch by using the git command git branch.

Yes, you can use both approaches to achieve your goal

  1. make changes in Branch A and merge with branch B
  2. create a new branch from branch A, make the change in a new branch and merge with branch B.

In the 2nd approach, we are unnecessary creating an extra branch from branch A. FYI, If you merge branch A with branch B, it will not delete the branch A. Branch A still there unless until you delete the branch by using the git command git branch -d source-branch.

Upvotes: 1

Shrey Garg
Shrey Garg

Reputation: 1327

  • If no commits are made to branch B yet, you can simply make the fixes required on branch A and then merge it to branch B. This will not show a merge commit as there will be no commits on branch B to merge.
  • Although, if there are commits made to branch B, you can either create a temporary branch (eg. C, hotfix) from branch A, then merge it with A and B, else you can directly make commits to branch A then merge with B. Both these ways are correct, if the fixes are small, then you can directly commit on A, else make a new branch and go ahead.

Upvotes: 3

Related Questions