In previous article, I explain how to pick up particular commits from other branches. So far, when I do branching operations, I keep working directory and staging area clean by commit all changes. However, in a real situation, I may need to switch branch when I didn’t finish current work. In this article, I will explain what happens when switch branch and how to save current work.
Git Switching Behavior
1. Run ‘git log --oneline --graph --all’ to see current commit history. To avoid too much confusion, let’s reset to commit when only dev branch has additional commits.
2. Run ‘git checkout master’ and ‘git reset --hard 473591c’. Then run ‘git checkout dev’ and ‘git reset --hard 096f270’. Finally run ‘git log --oneline --graph --all’
3. Modify Class1.cs to solution and run ‘git add .’ to stage all changes. Don’t forget to save changes in Visual Studio. No files are in staging area.
4. Modify the Class1.cs file again and save.
5. Run ‘git status’. Now you see same file lists in both staged and not staged state. This is because Git stores the item when you run ‘git add’, but do not track further change automatically.
6. Now, run ‘git checkout master’. Git shows us Class1.cs has modified.
7. Run ‘git status’ in master branch to see how Git brings everything.
Save the change in Git
Sometimes, I don’t want to bring these changes, as they are for dev branch. So what shall I do? The answer is, “Stash ‘em all!”.
1. Run ‘git checkout dev’ to go back to dev branch, and run ‘git stash’. What does stash do? Stash does save all the changes and save it for us. it saves tracked files only by default, so if you want to stash untracked file as well, then you run ‘git stash --include-untracked’. On the other hand, if you don’t want to stash staged are, then run ‘git stash --keep-index’. The option name is a bit confusing but it basically ask git to “keep” the index without stashing it.
2.Run ‘git status’ and I don’t see any pending changes. The changes are gone from actual file, too.
3. How I can see saved stashed? Run ‘git stash list’ will do.
4. Let’s put the change back. Run ‘git stash apply’. It shows you status as well. Now the change is back. but it seems staged info is gone..
5. If you want to restore staged information, run ‘git stash apply --index’. To do so, you first reset to current HEAD.
6. Run ‘git stash list’ again and the saved stash still exists.This is because I use “apply” which applied the stash but don’t delete it.
6. Run ‘git stash drop’ which drops the stash. You can also run ‘git stash pop’ instead of ‘apply’ which apply and drop the stash.
You can save multiple changes into stash. See here for more info.
Save the change in VS
Unfortunately again, I couldn’t find stash capability inside Visual Studio 2017 out-of-box features. You can still doing so via command line anyway.
Create branch from stash
What if I modified files in dev branch before apply or pop the stash? It may or may no conflict. In such case, we can create branch from stash directly.
1. Run ‘git stash’ again, and run ‘git stash branch temp’. This command will create new branch from the stash, and drop it.
2. Run ‘git branch’ and you see you are on temp branch.
3. Then you can do rebasing, merging or cherry-picking from here.
4. Run ‘git checkout master’ and ‘git branch -D temp’. I need to use capital D as temp contains some changes which never merged anywhere.
Summary
Before I know the stash command, I often used commit and commit --amend to save and clean the working directory. I hope Visual Studio supports it soon. In the next article, I will play with GitHub as remote repository.
Ken