Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: alternative for stash

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?

like image 819
Mhd Avatar asked Apr 07 '17 14:04

Mhd


People also ask

Is git stash necessary?

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Is stash and git same?

The git commit and git stash commands are similar in that both take a snapshot of modified files in the git working tree and store that snapshot for future reference. The key differences between the two are as follows: A commit is part of the public git history; a stash is stored locally.

What is opposite of git stash?

You can just run: git stash pop. and it will unstash your changes.


1 Answers

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

like image 127
CodeWizard Avatar answered Oct 03 '22 21:10

CodeWizard