1 \section{Alternate Timelines}
3 I like this time travel metaphor way too much for my own good, and I'm not going to let it go now.
5 Remember when I told you that you can't commit when you're on an old revision because you're in the past and you'd change the future?
7 That's not entirely true.
9 It is true that you can't change the future.
10 Every commit id (that long string of letters and numbers) corresponds to a specific commit message, state of your files, and history.
11 It is impossible to change the message, state, or history behind that commit id.
13 However, you can create an alternate timeline, with new commits that have their own id's, history, and states.
15 This is what we call a "branch".
17 master is the first branch that is created by default in any Git repository.
18 By convention, most repositories have a master branch.
20 At any time, you can start a new branch (and start a new timeline) with the command:
22 git checkout -b nameofnewbranch
25 Git should then tell you something like
27 Switched to a new branch "nameofnewbranch"
30 At this point, any commits you make will go in the new branch, and master will stay exactly where you left it.
32 That means that, if you want, you can try something crazy now.
33 It's OK, if it doesn't work out, you can just go back to master.
34 Even if it does work out, you can still go back to master.
36 Branches are cheap and easy to create, so you shouldn't worry about overusing them.
38 To delete a branch, checkout something else and type:
40 $ git branch -d nameofbranch
43 This is another of those commands that Git may fail to complete, rather than see you lose data.
44 In this case, Git will only let you delete branches that are in the past.
46 If the branch is not within the history of HEAD, that means there may be commits that are only on that branch, and deleting it would mean you lose those commits.
48 If that's ok with you, here's the dangerous version:
50 $ git branch -D nameofbranch
53 If you do delete a branch and lose commits that you, uh, actually wanted, don't worry.
54 The process of losing them happens to be very slow.
55 Unless you tell it otherwise, Git will keep everything you commit for at least 30 days from when you last checked it out, probably longer.
57 To recover a deleted commit, all you need to do is find its commit id, checkout that commit, and create a branch there.
59 For help in finding the commit id, you can use the "git reflog" command.
60 That will show you every commit id you've worked on in at least 30 days, probably longer.
63 Git also has a concept of a "tag" that I should tell you about.
65 A tag is basically a bookmark that points to a specific commit and never, ever changes.
67 To create a tag, checkout a revision and run
72 OK, so you actually can delete a tag and create a new one with the same name somewhere else.
73 That is similar to moving it.
74 But that's rewriting history, and you really shouldn't do that.