Git Cheatsheet
Setup and Configuration
List user name, user email and ssh command (Do not use --global option for repo based):
git config --global user.name
git config --global user.email
git config --global core.sshCommand
Set user name, user email and ssh command (Do not use --global option for repo based):
git config --global user.name "yourusername"
git config --global user.email "youremail@mail.com"
git config --global core.sshCommand "ssh -i ~/.ssh/id_ed25519 -F /dev/null"
Unset user name, user email and ssh command (Do not use --global option for repo based):
git config --global --unset user.name
git config --global --unset user.email
git config --global --unset core.sshCommand
Manage Personal Access Token info: GitHub PAT Docs
Repository Initialization
Initialize Git repository:
git init
Clone repository:
- Via https:
git clone https://github.com/yourusername/yourrepo.git your-new-directory
- Via ssh:
git clone git@github.com:yourusername/yourrepo.git your-new-directory
Staging and Committing
Check the status of changes:
git status
Stage a file:
git add README.md
Stage multiple files:
git add README.md index.html
Stage all changes (excluding ignored):
git add *
or
git add .
Unstage a file:
git rm --cached .vscode
Unstage multiple files:
git rm --cached -r .vscode/ bin/
Unstage all files:
git rm -r --cached .
Commit with the message:
git commit -m "initial commit"
Amend last commit with staged changes:
git commit --amend --all
Change commit message:
git commit -am "updated commit" --amend
Branching and Merging
List branches:
git branch
Create new branch:
git branch feature-restructure
Create the new branch and switch to it:
git checkout -b feature-restructure
Delete branch:
git branch -D feature-restructure
Rename branch:
git branch -m feature-new-name
Switch branch:
git checkout $branch_name
Merge branch into current branch:
git merge feature-restructure
Working with Commits
View commit history:
git log # detailed view
git l # same as above
git log --oneline # concise view
Checkout specific commit:
git checkout $commit_id
Hard reset to commit (WARNING: discards changes):
git reset --hard $commit_id
Soft reset to commit:
git reset --soft HEAD~
Unstage file with restore:
git restore --staged index.html
Discard changes in working directory:
git restore index.html
Remote Repositories
Add remote URL:
git remote add origin https://github.com/yourusername/yourrepo.git
Remove remote URL:
git remote remove origin
Change remote URL:
git remote set-url origin https://github.com/yourusername/yourrepo.git
Show remote URL:
git remote show origin
git remote get-url origin
git remote -v
Pushing and Pulling
Push to remote branch:
git push origin master
Push and set upstream:
git push --set-upstream origin main
Push after the upstream set:
git push
Pull with rebase:
git pull --rebase origin master
Set upstream branch for current branch:
git branch --set-upstream-to=origin/master
Show which upstream branch is currently set (It should show [origin/master] for master branch):
git branch -vv
Pull after the upstream set:
git pull
Miscellaneous
- Skip CI workflow by adding
[skip ci]in the commit message
Add [skip ci] anywhere in your git commit message to skip CI workflows.