Tutorial: GIT and GitHub 7. Branching & Merging - 2020
This section is a quick review of what we've done so far.
We have "README" in our GitProject directory:
Then we need to issue git init command:
As we can see the git init command creates an empty Git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial HEAD file that references the HEAD of the master
branch is also created.
We may also want to do email and name configuration using git config command:
Now we want to put our "README" to staging area and then do commit (take snapshot).
Up to this point, we've been working on local repo, but now it's time to use remote repo which is GitHub. To use remote repo, we need to tell git what remote repo we're going to use via git remote add command:
Since we can use remote repo name as origin, let upload our README to our GitHub:
We can check whether the README file is really in GitHub:
git checkout -b <new_branch> will create a new branch (the "-b" causes a new branch to be created) with the name of "new_branch":
We can check which branch we're now by using git branch:
As expected, we're on the new branch, 'fix1'. Now, we want to modify the README like this:
README first README fix1
To add the change and commit:
$ git commit -a -m "Added branch fix1" 1 file changed, 1 insertion(+), 1 deletion(-)
Note that we committed the change only to the branch not to the master
.
Now we can go back (switch) to the master
:
We can push our branch to GitHub:
If we check our GitHub, it now has two branches and the newly added fix1 branch shows our change:
To see which branch we're now on:
admin@KHONG /c/MyGit/GitProject (master) $ git branch fix1 * master admin@KHONG /c/MyGit/GitProject (master)
To see the merged branch:
admin@KHONG /c/MyGit/GitProject (master) $ git branch --merged * master merged admin@KHONG /c/MyGit/GitProject (master)
To list unmerged branch:
admin@KHONG /c/MyGit/GitProject (master) $ git branch --no-merged fix1 admin@KHONG /c/MyGit/GitProject (master)
To see all the branches and the last commit:
admin@KHONG /c/MyGit/GitProject (master) $ git branch -v fix1 77bb1bc Added branch fix1 * master 4aece6d initial commit merged 4aece6d initial commit admin@KHONG /c/MyGit/GitProject (master)
We're now on *master
, and check the README:
README first README
To merge with fix1
:
Now the README file has been merged on master
:
README first README fix1
Let's check our branches again:
admin@KHONG /c/MyGit/GitProject (master) $ git branch fix1 * master merged
The fix1
branch is there but we do not need it any more since it's been merged. So, let's remove it:
admin@KHONG /c/MyGit/GitProject (master) $ git branch -d fix1 Deleted branch fix1 (was 77bb1bc). admin@KHONG /c/MyGit/GitProject (master) $ git branch * master merged admin@KHONG /c/MyGit/GitProject (master)
If we want to delete unmerged branch, we can use "git branch -D name_of_unmerged_branch".
How we can delete a branch on our GitHub?
admin@KHONG /c/MyGit/GitProject (master) $ git push origin :fix1 To git@github.com:bogotobogo/branch-demo.git - [deleted] fix1 admin@KHONG /c/MyGit/GitProject (master)
Let's check how our GitHub looks like:
Our GitHub now has only one branch which is master
since another branch fix1
has been deleted.
Now, we need to push our merge local repo to remote repo:
admin@KHONG /c/MyGit/GitProject (master) $ git push origin master Counting objects: 5, done. Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:bogotobogo/branch-demo.git 4aece6d..77bb1bc master -> master admin@KHONG /c/MyGit/GitProject (master)
Git/GitHub Tutorial
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization