File
Add file to staging area. If a directory is given, all files inside will be added.
git add <file|dir>
Confirm before staging any change. This can be used to stage a file partially.
git add -p
Unstage file - undo the action of staging.
git restore --staged <file>
Rename file, git will stage this action.
git mv <from> <to>
Delete file, git will stage the action of removing this file.
git rm <file>
Git will stop watching this file. File will be kept on disk.
git rm --cached <file>
Start interactive rebase from commit
Commit identifier can be beginning of hash or HEAD~3
.
git rebase -i <commit>
Commit
Commit stage area to repo. Git will open default editor and wait for commit msg.
git commit
Commit with msg
directly.
git commit -m <msg>
Add and commit all changed file.
git commit -a
Commit stage area. Amend last commit instead of creating new commit.
When no file changed, this can be used to edit commit msg.
git commit --amend
Create a revert commit
git revert <commit>
Undo last commit, leaving changed files in working directory
git reset --soft HEAD~1
Undo last commit, remove all changed files. CAUTION: you will lost some work!
git reset --hard HEAD~1
Note: reset
and revert
get tricky on merge commit
Branch
Show local branches.
git branch -v
Show remote branches.
git branch -vv
Switch to branch
.
git checkout <branch>
Create new branch
and switch to it.
git checkout -b <branch>
View
Show status of current working dir and staging area.
git status
Show commit history
git log
Config
View all config of git
git config --list
Edit global git config.
git config -e --global
Edit repo specific git config.
git config -e
Example:
set git user name
git config --global user.name "<name>"
Repo
Create new git repo at current directory
git init
Clone repo from remote
git clone <url>
how to undo git merge