Home > OS >  Git: Is there a way to git stash but be able to specify a branch name?
Git: Is there a way to git stash but be able to specify a branch name?

Time:06-21

I'm working along with some changes and I'm not sure about them. They may be the right way to proceed, but maybe I'm going down the wrong path. I want to save my work just in case I am doing the right thing but not continue with this line of development right now and return to where I was a half hour ago for example. Basically, do a stash but make the stash commit a real branch with a descriptive name.

something like

git stash save-stuff  // not a valid command

what I've been doing requires multiple commands

git commit -a -m 'not sure this is where I want to go'
git checkout -b save-stuff
git checkout original-branch // return to original branch
git reset --hard HEAD~1 // I don't always do this one.

This causes me displeasure.

CodePudding user response:

Git history is always hierarchical, and that applies to git stash as well. git stash creates a new commit, relative to a specific commit, rather than a branch. As such, there is no need to save a Git stash with a specific name; in doing so, you would literally be creating a branch.

If you want to be able save a temporary piece of work that you are not confident is correct, the best thing to do would be to make use of a branch. Doing so will allow you, and anyone else working on the repository (should you push the branch to the origin), to be able to refer back to this point in time easily. You can make subsequent changes to this branch if your approach is found to be correct, and even squash the changes to make it seem like the questionable commit never existed.

Keep in mind that there is nothing stopping you from checking out an individual commit -- even on a branch that you are currently working on. This will allow you to revert back should you need to, and you can continue working from this old commit, even pushing to the same branch if desired.

CodePudding user response:

Two steps :

  • git stash has a subcommand git stash create, which basically creates the same commit as git stash but doesn't update the stash and does not touch your index or worktree
  • git update-ref allows you to "update" a ref (for example : branch) without it being the current active branch

You can combine the two, either in a script :

# file git-snap:

#!/bin/bash

if git diff -s --exit-code HEAD; then
    echo " --- worktree is clean, nothing to save"
    exit 0
fi

# get name of active branch, to update a branch named wip/<branch> :
branch=$(git rev-parse --abbrev-ref HEAD)
ref="refs/heads/wip/$branch"

# call git stash create:
sha=$(git stash create)
# update a branch named 'wip/<branch>' :
git update-ref -m "save wip on $branch" "$ref" "$sha"

git log --oneline -1 "$ref"

(if you place the above git-snap script on your PATH, you can then simply invoke git snap)

or if you are into one liners :

git update-ref refs/heads/mystash $(git stash create)
  • Related