Updating the current branch from parent branch

at child branch B, we can do

git merge origin/A 

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


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).


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.


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".)

Tags:

Git