Skip to content
Author: Tianle Yuan

Git Basic Commands⚓︎


Initialize Git repo⚓︎

Initialize the local repository by using: git init

picture 5

picture 6

Check status⚓︎

Create a file:

picture 7

Check the status of the local repo by using: git status

picture 8

Add changes⚓︎

Add changes into the staging area by using: git add . or git add *

picture 9

Recheck the status:

picture 10

Commit to the local repo⚓︎

Commit the new snapshot to the local repo: git commit -m "<mesage>"

picture 11

Recheck the status:

picture 12

Rename branch name⚓︎

Rename the main branch named from master into main by using: git branch -m master main

picture 13

Recheck the status:

picture 14

Create a branch and switch⚓︎

Create a new branch by using: git branch m_branch_1

picture 15

Then switch to the new branch: git checkout m_branch_1

picture 16

Or you can do one step instead of the above to command lines: git checkout -b m_branch_1

Recheck the status:

picture 17

Merge changes into main⚓︎

Edit the file by: vim

picture 18

Commit the new branch: git commit -a -m 'Added encourage sentence'

picture 19

Switch back to the main branch and merge the new branch into the main branch: git merge m_branch_1

picture 20

Check the commit history⚓︎

Take a look at git log: git log

picture 23

Use graph selection in git log: git log --graph --oneline --decorate --all

picture 22

More details: git log --graph --abbrev-commit --decorate --date=relative --all

picture 21

In Sourcetree:

picture 24

Rebase branch⚓︎

We rebase main branch into m_branch_2: git rebase m_branch_2

picture 25

Check the folder in different branches:

picture 26


Even though we rebase the main branch onto (or on top of) m_branch_2, since we cannot delete the main branch, there is NO DIFFERENCE WITH rebasing m_branch_2 onto the main branch:

picture 27

Squash and merge⚓︎

Look at the commit history below; we want to squash the last three commits on m_branch2 into one:

picture 28

We use this rebase command with interactive selection: git rebase -i HEAD~3; Do the edition as below:

Before After
picture 30 picture 32

Then it will automatically commit. You should add some commit messages:

Before After
picture 33 picture 34

After finishing squash, the terminal will return:

picture 35

The commit history will look like this:

picture 36

Finally, We merge the m_branch_2 into the main:

picture 38

Automatically commit and ask for a commit message:

picture 37

The commit history now looks like the below:

picture 39

Last update: December 4, 2022 05:57:23
Created: November 13, 2022 21:09:05