git rebase onto remote updates
Solution 1
I have found, over time, my favorite solution is:
git checkout topic
# make [n] commits
git rebase -i HEAD~[n] #clean up time
git checkout master
git pull --rebase
git checkout topic
git rebase master
git checkout master
git merge topic
git push origin
Solution 2
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.
Solution 3
For our team we set something up which works very well and does not use rebase at all.
We cut our work in Jira tickets which do not take too long, typically 1-2 days effort. Each dev creates a branch per ticket and works on this branch. When ready to share this is pushed to the central server.
The central server is monitored by a hudson CI server which pulls the changes, merges all updated branches, rebuilds the software, runs the tests and pushes everything to the central master git repository.
From there we pull it back to our repositories. We do regularly (i.e. every couple of days) merge our working branches with the central master to keep them 'close'.
Since nobody is working on the 'merging' machine and nobody other than the CI server touches the master, we have very infrequently merging issues (in about 1-2% of the commits). And these are quickly resolved on the build server by cleaning the workspace. We found we could have avoided most of these by keeping the branches short and merging with the remote master before pushing.
I also fond that merging is much more robust than rebasing and requires a lot less of rework.
Solution 4
You can also just rebase against the up-to-date master
git checkout topic_1
git rebase refs/remotes/origin/master
Which obviates the need for the pull (at least until the EOL of your feature branch). Our process uses GitHub pull requests so I never need to do that locally.
Related videos on Youtube
Comments
-
Blake Chambers over 3 years
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?
-
Blake Chambers about 14 yearsThat's great for slower development, but we have an environment that requires some commits to go up nightly while others wait for a larger release. I guess one way to handle that is to setup nightly and release branches on the remote repository... but that sounds like it might invite even more of these merge commits if not respected fully. While I also believe that merging is more 'robust', Our team is capable of figuring out git-rebase and working through issues. Having a more readable commit history is as important to me.
-
Blake Chambers over 13 yearsyes, and I use it frequently when updating a shared code base to the latest from remote. However, this post was more about feature branches a more streamlined way to do so and git pull rebase isn't it IMO. However, the branch auto rebase is an awesome feature and I have already added it to a few repos. Great tip! :)
-
Evan Plaice over 12 yearsIs the last merge necessary. I can imagine the novelty of seeing all the working forks on the graph but the code is already integrated into the master and I don't think it's unreasonable to toss out working code if it doesn't become a genuine fork. Otherwise IMHO, this answer is perfect.
-
Evan Plaice over 12 yearsPersonally, I think merged forks are ugly/messy in the history so I always rebase temp/developmental branches to the remote origin. Still, I find it interesting that your group has effectively implemented a merge-only plan with few issues and measurable benefits. Do you rebase on your regular pull schedule to keep things 'closer' to the central master? What does "cleaning the workspace" entail? And, do you pull and rebase to check for conflicts before merging with the central repository?
-
Peter Tillemans over 12 years@EvanPlaice Cleaning the workspace is erasing the working directory on Jenkins. The next time a fresh clone is made.