29

I am working on one branch, and suddenly I have to switch to another branch to fix an urgent bug. The problem is the changes I make to the current branch is still a mess and I do not want to commit that and leave some scrappy commit message.

Is there any way I can save the current changes without commit?

Justin Li
  • 1,035
  • 2
  • 12
  • 19
  • 1
    [stash it](https://git-scm.com/docs/git-stash) – litelite Jul 27 '17 at 13:29
  • Does this answer your question? [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/questions/22053757/checkout-another-branch-when-there-are-uncommitted-changes-on-the-current-branch) – Hritik Jul 03 '21 at 18:58

3 Answers3

37

Yes, you can use stash.

git stash -m "optional description here"

It will save any uncommitted stuff in a special area where you can get it back later using

git stash apply

You can see what is in the stash with

git stash list
dan1st
  • 12,568
  • 8
  • 34
  • 67
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
14

Stash obviously works. I just want to propose that it is also a viable, if not better, option, to just commit whatever you have got, for the following reasons:

  1. Stash is kept separately from the current branch, the index, and the working space, and less visible. Unless you are going to fix the other issue and switch back real quick, it's actually easier to get confused with the state of the stash after a while.
  2. Committing your changes in your environment does not affect anything or anybody else, as long as you don't push the commit. You can always reorganize your commits later when you are done with the feature. Compared to stash, a commit on the feature branch where it belongs is easier to manage later.
jingx
  • 3,698
  • 3
  • 24
  • 40
  • You can setup your `git log` command to show stashes as well. For me they show up in a similar way to commits (as a node in the graph, and having an ancestor commit as a parent). Regarding your second point, that depends on your workflow, some people prefer to only ever commit small commits with very descriptive messages, others (like myself) prefer to work however we feel is most efficient at the time, and squash the commits to "fake" the history to make it easy for others to digest. For people that follow the first method, it can be easy to make a messy commit they will then forget about. – instantepiphany Jul 27 '17 at 15:20
  • ... Which is something stash helps avoid. I think different people use different features of git, and its great that it supports a lot of workflows. – instantepiphany Jul 27 '17 at 15:21
  • 1
    @instantepiphany no argument there. It really comes down to personal styles and circumstances. – jingx Jul 27 '17 at 18:17
  • I too am not a fan of `git stash`. I prefer to save "less official" git commits with a "mid-edit save" commit, like this: https://gist.githubusercontent.com/johnnyutahh/e8cfb515fb65635dca520117477456b8/raw/ – Johnny Utahh Apr 17 '22 at 23:07
10

This is a neat little bash function I set up inspired by @jingx's answer:

gcl() { 
    echo "### Adding any wip files";
    git add .; 
    echo "### Committing files with a temporary commit";
    git commit --no-verify -am 'local commit - work in progress';
    echo "### Checking out branch";
    git checkout "$1";
    x="$(git log -1 --pretty=%B)"
    if [ "$x" = "local commit - work in progress" ]; then
        echo "### Undoing last commit";
        git reset --soft HEAD^
    else 
        echo "### Not undoing last commit"
    fi
    echo "gcl complete"
}

Add it to your .bashrc or .profile and then use gcl my-branch to switch branches and save your work as a local commit and simultaneously unpack your local commits on change.


For example, say you're on branch1 and have uncommitted changes. Just do a

gcl urgent-fix

do whatever you need to do and then hop back

gcl branch1

Your uncommitted changes will be there just like you left them.

Although a commit does take place, it isn't pushed remotely and so can be undone locally without consequence.

tnrich
  • 8,006
  • 8
  • 38
  • 59