Common Git Commands & Config
Sharing the git [gɪt] config I’ve accumulated over the years. The content below is based on version 2.34.1; if anything differs, upgrade your git first!
Copyright notice: This is an original article by xwi88, licensed under CC BY-NC 4.0. Commercial use is prohibited; please cite the source when reposting. Follow at https://github.com/xwi88
Basic account config
--globalmeans global config; non-global config must be set inside the specific git project directory
git config --global user.name [your_name]git config --global user.email [your_email]git config --global --listview global git configgit config --listview project git config
Signed-commit setup
configure your Git client to sign commits by default for a local repository, in Git versions 2.0.0 and above
git config --global user.signingkey [KeyID]git config --global commit.gpgsign true
See also: Signing Git commits/tags with GPG for setup.
Credential storage
To avoid typing the password every time, let the machine remember it. MacOS uses Keychain Access to store your password and other credentials.
git config --global credential.helper storepersistently save your password/credentials on this machinegit config --global credential.helper 'cache --timeout=300'cache the password for 300s
More:
If you have MFA enabled, you must use a Personal Token instead of the password.
- github path: Settings Developer settings->Personal access tokens->Generate new token
Personal access tokens 📢
- Visible only when generated — save it carefully
- Mind the scopes and expiration
Credential cleanup
After a password changes or expires, clear the stored one, or you may get
login failed/no permissionerrors.
git config --system --unset credential.helpergit config --global credential.helper 'cache --timeout=5'or set a very short expiry to flush the cached passwordgit config --global credential.helper storere-enable credential storage
Prettier log output
The plain
git logis too bare to inspect details. We can format it, andgit aliasmakes it easy to define our own git log command — do not override git’s built-in commands.
My usual git log config:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset' --abbrev-commit"
git lg -4
| |
To also show gpg signature info:
git config --global alias.lsg "log --color --graph --pretty=format:'%C(cyan)%G?%Creset %Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset' --abbrev-commit"
git lsg -4
| |
For even more detail:
git config --global alias.llsg "log --color --graph --pretty=format:'%C(cyan)%G?%Creset %Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset [%GK trust:%GT] %C(yellow)%GS%Creset' --abbrev-commit"
git llsg -4
| |
- Always pass a log entry count
- Don’t make the format too complex — output may slow down
- If you use
gpg signing, make sure you don’t have too manygpg keys
Basic commands
For more detail on any command, run
git <command> --help
git add [<path_spec>...]stage files into theIndexgit add [<path_spec>...] -ndry-run: show what would happen, without doing it
git status -sshow the current branch status — files differ between working tree and index:modified,not staged,staged but not committedgit diffgit diff [<path>...]diff working tree vs indexgit diff <commit>...<commit> [<path>...]diff between commitsgit diff --cached [<commit>] [<path>...]diff index vs repository
git commitcommit staged changes to the local repogit commit -m <message>commit to local repogit commit -S -m <message>sign and commit; requires your GPG keygit commit -a -mstage and commit all changes, skippinggit add; generally avoid thisgit commit --amendredo the latest commit — to edit its message or append changes; don’t chain this, and avoid it for already-pushed commits
git resetundo changes; see checkout vs resetgit rmremove files from the index and working treegit mv <old> <new>file rename; equivalent tomv old new,git addthe new file andgit rmthe old
Branch operations
Remote branch config
git remote -vgit remote add <name> <url>add a remotegit remote rename <old> <new>rename a remotegit remote set-url <name> <new_url>change a remote URL
Cloning
git clone -b branch_name --single-branch <url>single-branch clonegit clone <url> [dir]
Fetch & sync
git fetch --all||git mergeonly fetch into the local remote-tracking branch (e.g.origin/main,upstream/main); merge into the local branch later when neededgit pullfetch and merge the remote branch into the current branch; generally avoid unless you understand the consequencesgit push -u <repo_name> <branch_name>set up tracking between a new local branch and a remote branch — typically for a new branch’s first push
checkout vs reset
git checkoutgit resetgit revertrarely used on the CLI
| Git Command | Scope | Common use cases |
|---|---|---|
| reset | Commit | Discard commits in a private branch or throw away uncommited changes |
| reset | File | Unstage a file |
| checkout | Commit | Switch between branches or inspect old snapshots |
| checkout | File | Discard changes in the working directory |
| revert | Commit | Undo commits in a public branch |
| revert | File | (N/A) |
--softThe staged snapshot and working directory are not altered in any way.
--mixedThe staged snapshot is updated to match the specified commit, but the working directory is not affected. This is the default option.
--hardThe staged snapshot and the working directory are both updated to match the specified commit.
Merging & committing
For multi-person or large-team collaboration, master and use
git rebase, and keep each branch’s commit count vs the base branch minimal — easier rebases and conflict resolution for everyone.If you’ve opened a
PR/MR, before reviewers review, make sure your branch isFast-Forwardvs the base; do not Rebase from the web UI.
git rebaseuse with care demogit mergenot recommended
Branch push & delete
git pushpush the current branch; requires tracking a remote branch; for the first push usegit push -u <name> <branch>git push -fforce-push the current branch to its remotegit push -f <name> :<remote_branch>push an empty branch to the remote — i.e. delete the remote branchgit branch -ddelete a local branch
Unset config
Remove the line matching the key from config file.
git config --global --unset [key]remove from global configgit config --unset [key]remove from project config
Remove a mistakenly-created entry from global config
git config --global --unset alias.llg
Edit config files
git config --global -eedit the global config file at~/.gitconfiggit config -eedit the project config file at.git/config