Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase onto remote updates

Tags:

I work with a small team that uses git for source code management. Recently, we have been doing topic branches to keep track of features then merging them into master locally then pushing them to a central git repository on a remote server. This works great when no changes have been made in master: I create my topic branch, commit it, merge it into master, then push. Hooray.

However, if someone has pushed to origin before I do, my commits are not fast-forward. Thus a merge commit ensues. This also happens when a topic branch needs to merge with master locally to ensure my changes work with the code as of now. So, we end up with merge commits everywhere and a git log rivaling a friendship bracelet.

So, rebasing is the obvious choice. What I would like is to:

  • create topic branches holding several commits
  • checkout master and pull (fast-forward because i haven't committed to master)
  • rebase topic branches onto the new head of master
  • rebase topics against master(so the topics start at masters head), bringing master up to my topic head

My way of doing this currently is listed below:

git checkout master git rebase master topic_1 git rebase topic_1 topic_2 git checkout master git rebase topic_2 git branch -d topic_1 topic_2 

Is there a faster way to do this?

like image 442
Blake Chambers Avatar asked May 26 '10 21:05

Blake Chambers


People also ask

Does git rebase change remote?

No, locally rebasing doesn't change the remote.

How do I push changes to remote after rebase?

To push the changes to the branch after a rebase, you need to force push your commits using the -f or --force flag in the git push command on Git. This is because something has changed in the remote branch and the commit history is different for both the remote and your local branch.

Does git rebase overwrite changes?

If another user has rebased and force pushed to the branch that you're committing to, a git pull will then overwrite any commits you have based off that previous branch with the tip that was force pushed. Luckily, using git reflog you can get the reflog of the remote branch.

What does git rebase onto do?

git rebase --onto allows you to rebase starting from a specific commit. It grants you exact control over what is being rebased and where. This is for scenarios where you need to be precise. For example, let's imagine that we need to rebase HEAD precisely on top of F starting from E .


1 Answers

Do you know about git pull --rebase? It rebases rather than merging when you pull, and prevents merge commits from polluting your history.

You can also set it up as the default behaviour for a branch with the branch.<name>.rebase and branch.autosetuprebase config options.

like image 87
Martin Owen Avatar answered Sep 20 '22 21:09

Martin Owen