DEV Community

Cover image for Rebase to the future!

Rebase to the future!

konrad_126 on May 02, 2018

Although rebase is not a complicated concept itself, many people have trouble understanding it. I believe this is mostly because people new to git...
Collapse
 
dkarlovi profile image
Dalibor Karlović

Bob can actually do this to fix it:

  1. git checkout -b feature-bob - creates a new branch from the existing feature branch for Bob, with his changes
  2. git checkout feature - go back to the original branch
  3. git fetch origin - fetch changes from remote after the rebase
  4. git reset --hard origin/feature - resets Bob's current feature branch to be identical with remote's
  5. git checkout feature-bob - go back to Bob's branch
  6. git rebase feature - include your changes into his branch

Bob now keeps working on his feature branch and just rebases on top of yours. That way you can work on the same feature, but still do your own rebase.

Collapse
 
jsgarmon profile image
JSG

From the Bob's local feature branch, he can accomplish all this with just:

  1. git pull --rebase origin feature

I use pull --rebase all the time when I don't want a merge commit every time I pull down from the public branch. Simply replays my local commits on top of the remote head.

Collapse
 
jessekphillips profile image
Jesse Phillips

Yeah I was going to mention this in the fetch flow.

It actually makes collaborating with rebase completely acceptable as long as you never merge which I think may work at times too.

Collapse
 
konrad_126 profile image
konrad_126 • Edited

Hello Dalibor,

This solution would fit the "no nice way for him to fix this situation" category in my opinion :)

Collapse
 
sparkcruz profile image
Roger Cruz

Rebase is a nice trick, but not a real workflow:

  1. use of --force to push your changes;
  2. conflicts ignored;
  3. code gets shoved at the line-numbers as it rewrites the diffs.

Just pull and merge, it's ok. Don't lie to git's history.

Collapse
 
willamesoares profile image
Will Soares • Edited

Saying that rebase is not a real workflow is relative. It will depend on what level you are rebasing things. Although it might not be the best option when you do not put your work in separate branches and instead commit everything to the default branch, I can say that rebasing makes your git history much cleaner. So a good approach in my opinion is when you work in separate branches and use rebase on those to clean history a little bit and then after that you merge to the default branch, which should give you merge commits, which believe me can be really helpful.

Collapse
 
jessekphillips profile image
Jesse Phillips

Git push --force-with-lease

That is a slightly safer push, wish the command was shorter.

Using git to communicate, I believe requires rebase.

Collapse
 
lexlohr profile image
Alex Lohr

It depends on how you work. If your branch is only being worked on by you and/or your team, just communicate before you git push -f it, so everyone will be up to date afterwards. If that doesn't work for you, merge and silently cry about your ugly git history.

Collapse
 
konrad_126 profile image
konrad_126

Hello Roger,

I wouldn't call rebase a "trick". It's a git command like any other.

Whether you'll use it depends on your(team's) workflow.

Collapse
 
davidgf profile image
David García

If the two commits of the feature branch have conflicts with the new one in master, I'll have to resolve conflicts twice, which is even worse if there are more. This is what I normally do:

git reset --soft HEAD~<Number of commits in my feature branch>
git commit --amend # Combine all the commits of the branch into a single one
git rebase # If there are conflicts, it's only a single commit
git merge

This is what I do when working in a feature branch + PR workflow, what do you think?

Collapse
 
konrad_126 profile image
konrad_126

Hy David,
Yes, this happens because during rebase git re-applies the commits (from feature branch in this case), so if you have conflicts on some commit, you must resolve them before git can try to re-apply the next commit. And if you have conflicts on every commit than you'll have to do it fore every of those commits.

Your workflow helps you avoid that "multiple" conflict resolution but you lose you commits granularity since they all get squashed into a single commit.

Only one thing - checkout interactive rebase, it's a cool feature and could help you combine a message for your squashed commits.

Collapse
 
davidgf profile image
David García • Edited

Exactly, but I actually prefer to have a single commit for a feature, while I like to have the whole commit history of my branch while I'm working on it, so yeah, interactive rebase is a good choice for that :)

Collapse
 
jessekphillips profile image
Jesse Phillips

I need to evaluate more cases, but if each commit conflicts, you don't eliminate any merge conflict by squashing. You'll just get one big collection of conflicts to deal with.

Collapse
 
thebouv profile image
Anthony Bouvier • Edited

I'll have to admit that I rarely use rebase. I don't mind the "messier" git history, as as far as I am concerned it is a true history.

On top of that, I am of the "commit early, commit often" mindset and hand in hand with that is "push your branch to origin often IN CASE OF FIRE".

And since I instilled this in my team as well, our branches are pushed up public often, which is exactly the scenario one should not be rebasing from.

So it works out for us just fine.

FIRE

Collapse
 
jessekphillips profile image
Jesse Phillips

Vim has the true history which is actually really helpful during initial changes. But I don't commit my vim history to git, I have a different goal when I commit.

Collapse
 
tmcsquared profile image
TMcSquared

Thanks for explaining rebasing! :D

Collapse
 
colinmtech profile image
Colin Morgan

Where we're going, we don't need old commit ids.

But seriously, you combined one of my favorite movies with my favorite git command. Nailed it!

Collapse
 
chiangs profile image
Stephen Chiang

Really well explained!

Collapse
 
serafss2 profile image
Fares B.

I don't suggest using rebase because if that red node on master removes some dependencies and functions that you actually need in those purple nodes, your commits won't build.

Collapse
 
jessekphillips profile image
Jesse Phillips

That is fair I suggest using commit --fixup to get the solution to the right commit. You still need to make a solution on merge.