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-edit
git config --global color.ui auto
git config core.autocrlf
show 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 input
git config --list
git init <repository_name>
git clone <repository_url>
e.g:
git clone https://github.com/path/repository.git
Create 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 branch
git branch -a
List all tracking branches, their upstreams, last commit on branch, and if local branch is ahead, behind, or both
git branch -vv
git branch --merged <branch_name>
(just get the changes, it doesn't merge them)
git fetch <remote_name>
it's usually:
git fetch origin
git status
Show 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-edit
git commit --allow-empty
git 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.json
git 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 HEAD
HEAD 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 reflog
Move them to stash: a place to temporarily store the modified and staged files in order to change branches.
git stash
git stash push -m <message>
git stash -u
or
git stash push -u
or
git stash push --include-untracked
git stash -a
or
git stash --all
or
git stash push --all
git stash list
git stash show -p <stash@{n}>
git stash pop
git stash apply <stash@{n}>
git diff
git diff --staged
or
git diff --cached
git diff HEAD
git diff --name-only --diff-filter=U
git show --name-only
git 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 shortlog
git 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 production
Check 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 beorigin
orupstream
for 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.git
Change the origin
remote's SSH URL e.g.:
git remote set-url origin git@github.com:USERNAME/NEW-REPOSITORY.git
Check 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~1
git reset [--hard] <target_reference>
Switch the current branch to the target reference, leaving a difference as an uncommitted change:
git reset origin/master
Switch 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_typo
git branch -d <branch_name>
-D
instead of-d
forces deletion
git push --delete <remote_name> <branch_name>
e.g.:
git push --delete origin my_remote_branch
git rm <path/to/file.txt>
git tag -d <name>
git push --delete origin <tag_name>
or:
git push origin :refs/tags/tag_name
Remove 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 -d
Types 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 tag
git describe --tags
git 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.1
git remote
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
git notes add -m 'Note message here'
git log --show-notes='*'
git help -g
git instaweb --httpd apache2
git instaweb --httpd nginx
git instaweb --httpd=webrick
git fetch origin && git reset --hard origin/<branch_name> && git clean -f -d
git bisect start
git bisect bad
git bisect good