qinghang.dev
Published on

Commonly Used Git Commands

Below are the git commands that I use daily:

Check and commit change

Show branch detail

git status

Get change from remote and merge to current local branch

git pull

Add files to staging

git add file.js

Commit change with message

git commit -m "message"

Commit change to last commit

git commit --amend

Amend last commit without changing its commit message

git commit --amend --no-edit

Amend last commit with new message

git commit --amend -m "new commit mesage"

Ignore ESLint check, and commit change

git commit --no-verify

Revert commit

git revert <commit-hash>

Combine multiple commits into one commit (with squash)

1) git rebase -i HEAD~10
2) change `pick` to `s` to select commits that want to squash
3) leave one main commit unselect

Git branch

List local branches

git branch

List remote branches

git branch -r

Create a new branch

git checkout -b my-branch

Switch to a branch in local repo

git checkout my-branch

To "uncommit", erase the last commit message, and put the modified files back in staging

git reset --soft HEAD~1

Discard change in the last commit

git reset --hard HEAD~1

Delete a branch

git branch -D my-branch

Push local branch to remote

git push origin <my-branch>

Remove remote branch

git push origin :<my-branch>

Cherry pick

Pick the change in a commit and apply to the current branch

git cherry-pick <commit-hash>

Merge

In main branch, and merge custom branch change

git merge <my-branch>

Rebase

In custom branch, and rebase change from main branch

git rebase <main-branch>

Stash

Get a list of stashes

git stash list

Apply a stash to current branch and remove it from the stash list

git stash pop stash@{n}

Apply a stash to current branch and keep it in the stash cache

git stash apply stash@{n}

Git global config

Show git global config

git config --list

Create git logg alias in global config

git config --global alias.logg "log --oneline --decorate --graph

Check change with git diff

See change for a file before staging

git diff file.js

See change between staging and repository

git diff --staged

See change between two commits

git diff HEAD^..HEAD

HEAD^ -> previous commit, HEAD -> current commit

See the difference between your last commit and not yet committed changes

git diff

See the difference between two commits on a file

git diff <commit-hash1> <commit-hash2> file.js