Tutorial: GIT and GitHubCreating & switching Branches - 2020
Here is the initial file structure for heads under .git directory:
Currently, the HEAD is at master branch which is the default branch (.git/refs/heads/master). Actually, the heads directory contains all the heads and we see our master branch is right there.
Let's create a new branch called 'car':
k@laptop:~/GitDemo$ git branch car k@laptop:~/GitDemo$ git branch car * master
Now we have two branches and the (*) indicates the HEAD is pointing to the master branch. If we look at the directory structure again:
Now we have two heads for our branches ('car' and 'master').
Switching to another branch is simple: use git checkout branch_name:
k@laptop:~/GitDemo$ git branch car * master k@laptop:~/GitDemo$ git checkout car Switched to branch 'car' k@laptop:~/GitDemo$ git branch * car master
Now we are on 'car' branch.
But each of our two branches points to the same thing:
k@laptop:~/GitDemo$ git branch * car master k@laptop:~/GitDemo$ ls Appendix Book1 Introduction k@laptop:~/GitDemo$ git checkout master Switched to branch 'master' k@laptop:~/GitDemo$ git branch car * master k@laptop:~/GitDemo$ ls Appendix Book1 Introduction
Let's modify the 'Book1' in t he'car' branch.
So, the first line of the 'Book1' looks like this:
This is in 'car' branch.
Then, we do commit:
k@laptop:~/GitDemo$ git status On branch car Changes not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Book1 no changes added to commit (use "git add" and/or "git commit -a") k@laptop:~/GitDemo$ k@laptop:~/GitDemo$ git add Book1 k@laptop:~/GitDemo$ git commit -m "added a line at the beginning" [car a7f55e3] added a line at the beginning 1 file changed, 2 insertions(+)
Then, check the log:
k@laptop:~/GitDemo$ git log --oneline a7f55e3 added a line at the beginning 0c97f22 removed three updates 42dacd8 name changed ... 28a0a9e deleted OldBook 09a5016 Adding OldBook b440952 initial commit
Note that this log for 'car' branch not for 'master' branch. To see it, we can switch back to 'master' and check the log:
k@laptop:~/GitDemo$ git checkout master Switched to branch 'master' k@laptop:~/GitDemo$ git branch car * master k@laptop:~/GitDemo$ git log --oneline 0c97f22 removed three updates 42dacd8 name changed ... 28a0a9e deleted OldBook 09a5016 Adding OldBook b440952 initial commit
As we can see the commit we've made is not in this 'master' branch.
Now we want to create a new branch and switch to the new branch at the same time with one git command. We can use -b flag:
k@laptop:~/GitDemo$ git branch car * master k@laptop:~/GitDemo$ git checkout -b boat Switched to a new branch 'boat' k@laptop:~/GitDemo$ git branch * boat car master k@laptop:~/GitDemo$
Another useful option for git log to see where the HEAD is and what's the relationships among branches:
Git/GitHub Tutorial
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization