Rebasing local Git branch before push to remote

10,397

After creating your "new_feature" branch the you will have a state like

o   <master> <origin/master> <new_feature>  most recent commit
|
...

Then, after commiting your changes to your local branch your repository will look like

o   <new_feature>   your last commit
|
o   your first commit
|
o   <master> <origin/master>    most recent commit
|
...

Doing a

git pull --rebase origin master

, as Cupcake suggests, you will end with

o   <new_feature>   your last commit
|
o   your first commit
|
o   <origin/master> something meanwhile commited on remote master
|
o   <master>    most recent commit
|
...

your changes rebased on top of "origin/master". These are not your original commits but commits changed to fit on the "new" "origin/master".

Doing rebase you can get merge conflicts, because changes made on remote master may conflict with your changes.

But because "new_feature" is now "on top of" "origin/master" you can do a push to the remote master.

This will also move the tag "origin/master" to the level of "new_feature". If you also want to have your local "master" on track, you have then to check it out and do a merge with "origin/master".

Share:
10,397

Related videos on Youtube

script_kiddie
Author by

script_kiddie

scaling is the mantra of life

Updated on October 11, 2022

Comments

  • script_kiddie
    script_kiddie over 1 year

    Remotes: origin

    $ git branch
    * master
    
    $ git checkout -b "new_feature"
    

    Now I do couple of commits on "new_feature" branch and want to push it to origin after updating it.

    $ git branch
    master
    * new _feature
    
    $ git pull --rebase origin new_feature    
    $ git push origin new_feature
    

    Is this the correct way to update the local branch before pushing to remote?