Updating the current branch from parent branch

I created a new git branch B from branch A with tracking option.

Now, when A branch gets updated by few commits, I want to pull the commits to B as well, so I can keep track of it, and do not have to face big change sometimes later.

How should I approach this? Is it automatically done in git?


Solution 1:

This is not made automatically. You have to manually merge your changes from A to B, which is pretty simple. Just switch to branch B and do

git merge A

Which will automatically merge your changes from A to B. As long as you don't have any conflicts, all of the changes in A will be marked as merged in B. A common best practices is to make daily merges, but that is dependent on the number of users/commits using your branch.

Solution 2:

Here is how I got it to work.

My approach is that I am going to create a new folder, and put the below calls (in that same new folder)..so I know I have only "fresh" code on local (but "fresh" (as possible) from the remote server), and not any accidental local changes. Aka, a "new folder clean" approach. It will retrieve the 2 branches of interest (freshly/newly retrieved) to local ... and do the merge (locally). After resolutions, it can be pushed (not shown).

short version:

git checkout  feature/mychildbranch

git branch

git checkout  feature/myparentbranch

git pull

git branch

git checkout feature/mychildbranch

git branch

git merge feature/myparentbranch

longer version (explained) I'll use /* as comments */

/* first, make sure you at least have the child branch */
git checkout feature/mychildbranch


/* ok, just show the branches.  make sure at least feature/mychildbranch exists  note the "*" below says "this is the branch i am on" */
git branch
    * feature/mychildbranch
      feature/myparentbranch


/* now checkout the parent branch...note the "switched" happens automatically with the checkout */    
git checkout feature/myparentbranch
    Switched to branch 'feature/myparentbranch'
    Your branch is up to date with 'origin/feature/myparentbranch'.

/* now pull, the pull will occur on the branch you are currently on, which should be feature/myparentbranch at this point */    
git pull
    remote: Enumerating objects: 69, done.
    remote: Counting objects: 100% (55/55), done.
    remote: Compressing objects: 100% (22/22), done.
    remote: Total 22 (delta 17), reused 0 (delta 0)
    Unpacking objects: 100% (22/22), done.
    From https://mygit.hub.com
       96ae0e9..6eb0a03  feature/myparentbranch -> origin/feature/myparentbranch
        * [new branch]      feature/blah blah blah (specific to my stuff only)
       xb99638..x86db6f  master                  -> origin/master
    Updating x6ae0e9..xeb0a03
    Fast-forward
     .../somefileone.txt | 30 ++++++++++++--------
     .../somefiletwo.txt       |  7 +++--
     .../somefilethree.txt  |  6 ++--
     X files changed, Y insertions(+), Z deletions(-)
     create mode 100644 somenewfileone.txt

/* do a git branch just to show that you're on feature/myparentbranch */    
git branch
  feature/mychildbranch
* feature/myparentbranch

/* ok, now (above) you have the latest-greatest feature/myparent, lets do a checkout on the child to switch to the child */    
git checkout feature/mychildbranch
Switched to branch 'feature/mychildbranch'
Your branch is up to date with 'origin/feature/mychildbranch'.

/* another sanity check, show you're on feature/mychildbranch */
git branch
* feature/mychildbranch
  feature/myparentbranch

/* finally, the magic.  do a merge from feature/myparentbranch (which you know is local and up to date because of the voodoo above */    
git merge feature/myparentbranch
Merge made by the 'recursive' strategy.
 .../somefileone.txt | 30 ++++++++++++--------
 .../somefiletwo.txt       |  7 +++--
 .../somefilethree.txt  |  6 ++--
 X files changed, Y insertions(+), Z deletions(-)
 create mode 100644 somenewfileone.txt

If there are no conflicts, you should be where you want to be. (remembering this is "local"... now you can do a commit and push to remote)

If there are conflicts, that's a whole new SOF question/answer IMHO.

Solution 3:

Another option is to do a git fetch A and git merge A.

Blog post describing reason for doing it this way