Anson LowZF Blog

moon indicating dark mode
sun indicating light mode

Git CLI You Should Know

March 28, 2020 #Git

Git version control system is one of the essential tool & skill we should master to become a better web developer. I document the git CLI we should know for future reference.

In case you want to download Git version control system, Please visit Git official website

There are 3 popular git version control hosting you should know:-

  1. Github
  2. Gitlab
  3. Bitbucket

Bitbucket has a well-structured tutorial to learn git. From beginner to advance. Check out Bitbucket Git Tutorial

Git CLI below are my personal cheatsheet I learn from many different YouTube tutorial, filter and document those I think it’s useful

General

DescriptionGit Command Line
To check git versiongit --version
Update git to latest version in windows 10git update-git-for-windows
List all global configgit config --global --list or -l
Set or reset usernamegit config --global user.name "[name]"
Set or reset email addressgit config --global user.email "[email address]"
Delete usernamegit config --global --unset user.name
Delete email addressgit config --global --unset user.email
Tell git start tracking a projectgit init
Check file status on untracked, or staginggit status

Staging

DescriptionGit Command Line
Add a file to staging areagit add [filename]
Add all files with certain extensiongit add *.html
Add all files to staginggit add . or git add -A
Remove file from staging areagit rm --cached [filename]

Commit

DescriptionGit Command Line
Add all staging files to repository with descriptiongit commit -m "[description]"
Amend last commit descriptiongit commit --amend -m "[descriptioni]"
Commit staging files without descriptiongit commit

Not encourage to just run git commit. You will need to do extra thing to add description after the commit. If you are using Vim, you will enter Vim mode just to add your description. Then you have to press “esc” and type :wq + enter to save and exit. Or :q; + enter to exit without saving

Branch

DescriptionGit Command Line
To create a branchgit branch [branchName]
To list all branchesgit branch
Delete a branch after merginggit branch -d [branchName]
Delete a branch before merginggit branch -D [branchName]
Delete a branch in remote repositorygit push origin -d [branchName]
Check branch loggit branch -vv
Rename branch name in localgit branch -m [newName]

Checkout

DescriptionGit Command Line
Switch to a branchgit checkout [branchName]
Create & switch to a branchgit checkout -b [branchName]
Switch to mastergit checkout master
Create a branch and switch to itgit checkout -b [branchName]
Switch to the version code you wantgit checkout [first6HashIdDigit]
Roll back to last commitgit checkout -- . (dash dash space dot)

Merge

DescriptionGit Command Line
Merge a branch (checkout to master before merge)git merge [branchName]
Merge and only include the latest commit loggit merge --squash [branchName]

Push

DescriptionGit Command Line
Push changes to remote mastergit push origin master
Push to remote master and set as upstreamgit push -u origin master
Push to remote master after set up upstreamgit push

Pull

DescriptionGit Command Line
Pull changes from remote mastergit pull origin master
Pull changes to branchgit pull origin master
Pull from remote master and set as upstreamgit pull -u origin master
Pull from remote master after set up upstreamgit pull
Pull changes from remote to local branchgit pull [remoteNetworkAddress] [branchName]

checkout to branch before pull changes to branch

Clone

DescriptionGit Command Line
Clone from a remote repository to localgit clone [remoteNetworkAddress]
Clone and give it a new namegit clone [remoteNetworkAddress] [newName]

cd to the folder you want to clone before runnind git clone CLI

Fetch

DescriptionGit Command Line
Fetch latest changes to localgit fetch origin

Git Log

DescriptionGit Command Line
Show commit logsgit log
Show last 2 commit logsgit log -2
Show commit log in one linegit log --online
Show last 2 commit log in one linegit log --online -2
Show commit log before the dategit log --before="2020-01-31"
Show commit log after the dategit log --after="2020-01-31"
Show commit log by the authorgit log --author="[authorName]"
Show commit log since yesterdaygit log --since="yesterday"
Show commit log since 2 weeks agogit log --since="2 weeks ago"

Remote

DescriptionGit Command Line
List remote repository associate with your projectgit remote
Add repository to remote hostinggit remote add origin [remoteNetworkAddress]
Add repository to remote hosting 2git remote add origin2 [remoteNetworkAddress]
Remove repository from your projectgit remote remove origin
List remote network address associate with your projectgit remote -v
Set remote network address to your projectgit remote set-url origin [remoteNetworkAddress]
Add a new remote repository to the current repositorygit remote add [remoteName] [remoteNetworkAddress]

.gitignore file

DescriptionGit Command Line
Tell git to ignore some files which no need to be trackedExample: node_module, .cache/, public

A self-taught developer documenting and sharing his learning experience.
React.js • Gatsby.js • Next.js soon. More about me