2

Some time ago I pulled master. I then created a branch off of master, branch1. I've done quite a bit of work in branch1. I have several commits, and since my last commit I have changed a lot of files and added new files, possibly deleted some files. So my working directory is currently not clean.

Someone else has now merged a bunch of changes into master on the server, and I need to pull those changes to master and rebase my work.

In order to rebase branch1 to the latest master, do I need a clean working directory? In other words, do I need to commit my current work before rebasing? I'm not clear how this works.

David Burson
  • 2,947
  • 7
  • 32
  • 55
  • 2
    Yes, you need a clean working tree. But it can be easily done by using the stash. Save to stash, do the rebase, then stash pop – eftshift0 Oct 29 '20 at 15:19
  • Some of the answers to [this question](https://stackoverflow.com/q/4913360/9210961) explain why a rebase modifies the working directory (and thus why, indeed, you do need a clean working directory before a rebase). – prosoitos Oct 29 '20 at 15:42
  • 1
    @eftshift0: I don't like using stash (or autostash) when rebasing because that potentially leaves you with a stash. If it doesn't apply properly, you have all the problems of a stash. If you make a regular commit, and it doesn't apply properly, you have all the tools for working with commits. I think it might be nice if the rebase autostash code made a commit instead of a stash (although that would make it mis-named!). – torek Oct 29 '20 at 21:37
  • I beg to differ, but that's fine. – eftshift0 Oct 29 '20 at 22:19

2 Answers2

2

Forget about the clean staff, you just need a git pull --rebase --autostash. which will stash your current work automatically to get a clean working directory (based on your last commit) , then rebase your current branch from remote, than pop the stashed content.

This command works out of box, no matter working dir is clean or not.

If you run into some conflicts, just remember this command is equivalent to

git stash
git pull --rebase
git stash pop

And solve it like you normally do.

Danielhu
  • 499
  • 4
  • 13
0

Yes, you should have a clean working directory before you rebase (or switch branches if you intend to checkout master). The simplest way to achieve this is to just commit whatever you have right now. Note it doesn't matter if you aren't ready to commit because it isn't done. Just set your commit message to something to remind you it isn't finished. For example, I use "wip - some quick note here". And since I always start my commits with a capital letter for actual commit titles, I use a lowercase starting letter to indicate to me that I need to rebase/amend/reword the commit before I create my pull request. If for some reason you are dead set against committing unfinished work (I can't think of any good reason for this), then you could stash your changes instead to get a clean directory.

Tip: delete your local master branch, as you (probably) never will need it. If you want to rebase onto master, you don't need to do what you implied you were going to do, which is: checkout master, pull the changes, then checkout your branch, and then rebase onto master. Instead, just git fetch, and then rebase onto origin/master. It's much faster and achieves the identical result. Anytime you want to create a new branch off of master, git fetch, then create your branch off of origin/master.

TTT
  • 22,611
  • 8
  • 63
  • 69