Updating the current branch from parent branch

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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 merge conflict resolutions (if any), it can be pushed to remote.

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.

But to finish out the "complete cycle" (let's say you get the conflicts resolved)

Now is the time to

BUILD your code. If it works, continue, if it does not build...fix it.

TEST your code. Run your unit-tests locally. If the unit tests are broken...then address the issue(s). Once they work, continue.

At this point, you have your local code healthy.

Now you need to push your code to remote server.

git commit -m "Use a meaningful message here"

git push

At this point, if you create a PR (or open an existing one) that is trying to do a

feature/mychildbranch -> feature/myparentbranch

You should only see what is different between your "feature/mychildbranch" and "feature/myparentbranch"... and without any "conflict files" listed in the PR. (Obviously, it can be a "moving target" if others are checking code into "feature/myparentbranch".)

Solution 3 - Git

Assuming your call to create B was git clone /path/to/server/A, you just have to do a git pull and you're done. That's how git pull works: first it fetches the changes from the upstream (the tracked branch A in your case), then it merges these changes into the branch that tracks the tracked branch (B in your case).

The Git Book and Pro Git discuss that topic in depth, so they're quite worth reading (if you're not in a hurry, read the rest of them too).

Solution 4 - Git

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

Blog post describing reason for doing it this way

Solution 5 - Git

at child branch B, we can do

git merge origin/A 

This would keep it in sync with parent's origin.

Solution 6 - Git

Add upstream Git repository

git remote add upstream <PARENT_REPOSITORY_URL>

Pull remote upstream branches
git fetch upstream
Checkout to the local branch to update
git checkout master
Merge latest changes from the upstream Git repository
git merge upstream/master

Solution 7 - Git

For merging with parents:

It is very important to run both commands:

  1. git fetch [to pull all meta data associated to branches]
  2. git merge parentBranchName

Just FYI: Now in your local history/logs you will see list of commits but this will commit your changes associated to parent branch in your local not on remote.

To see your all changes on remote like github, just execute

  1. git push

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser482594View Question on Stackoverflow
Solution 1 - GitHackerGilView Answer on Stackoverflow
Solution 2 - GitgranadaCoderView Answer on Stackoverflow
Solution 3 - GiteckesView Answer on Stackoverflow
Solution 4 - GitAndyView Answer on Stackoverflow
Solution 5 - GitAditya RewariView Answer on Stackoverflow
Solution 6 - GitNaga Lokesh KannumooriView Answer on Stackoverflow
Solution 7 - GitRohit KumarView Answer on Stackoverflow