Git – Interrupted Workflow, What Will You Do?

When you’re working on something intensively and you haven’t finished the thing, suddenly somebody just wants you to do something again which is more important. Normally, what you will do is to create a new branch and commit your changes to the new branch and switch back to the master branch and do the other thing and reset the working directory.

or something like this. I’ve taken this example from http://www.kernel.org/pub/software/scm/git/docs/git-stash.html

1
2
3
4
5
6
7
8
9
# ... hack hack hack ...
git checkout -b my_wip
git commit -a -m "WIP"
git checkout master
edit emergency fix
git commit -a -m "Fix in a hurry"
git checkout my_wip
git reset --soft HEAD^
# ... continue hacking ...

But there is other way to do this by using git stash. Or something like this

1
2
3
4
5
6
# ... hack hack hack ...
git stash
edit emergency fix
git commit -a -m "Fix in a hurry"
git stash pop
# ... continue hacking ...

Well, I just wanted to give an example of git stash so you can get more understanding of what is happening in git stash

first you need a clean directory to start git

1
mkdir gitstash && cd gitstash && git init

Now you create a new file to work on

1
touch main

And you edit the file main like you’re working with something

1
I'm working on something important.

And you use git add to track the file

1
git add .

And you commit your change

1
git commit -m "Initial Revision"

You will see something like this

1
2
3
[master (root-commit) 95616b9] Initial Revision
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 main

Then you keep working of the file main, so the file main would look like this

1
2
3
I'm working on something important.

Something which is also important.

Then somebody just interrupted you and asked you to fix something which is might impact the work that you’re working on right now. So, what you need to do is to use git stash to clean your working directory.

1
git stash

You would see something like this to confirm that you have saved the working directory

1
2
Saved working directory and index state WIP on master: 95616b9 Initial Revision
HEAD is now at 95616b9 Initial Revision

Now you’re main file would look something like this. It’s like the file main is rolled back to the HEAD commit

1
I'm working on something important.

Now you edit the file main to be something like this.

1
I'm working on something important. Something which is very very important.

And you commit this change

1
git commit -am "Fixed something"

Now you can go back and keep working on what you’ve left off by using this command

1
git stash pop

You will see something like this

1
2
Auto-merging main
CONFLICT (content): Merge conflict in main

Git will try and merge the change for you, if Git cannot merge the file for you, you have to merge it by yourself manually. In this case, the main file has to be merge manually. I just use vimdiff to merge.

Now you’re main file will look something like this

1
2
3
I'm working on something important. Something which is very very important.

Something which is also important.
Mar 30th, 2011

Comments