2025-06-12

git command reference

commands to be executed in the repository directory or any of its subdirectories

1

clone and remotes

copy a repository
  git clone http://gitserver/myrepository
get all new changes from a remote
  git fetch
get and merge all new changes from the remote of the current branch
  git pull
pull from another branch
  git pull mybranch
list registered remote repositories
  git remote
add a new remote
  git remote add myremotename http://myrepository
change/edit remote url
  git remote set-url myurl
fetch force pushed branch
  git fetch && git reset origin/master --hard
push to another remote
  git push myremote
sync your local tags with the remote
  git push --tags

working directory

list current changes
  git status
add changes to the transaction
  git add myfile ...
  git add --all

commits and history

create a commit
  git commit
show history
  git log

branches

list local branches
  git branch
list remote branches
  git branch -r
create a new branch
  git checkout -b mybranch
checkout an existing branch
  git checkout mybranch
checkout an old state
  git checkout mycommit
reset only one specific file to the last commit
  git checkout myfile
checkout the previously used branch
  git checkout -
checkout one file to specific commit
  git checkout mybranch -- path

merges

merge another branch into the current branch
  git merge mybranch

diffs

show file content differences
  git diff
show differences for a specific myfile
  git diff -- myfile
show differences for a specific file at specific branch or commit
  git diff mybranch -- file
show differences to a specific commit
  git diff mycommit

reset

remove uncommitted changes only from the index
  git reset
delete uncommitted changes completely and permanently
  git reset --hard

tags

tag the current commit
  git tag add mytag

rebase

change/rewrite/edit commit messages
  git rebase -i HEAD~3

2

submodules

add a submodule
  git submodule add http://gitserver/repositoryname
download submodule contents
  git update
rename or move a submodule
  git mv a b
remove submodule
  git rm

commits and resets

merge a single commit from another branch
  git cherry-pick commit-id
reset to remote state
  git reset --hard origin/mybranch
delete last commit and sync with remote
  git reset HEAD^ --hard
  git push origin -f
uncommit the most recent commit
  git reset --soft HEAD^
create a new commit that reverts to the state at an old commit
  git revert

archive and export

copy all files to a tar archive without the .git directory and without files that match .gitignore
  git archive mybranch mydestination.tar
export zip without .git directory
  git archive main --prefix directory-name --output /tmp/name.zip
extract only the versioned files into an archive file
  git archive --output /tmp/a.bundle
transfer commits via archive file
  git bundle create /tmp/a.bundle --all
  git bundle pull a.bundle
  https://git-scm.com/docs/git-bundle

remote cleanup

delete a remote tag
  git push origin --delete mytag
delete a remote branch
  git push origin --delete mybranch
delete local branches that do not exist on remote
  git remote prune origin
remove branches without remote counterpart
  git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done

clean working directory

remove untracked and ignored files
  git clean -fX

repository management

create a new database-only git repository
  git init --bare
checkout from directories
  git init --bare
  git pull /home/my-checked-out-repository
create a new empty branch without any history
  git checkout --orphan a
rename local branch
  git branch -m myoldname mynewname
move tag
  git tag -f mynewtagname

configuration and tooling

disable ssl certificate verification
  git -c http.sslVerify=false clone https://example.com/path/to/git
  git config --global http.sslVerify false
use meld to display directory diff
  git difftool -t meld --dir-diff develop
show current hooks directory
  git rev-parse --git-path hoos
ignore mode changes
  git config core.fileMode false

reference identifiers

reference identifiers
  HEAD: current commit checked out in your working directory
  HEAD@{}: refers to the state of HEAD n changes ago
  HEAD^: refers to the parent of the current commit
  HEAD^^: equivalent to HEAD~2
  HEAD~n: refers to the nth ancestor of the current commit
  branchname: refers to the latest commit on the specified branch
  commithash: refers to a specific commit identified by its sha-1 hash
  tagname: refers to the commit that a specific tag points to

search

search for a commit where a certain string changed
  git log -S"exact string" --all --patch
search through all diffs
  git grep 'attr1' $(git rev-list --all) -- filename