Git: alternative for stash
Asked Answered
C

2

6

I'm using Git Extensions for my projects. I really like it. There is an issue that keeps bugging me and I'm pretty sure there is a trick in Git Extensions for it. Here is the scenario:

  • From master branch, I created 3 branches A, B and C.
  • Started working in branch A, did some changes
  • Switched to branch B, I still see changes made in A as they are not committed
    • I don't want to commit changes in A because I'm not done yet and I don't want this commit to appear in commit history
    • I don't want to stash changes in A because if I make changes in B and switch to C, I have to stash changes in B too ==> changes in A are gone: overridden by the new stash.

Can I make many stashes in different branches?
If not, whats the alternative for stash?
Is commit and revert commit my only option here?

Celestine answered 7/4, 2017 at 14:59 Comment(0)
G
10

git worktree

Git worktree was introduced in 2007 under the contrib folder in git repo and was called new-workdir.


for example:

git worktree add <second path>

will create another folder on your computer which allow you to work on different branch simultaneously.

git worktree will create 2 separate working folders separated from each other while pointing to the same repository.

This will allow you do to any experimentals on the new worktree without having any effect on the repository itself. In the attached image you can see that there are 2 separate working folder but both of them are using a single repo and share the content.

Here is a sample on how to create new worktree and what is the result of it:

enter image description here


Can I make many stashes in different branches?

Yes you can but you want to avoid it. You can pop up stash to different branches than the one you originally stahed the sources.

If not, whats the alternative of stash?

As explained above - use worktree

Is commit and revert commit, my only option here?

Again: As explained above - use worktree

Gracchus answered 7/4, 2017 at 15:4 Comment(4)
I like the idea of worktree but I don't know how to use it in Git Extensions GUI.Celestine
use it from git bash.Gracchus
I will try. ThanksCelestine
The support for worktrees was added in GitExtensions-2.50 github.com/gitextensions/gitextensions/releases/tag/v2.50RC1Viper
E
2

You can keep track of your stashes and apply them in any order you wish. While on branch A type:

git stash save "stash_a"

You can similarly name stashes from branches B and C. You can list all stashes by typing

git stash list

To apply a certain stash you can use

git stash pop stash@{n}

where n is the index of the stash. Use the names you gave to correlate stashes with their respective indices in the stack.

You mentioned the following:

I don't want to commit changes in A because I'm not done yet and I don't want this commit to appear in commit history.

Actually, there is nothing wrong with making a commit, because you can always amend that commit later on with no consequences, assuming that you either haven't pushed, or if you have pushed, that the branch isn't shared. So an alternative to using git stash on branch A would actually be to just do this:

git commit -m 'WIP'

Then, when you return to branch A and complete the task, commit via this:

git commit --amend

Note that doing git stash actually creates 2 (or sometimes 3) commits under the hood. So both methods I described rely on making commits in some way. The worktree answer given by the @CodeWizard might be an alternative to using commits.

Ebullition answered 7/4, 2017 at 15:8 Comment(3)
type git stash list and than you can view on which branch the commit was made and you can grab any stash you wantGracchus
One more thing that's worth noting here: git stash makes its commits on no branch. This is why it's so easy to use git stash to move uncommitted changes from one branch to another: you save a new stash, then check out the branch, then apply and drop the saved stash. I find, however, that if there are a lot of saved stashes hanging around, I lose track of which one is which.Oedema
"Actually, there is nothing wrong with making a commit," - exactly! Aside from amend, you can also easily commit and then later come back to it and reset to the last commit before it (while leaving the work dir untouched) and just continue working where you left off. This of course works cleanly in terms of commits only as long as you haven't pushed the commit.Beaufert

© 2022 - 2024 — McMap. All rights reserved.