How to manage Multiple branches on Git


I am working on a project using git where I’ve been assigned two tasks. Let’s assume task A and task B.

I started by forking the project and created a branch named A at this point I was not aware I had to do task B. After forking the project I’ve cloned the project to my local pc.

Next I ran following command:

git checkout -b A — created branch A

I made changes to project as per the requirement then I did the following

git add *

git commit -m "message"

git push origin A

Now I was assigned task B for which I created the branch named B.

git checkout -b B

Made changes as per the requirements and committed those changes to branch B.

Now the issue is Branch B contains changes also made in A, instead it should follow the main branch plus changes required as per task B. How can I fix this issue? Branch B should follow main branch and changes as per task B on branch B.

I tried git checkout main but no such branch exist.


Try with this:

# Checkout the master branch
git checkout master

# Create a fix branch
git branch task-b-fixup

# Get history of branch task-b
git log --oneline --graph --decorate task-b

# Cherry pick commits from task-b by repeating the following command
git cherry-pick {{task-b commit hash}}

Now, if you want, you could destroy the old task-b branch.

Answered By – Antonio Petricca

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published