Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Forked from santisbon/Update-branch.md
Created September 17, 2019 17:38
Show Gist options
  • Save whoisryosuke/36b3b41e738394170b9a7c230665e6b9 to your computer and use it in GitHub Desktop.
Save whoisryosuke/36b3b41e738394170b9a7c230665e6b9 to your computer and use it in GitHub Desktop.
Bring your feature branch up to date with master. Deploying from Git branches adds flexibility. Bring your branch up to date with master and deploy it to make sure everything works. If everything looks good the branch can be merged. Otherwise, you can deploy your master branch to return production to its stable state.

Updating a feature branch

First we'll update your local master branch. Go to your local project and check out the branch you want to merge into (your local master branch)

$ git checkout master

Fetch the remote, bringing the branches and their commits from the remote repository. You can use the -p, --prune option to delete any remote-tracking references that no longer exist in the remote. Commits to master will be stored in a local branch, remotes/origin/master

$ git fetch -p origin

Merge the changes from origin/master into your local master branch. This brings your master branch in sync with the remote repository, without losing your local changes. If your local branch didn't have any unique commits, Git will instead perform a "fast-forward".

$ git merge origin/master

Check out the branch you want to merge into

$ git checkout <feature-branch>

Merge your (now updated) master branch into your feature branch to update it with the latest changes from your team.

$ git merge master

Depending on your git configuration this may open vim. Enter a commit message, save, and quit vim:

  1. Press a to enter insert mode and append text following the current cursor position.
  2. Press the esc key to enter command mode.
  3. Type :wq to write the file to disk and quit.

This only updates your local feature branch. To update it on GitHub, push your changes.

$ git push origin <feature-branch>
@sunbuhui
Copy link

thanks, really help me a lot😊

@pablocar80
Copy link

is this different from executing git pull origin master in our feature branch?

@tavisca-tgoyal
Copy link

@pablocar80 I think this will only update the master branch, not your feature branch. I'm also not sure about this.

@loughlincodes
Copy link

Handy I always come back to this one for reference :)

@mariogusman
Copy link

Saved my life thanks!

@Devprasad
Copy link

@pablocar80 same actually. Pull is internally fetch followed by a merge.

@BharathKoneru471
Copy link

git pull = git fetch + git merge.
I think we can use "git pull origin master" for the local master branch, to stay updated with the remote master branch.
Then we can checkout to the feature branch on to which, we can merge the work of the local master.

@varuntheestallion
Copy link

@pablocar80 @tavisca-tgoyal yes, this simplifies the first couple of steps though, so you don't have to switch branches at all

@brandaspt
Copy link

@BharathKoneru471 @varunmathuria can I do git pull origin master directly from my feature branch? If so what happens exactly? Does it update both my feature and local master branches?

@sandramolina
Copy link

Awesome, thanks!

@bennami
Copy link

bennami commented Mar 11, 2022

@BharathKoneru471 @varunmathuria can I do git pull origin master directly from my feature branch? If so what happens exactly? Does it update both my feature and local master branches?

git pull origin master will pull changes from the origin remote master branch and merge them to the local checked-out branch.

@taylor-codes
Copy link

I always come back to this. Thanks!

@noak-salmgren
Copy link

Great explanation, thanks!

@lbattaglioli2000
Copy link

thank you for this ❤️

@amajoros16
Copy link

Love it , works just the way we need it

@TheoKondak
Copy link

Thanks well written, to the point!

@zaib-khan
Copy link

Thank you very much 👍

@maddox05
Copy link

maddox05 commented Feb 21, 2023

Thanks! (I didn't have access to my account when I (ie someone) sent this??)

@StellarBlocks-Assistant
Copy link

Thanks!

@AhlamYu
Copy link

AhlamYu commented Mar 5, 2023

Thank you for this.

@SowmyaPhilips
Copy link

is there way to update automatically using yml code

@raykipkorir
Copy link

Thanks a lot. Great explanation 👍

@JayeshJain-SF
Copy link

Thanks for this explanation

@iamsoorajsingh
Copy link

Thanks a lot ...really appreciated your efforts

@nrowley71115
Copy link

Is this the same as rebasing?

@RangeOfGlitching
Copy link

@nrowley71115

Rebase is generally not recommended if the branch is already on the remote.

Rebasing rewrites the git history, which means you'll need to use the force option when pushing (since the branch is already on the remote).
Additionally, if there are multiple ppl are working on the same branch, this is error-prone and can lead to conflicts when others pull the branch.
Moreover, in a project with a CI/CD process, rebasing can lead to a cascade of issues.

@DrunkSunGod
Copy link

Saved my life thanks

@keilin-anz
Copy link

It's not always convenient to switch back to main (or master) as occasionally a feature branch may have changes which will necessitate stashing before the switch. (turning the OP into roughly git stash && git checkout master && git pull && git checkout - && git stash pop && git merge master (yikes)

I tend to prefer the simpler equivalent which avoids all that:

git fetch origin master
git merge origin/master

Disclaimer: if you have changes which would have prevented a switch to main in the first place you'll still have to deal with them of course - just won't have to do quite so many steps beforehand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment