OverviewTeaching: 5 min
Exercises: 10 minQuestions
- How can Git help us to deal with interrupted work and context switching?
- Learn to switch context or abort work without panicking.
We all wish that we could write beautiful perfect code. But the real world is much more chaotic:
Git provides lots of ways to switch tasks without ruining everything.
The stash is the first and easiest place to temporarily “stash” things.
git stashwill put working directory and staging area changes away. Your code will be same as last commit.
git stash popwill return to the state you were before. Can give it a list.
git stash listwill list the current stashes.
git stash save NAMEis like the first, but will give it a name. Useful if it might last a while.
git stash save [-p] [filename]will stash certain files files and/or by patches.
git stash dropwill drop the most recent stash (or whichever stash you give).
git stash list. Can you pop the stashes in the opposite order?
git graphshow when you have something stashed?
You can use branches almost like you have already been doing if you need to save some work. You need to do something else for a bit? Sounds like a good time to make a feature branch.
You basically know how to do this:
$ git checkout -b temporary # create a branch and switch to it $ git add <paths> # stage changes $ git commit # commit them $ git checkout master # back to master # do your work... $ git checkout temporary # continue where you left off
Later you can merge it to master or rebase it on top of master and resume work.
You already know how to do this…
It happens often that you do something and don’t need it, but you don’t want to lose it right away. You can use either of the above strategies to stash/branch it away: using branches is probably better. Note that if you try to use a branch after a long time, conflicts might get really bad but at least you have the data still.
There is almost never reason to clone a fresh copy to complete a task that you have in mind.