Is git stash branch-specific or for the whole repository?

后端 未结 4 1925
小鲜肉
小鲜肉 2020-12-12 18:32

I went into a branch and did some work. I wanted to go into another branch but didn\'t want to commit so I did git stash. Then I did git checkout

4条回答
  •  春和景丽
    2020-12-12 19:09

    git stash is not per-branch.

    • Instead of git stash (which can be lost easily when you have lots of stashes and branches)
    • I suggest doing a git commit to save the unfinished code in your branch and when you are ready to finish the code do a git reset ${COMMIT_HASH_VALUE} to get the unfinished code back
    • git commit and git reset when used together correctly can simulate a git stash for a specific branch

    Here is a common real-life scenario that demonstrates the value and the usage the commit and reset commands:

    • you are working on feature branch X and your code doesn't even compile or pass the tests
    • there is a bug that is higher priority than the current new feature and so you must start work immediately on the bug fix
    • rather than do a git stash (and the stash gets lost in the mix because you have many stashes and many branches)
    • you can do a git commit on feature branch X
      • write down the COMMIT_HASH_VALUE for later
    • checkout a new branch Y for the hot fix
    • finish the hot fix on branch Y (do a merge request to get the hot fix into the baseline and delete the hot fix branch)
    • then checkout the feature branch X again
    • to pop your unfinished work that didn't compile or pass testing --> just do a git reset ${COMMIT_HASH_VALUE}

    (FYI the default for git reset is --mixed)

提交回复
热议问题