Rahul
Rahul

Reputation: 603

How to pull a branch which is being created from another branch in GIT

I have a codebase in a branch developmentV2. From developmentV2 I have created a branch rahulV2 as below.

git checkout -b rahulV2 developmentV2
git push origin/rahulV2

I have made some changes in rahulV2 branch.Before pushing it to rahulV2 branch I want to have updated code from developmentV2 if anyone has pushed some code to developmentV2.

So I stash my changes in rahulV2.

git stash

if I will use

git pull

it will get pulled from origin/rahulV2.

I want to pull developementV2 code so that rahulV2 will be updated and after pushing the code I can merge my rahulV2 changes to developmentV2 branch.

Please help me out with this.

Upvotes: 1

Views: 2152

Answers (5)

I think the fact that the branch is being created by another will not affect the pull effect. What you need is just pull it and get the changes you want, anyway, by the time you get the changes it will not matter if they came from a branch that comes from another.

Hope this was helpful

Upvotes: 0

Rahul
Rahul

Reputation: 603

I tried

git checkout developmentV2

git pull

Here there were new 15 commits which has been pulled.

Then I tried

git checkout rahulV2

Here it is showing Your branch is ahead of 'origin/rahulV2' by 15 commits.

Upvotes: 0

Josef Ginerman
Josef Ginerman

Reputation: 1560

There are two accepted ways of updating your branch with your parent branch (or any other branch actually). This are: Merge and Rebase.

Rebase: Rebase means that you change the history of your commits. In Git every commit has a parent commit. What rebase does is change the parent commit of the first commit of your branch (when your branch started) to the last commit of the branch you rebase on. This changes the history of your commits, but makes it look tidier. The way to do it is:

git checkout developementV2
git pull                         # to make sure you rebase onto the updated version
git checkout rahulV2
git rebase developementV2

Merge: With this method you don't change the history of your commits. All this does is create a new commit with both the changes in your branch and your base branch (in this case developmentV2 and rahul2). Merge is less dangerous than rebase, for it can generate conflicts only one time, while rebase can give you conflicts for every commit between your old and your new origin commit. The way to merge is:

git checkout developementV2
git pull                         
git checkout rahulV2
git merge developementV2

In both cases you might get conflicts. This means you have to go into the code and decide which version you'll want to take.

Upvotes: 2

Urvi Soni
Urvi Soni

Reputation: 314

origin/rahulV2 is a remote tracking branch, and gets updated with changes from the remote repository every time you do a git fetch. On the other hand, rahulV2 is your local version of this branch. rahulV2 may be out of sync with origin/rahulV2 which in turn may be out of sync with what is actually on the remote repository.

Hence the difference in doing a merge will depend on the differences in the various incarnates of rahulV2. If you want to merge the very latest rahulV2 into your developmentV2 then you should do the following:

git checkout rahulV2        # update remote tracking rahulV2
# git pull origin rahulV2     
git checkout developmentV2 # switch to developmentV2
git merge rahulV2          # merge

Upvotes: 0

Romain Valeri
Romain Valeri

Reputation: 22057

This is quite typical workflow.

git checkout developementV2
git pull
git checkout rahulV2
git merge developementV2

But just in case this is a recurrent task, maybe consider an alias?

git config --global alias.upd '!f() { git fetch && git checkout developementV2 && git merge --ff-only origin/developementV2 && git checkout -; }; f'

then each time you need to get your rahulV2 branch up to date with the state of developementV2, do

# update your developementV2 branch from remote
git upd

# merge changes in
git merge developementV2

Upvotes: 0

Related Questions