Git and GitHub for Beginners II(Tutorial)

PJ Wang
CS Note
Published in
4 min readNov 9, 2017

This lecture will teach you how to create a new branch, and how to merge branch. But you need to know the basic command previously, you could learn from lecture 1: Git and GitHub for Beginners I(Tutorial).

Previously, we add and commit files on master branch. Furthermore, we could develop new function or debug on other branch.

Step 1: Create a new branch

Look over your present branch.

gitDemo user$ git branch

Create a new branch named “Daniel”

gitDemo user$ git branch Daniel

Open the Sourcetree

Change the branch

gitDemo user$ git checkout Daniel

How to delete branch?

gitDemo user$ git branch -D Daniel

Step 2: Add/modify the file on the other branch

2–1: Add the file on Daniel branch

gitDemo user$ echo 123 > D_branch.txt
gitDemo user$ git add D_branch.txt
gitDemo user$ git commit -m "Add D_branch.txt"

2–2: Or you could modify the “testFile.txt” committed previously.

gitDemo user$ vim testFile.txt
gitDemo user$ git add testFile.txt
gitDemo user$ git commit -m "Daniel modify testFile.txt"

After step 2–1, and checkout back to master branch. The file added by Daniel branch isn’t exist.

In the end of this step, we add and commit the file(testFile2.txt) on master branch.

Step 3: Rebase

Currently, we snap our Sourcetree:

gitDemo user$ git rebase -i 6e0c65f

3-1. Revise committed message

Next, pop the following window:

Save and display on the sourcetree.

3–2. Edit committed

Next, the HEAD stopped at 60bfea05

Solve conflict error

If you modified the file which depended on a94494d, it will pop “Conflict” message. For example, we modify the file “D_branch.txt” and commit it.

After we committed it, we could continue the rebase process:

gitDemo user$ git rebase --continue 

Pop the error, because the file is dependent. Solved this problem, we could open the file and correct it.

Keep the words you want.

Next, we type commandsgit add <file>and git rebase --continue

3–3. Rebase onto

Currently, we snap our Sourcetree:

The tree branches on 3155c68 , and we use rebase --onto to change it.

gitDemo user$ git rebase --onto 6ba67c7 3155c68

3–4: Rebase other branch

Except merge, we could use rebase to combine the branch.

gitDemo user$ git branch
* Daniel
master
gitDemo user$ git rebase master

How to reset branch before rebase?

gitDemo user$ git reset --hard ORIG_HEAD

Step 4: Merge

Finally, we could merge two branch master and Daniel

gitDemo user$ git checkout master
gitDemo user$ git merge Daniel

Or you could merge it by no fast-forward

gitDemo user$ git merge Daniel --no-ff

How to use basic command?

Git and GitHub for Beginners I(Tutorial)

How to use GitHub?

Git and GitHub for Beginners III(Tutorial)

--

--

PJ Wang
CS Note

台大資工所碩畢 / 設計思考教練 / 系統思考顧問 / 資料科學家 / 新創 / 科技 + 商業 + 使用者