Git Quick Reference
(to be show on version history)
git config --global user.name "<firstname lastname>"e.g.:
git config --global user.name "Andréia Bohner"git config --global user.email "<email>"git commit --amend --author="<firstname lastname> <<email>>" --no-editgit config --global color.ui autogit config core.autocrlfshow current CRLF config
git config --global core.autocrlf <input|true|false>set line-ending style
input: convert crlf to lf on commit but not the other way aroundfalse: turn off crlftrue: converts lf endings into crlf
e.g.:
git config --global core.autocrlf inputgit config --listgit init <repository_name>git clone <repository_url>e.g:
git clone https://github.com/path/repository.gitCreate a new branch based on [base_branch] or on current branch if [base_banch] isn't informed
and check it out right away:
git checkout -b <branch_name> [base_banch]Create a new branch based on current branch and stays on current branch:
git branch <branch_name>git branchgit branch -aList all tracking branches, their upstreams, last commit on branch, and if local branch is ahead, behind, or both
git branch -vvgit branch --merged <branch_name>(just get the changes, it doesn't merge them)
git fetch <remote_name>it's usually:
git fetch origingit statusShow the status of your working directory:
- new, staged, and modified files.
- current branch name
- current commit identifier
- changes pending to commit
git add <path/to/file.txt>git add .git commit -m "commit message"git commit -am "Commit message"Equivalent to:
git add .
git commit -m "Commit message"git add missing_file.txt
git commit --amend --no-editgit commit --allow-emptygit checkout <branch_name>git checkout -or:
git checkout @{-1}git fetch origin
git checkout <remote_branch_name>or:
git checkout -t origin/<remote_branch_name>git merge <branch_name_to_merge_on_current_branch>(fetch from remote and merge into local)
git pull origin <remote_branch_name>git cherry-pick <commit_hash>git checkout --theirs <path/to/file.txt>git checkout --ours <path/to/file.txt>E.g.:
git checkout --ours package-lock.jsongit push origin <remote_branch_name>-u: add upstream (tracking) reference
git push -u origin <remote_branch_name>--tags: push also the tags
git push --tags origin <remote_branch_name>--force: if there are changes on remote branch that aren't in local branch (command refuses to update the remote), and you want to overwrite them:
git push --force origin <remote_branch_name>Note: You can use HEAD instead of <remote_branch_name>:
git push origin HEADHEAD is the current branch on your local repository:
cat .git/HEAD
ref: refs/heads/<name_of_the_branch>git push --force-with-lease origin <branch_name>e.g.: commits, checkouts, pull, ... (also list removed commits with git reset, git rebase, ...)
git reflogMove them to stash: a place to temporarily store the modified and staged files in order to change branches.
git stashgit stash push -m <message>git stash -uor
git stash push -uor
git stash push --include-untrackedgit stash -aor
git stash --allor
git stash push --allgit stash listgit stash show -p <stash@{n}>git stash popgit stash apply <stash@{n}>git diffgit diff --stagedor
git diff --cachedgit diff HEADgit diff --name-only --diff-filter=Ugit show --name-onlygit log --no-merges --raw --since='2 weeks ago'or:
git whatchanged --since='2 weeks ago'git log -S '<content to search>'git log --all --grep='content to search'or
git log --oneline | grep -F 'content to search'git log -p <path/to/file.txt>git diff-tree --no-commit-id --name-only -r <commit_sha>(history as a one-line short message - sha & message)
git log --oneline --graph --all(with the first line of each commit message)
git shortloggit branch -m <old_name> <new_name>- Delete the current remote branch:
git push origin --delete <old_name>- Push the new local branch with the new name:
git push -u origin <new_name>List your existing remotes to get the name of the remote you want to change:
git remote -v
> origin https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin https://github.com/USERNAME/REPOSITORY.git (push)Rename the remote from old_name to new_name:
git remote rename <old_name> <new_name>E.g.:
git remote rename origin productionCheck that the remote URL has changed:
git remote -v
> production https://github.com/USERNAME/REPOSITORY.git (fetch)
> production https://github.com/USERNAME/REPOSITORY.git (push)To rename the existing local repository accordingly to the remote you can first rename the repository directory (optional) and then, to rename the remote URL to the new name:
List your existing remotes:
git remote -v
> origin https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin https://github.com/USERNAME/REPOSITORY.git (push)Rename the remote URL to new_url:
git remote set-url <remote_name> <new_url><remote_name>could beoriginorupstreamfor example<new_url>could be the HTTPS or SSH URL
Change the origin remote's HTTPS URL e.g.:
git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY.gitChange the origin remote's SSH URL e.g.:
git remote set-url origin git@github.com:USERNAME/NEW-REPOSITORY.gitCheck that the remote URL has changed:
git remote -v
> origin https://github.com/USERNAME/NEW-REPOSITORY.git (fetch)
> origin https://github.com/USERNAME/NEW-REPOSITORY.git (push)git mv <old_name> <new_name>
git commit -m "renamed"
git push origin main(retain the changes in working directory)
git reset HEAD <path/to/file.txt>(retain the changes in working directory)
git reset HEAD -- .(changes to the modified file are discarded)
git checkout -- <path/to/file.txt>(changes to the modified files are discarded)
git checkout .(most recent commit)
Keep the work done on last commit (files will show in the stage area as an uncommitted change):
git reset --soft HEAD^git reset HEAD~git reset HEAD <path/to/file.txt>Delete all the work done on last commit:
git reset --hard HEAD~1git reset [--hard] <target_reference>Switch the current branch to the target reference, leaving a difference as an uncommitted change:
git reset origin/masterSwitch the current branch to the target reference, discarding all changes
git reset --hard origin/master(Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.)
git revert <commit_sha>git reset --hard HEAD@{3.minutes.ago}git commit --amend -m "New message here"git rm --cached <path/to/file.txt>(uncommitted changes will be removed)
git restore .git branch <correct_branch_name>
git reset --hard <target_reference>
git checkout <correct_branch_name>Example: add local commits on correct fix_typo branch, remove them from the master branch, and checkout fix_typo branch:
git branch fix_typo
git reset --hard origin/master
git checkout fix_typogit branch -d <branch_name>-Dinstead of-dforces deletion
git push --delete <remote_name> <branch_name>e.g.:
git push --delete origin my_remote_branchgit rm <path/to/file.txt>git tag -d <name>git push --delete origin <tag_name>or:
git push origin :refs/tags/tag_nameRemove the [stash_name] informed or the last one if none is provided.
git stash drop [stash_name]e.g.:
git stash drop stash@{0}git stash clear(Remove untracked files. Modified files are unchanged)
git clean -f(Remove untracked files and directories. Modified files are unchanged)
git clean -f -dTypes of tags:
lightweight: just the commit checksum stored in a file, i.e., a pointer to a specific commitannotated: stored as full objects in Git (checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard - GPG).
git taggit describe --tagsgit tag <tag_name> [commit_sha]git tag -a <tag_name> [commit_sha] [-m "tagging_message"]e.g.:
git tag -a v2.1 -m "version 2.1"git show <tag_name>e.g.:
git show v2.1git remotegit remote set-url origin https://github.com/USERNAME/REPOSITORY.gitgit remote set-url origin git@github.com:USERNAME/REPOSITORY.gitgit notes add -m 'Note message here'git log --show-notes='*'git help -ggit instaweb --httpd apache2
git instaweb --httpd nginx
git instaweb --httpd=webrickgit fetch origin && git reset --hard origin/<branch_name> && git clean -f -dgit bisect start
git bisect bad
git bisect good