As others have mentioned, I highly recommend getting comfortable with branches. My workflow is typically:
Starting form the master* branch:
git checkout -b awesome-new-killer-feature
create a new branch (-b) and have it checked out.
write some code ...
git add .
, git status
, git commit
commit small changes, repeat step 2
OH no! My friend just reported a serious bug! He lost data!!!!!
git checkout master
go back to the master branch
git checkout -b bugfix-serious-data-loss
create new branch for hotfix
fix bugs, git add
, git status
, git commit
, rinse, repeat until bug is fixed
git checkout master
go back to master branch
git merge --no-ff bugfix-serious-data-loss
merge bug fix back onto master
OK, now I can get back to working on my awesome-new-killer-feature:
git checkout awesome-new-killer-feature
resume working on what I was working on
git rebase master
merge back changes to master onto working code so we get the benefit of the bugfix. Not to mention this reduces the probability of merge conflicts later on when we need to merge this branch back to master
write code, git add
, git status
, git commit
, rinse, repeat until feature is complete
git checkout master
, git merge --no-ff awesome-new-killer-feature
merge the branch back onto master
Now sit back and type gitk
to see a nice historical view of what you've been doing.
Optional:
git branch -D bugfix-serious-data-loss awesome-new-killer-feature
delete unused branches. I like to keep my repo clean
The power of git comes not from being able to checkpoint your work. It comes from how fast and cheap branching & merging is. Branching allows you to work on multiple ideas at the same time and/or experiment with throw-away ideas all without affecting your stable code. If the idea doesn't work simply delete the branch, if it works merge it back onto master.
*note: By convention most git users call their main/trunk branch "master".