Git Basic Commands⚓︎
Initialize Git repo⚓︎
Initialize the local repository by using: git init
Check status⚓︎
Create a README.md file:
Check the status of the local repo by using: git status
Add changes⚓︎
Add changes into the staging area by using: git add . or git add *
Recheck the status:
Commit to the local repo⚓︎
Commit the new snapshot to the local repo: git commit -m "<mesage>"
Recheck the status:
Rename branch name⚓︎
Rename the main branch named from master into main by using: git branch -m master main
Recheck the status:
Create a branch and switch⚓︎
Create a new branch by using: git branch m_branch_1
Then switch to the new branch: git checkout m_branch_1
Or you can do one step instead of the above to command lines: git checkout -b m_branch_1
Recheck the status:
Merge changes into main⚓︎
Edit the README.md file by: vim README.md
Commit the new branch: git commit -a -m 'Added encourage sentence'
Switch back to the main branch and merge the new branch into the main branch: git merge m_branch_1
Check the commit history⚓︎
Take a look at git log: git log
Use graph selection in git log: git log --graph --oneline --decorate --all
More details: git log --graph --abbrev-commit --decorate --date=relative --all
In Sourcetree:
Rebase branch⚓︎
We rebase main branch into m_branch_2: git rebase m_branch_2
Check the folder in different branches:
Warning
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:
Squash and merge⚓︎
Look at the commit history below; we want to squash the last three commits on m_branch2 into one:
We use this rebase command with interactive selection: git rebase -i HEAD~3; Do the edition as below:
| Before | After |
|---|---|
![]() |
![]() |
Then it will automatically commit. You should add some commit messages:
| Before | After |
|---|---|
![]() |
![]() |
After finishing squash, the terminal will return:
The commit history will look like this:
Finally, We merge the m_branch_2 into the main:
Automatically commit and ask for a commit message:
The commit history now looks like the below:



