-
Notifications
You must be signed in to change notification settings - Fork 1
Switch branches
Now that we have three branches, we want to work in that second branch.
- Now if we want to switch to this new branch we have to checkout that branch. As you will see
git checkoutworks the indifferently for a commit or a branch. This is normal since branches point at their head commit.
ben@GregoryHouse:~/tmp/testRepo ((4a9deba...))$ git checkout experimental
Switched to branch 'experimental'
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git status
# On branch experimental
nothing to commit (working directory clean)
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git branch -v
alternateHistory 4a9deba First commit: add README
* experimental c6c5566 Add .gitignore, add a line to README
master c6c5566 Add .gitignore, add a line to READMEgit status tells us we are in our experimental, and git branch confirms it by putting a star in front of our current branch. Now all commit will be done in that branch only until we switch to an other.
We could also have squashed the two operations branch and checkout in one single command. The famous git checkout -b name.
In fact git checkout -b name is equivalent to doing git branch my_name; git checkout my_name.
That said the power of checkout is to checkout a specific commit, so we can perfectly do git checkout -b alternateHistory 4a9deba. That was the shortcut for our previous git checkout 4a9deba; git branch alternateHistory; git checkout alternateHistory. How much more convenient is that?
You may commit a few things in the experimental branch:
ben@GregoryHouse:~/tmp/testRepo (experimental)$ gvim README
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git commit -a
[experimental 16ff837] added line in experimental branch
1 file changed, 2 insertions(+)
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git diff master
diff --git a/README b/README
index 35e406b..cbd27c7 100644
--- a/README
+++ b/README
@@ -6,3 +6,5 @@ Short git tutorial repository.
Let's learn a bit of git.
We can add some lines!
+
+Line added in experimental branch.
git diff gives you differences between any 2 commits. And again yes, branches are translated into their head commits in this case.
Jump to Distribute the code