Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Rebase from Remote Master

This may have been asked before but I am a little confused on the terminology, so I am not sure what command(s) are the correct ones to accomplish what I need to accomplish. Right now I have a GitHub repository I am working from with my team. On there, I made a branch for a task. I cloned the repository to my local machine and then checked out that branch locally. I started making changes that I wish to continue working on... While making these changes, my team has been updating their code and pushing changes back into the master branch on GitHub. How do I pull down their changes from the master branch on GitHub so that it updates my branch locally AND on GitHub as well (Right now on GitHub, it says I am 1 commit ahead and 19 commits behind master). Thanks for the advice here...this will be something I plan to do regularly so learning the right process would be instrumental for me...

like image 554
Chris Moretti Avatar asked Mar 07 '17 02:03

Chris Moretti


People also ask

How do you rebase against a master?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).

Can you rebase after pushing to remote?

If you had already pushed changes before using THAT option, those changes wouldn't be rebased because they're already in the remote. The only exception may be if you have multiple remotes, and have pushed changes to one remote, then do a pull/rebase from another - that could cause serious problems.


1 Answers

There are a series of methods you could apply here to accomplish what you want. And that may depend on your repository policy. For instance, if you rebase your branch you'll not be able to push it back to the server, you would have to either push it using -f or deleting the remote branch and push a new one. Some could say rebase is not a good approach once you have already pushed you branch to the server, if that's the policy of your repo so you would have to merge the master into your branch and than push it back to the remote.

Let's talk about both approaches:

Rebase:

I personally like doing rebase more, but only if I'm sure no one else is using the branch which I'm pushing to on the remote. I like rebase because this ends up leaving a cleaner history, it does that because it removes my changes from the branch, applies the remote changes and then apply my changes back on top, but, doing that, it changes the commit hashes and it does not match with the ones you had previously for the same commit, for that reason you have to either push it using -f or delete the remote branch and creating it back again.

I would do like this, on your local repository I would go to the master branch:

git checkout master

then update it with the remote changes:

git pull

It will run a git fetch with merge behind the scenes on your git master branch.

Then go back to you branch:

git checkout my_branch

and then rebase it with the master:

git rebase master

If everything runs without conflict you're good to go.

you can either use:

git push -f origin my_branch

(BE CAREFUL! this command will override your remote branch, it's very dangerous make sure everything went fine on your rebase before using this command.)

or you could delete your remote branch end push it normally back to the server:

git push origin my_branch

Merge:

Using this approach you'll end up with a merge commit on your history, and the log can get confusing over time, but there are some advantages on this as well, if you do have a conflict, you need to resolve it only once, when you're merging, if you have a conflict when you're rebasing, you may have to resolve it multiple times, since it will try to apply your commits over the new remote commits one by one.

To do this you can just go to your master branch and update it:

git checkout master

and:

git pull

will do the same as we saw earlier

now go back to your branch:

git checkout my_branch

and run:

git merge master

if you do not have any conflict you're good to go and you can push it to the server, if you do you only have to resolve it and you can push it normally to the server as well since now you resolved the conflicts on a new commit, there is nothing diverging between your local branch and your remote one.

git push origin my_branch

And that's it, I hope you find this helpful, you can probably do this in fewer steps, but these are just the steps I like to make, they may not be the shorter ones but I feel safer this way. And of course there are lots of options on merging and rebasing I would advise you to look more into it they are very useful.

like image 101
Marcone Avatar answered Sep 28 '22 15:09

Marcone