OMGit
OMGit copied to clipboard
Git Quick Reference
OMGit! <3
Git Quick Reference
Setting up
Set author name
(to be show on version history)
git config --global user.name "<firstname lastname>"
e.g.:
git config --global user.name "Andréia Bohner"
Set email address
git config --global user.email "<email>"
Change author's name and email
git commit --amend --author="<firstname lastname> <<email>>" --no-edit
Set automatic CLI coloring for Git
git config --global color.ui auto
Configure line-ending (CRLF)
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 around -
false
: turn off crlf -
true
: converts lf endings into crlf
e.g.:
git config --global core.autocrlf input
List all Git config settings
git config --list
Workflow
Create a new repository from current local directory (local files)
git init <repository_name>
Create a new repository locally from a remote repository
git clone <repository_url>
e.g:
git clone https://github.com/path/repository.git
Create a new branch
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>
List all local branches
git branch
List all local and remote branches
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
List all branches merged into the specified branch
git branch --merged <branch_name>
Get new changes, branches, and tags from remote repository
(just get the changes, it doesn't merge them)
git fetch <remote_name>
it's usually:
git fetch origin
Check changes on current branch
git status
Show the status of your working directory:
- new, staged, and modified files.
- current branch name
- current commit identifier
- changes pending to commit
Add a local changed file to stage area
git add <path/to/file.txt>
Add all local changes to stage area
git add .
Commit staged local changes on current local branch
git commit -m "commit message"
Add all local files to stage area and commit, with one line:
git commit -am "Commit message"
Equivalent to:
git add .
git commit -m "Commit message"
Add missing file on last local commit
git add missing_file.txt
git commit --amend --no-edit
Empty commit
git commit --allow-empty
Change to another local branch
git checkout <branch_name>
Back to the previous branch
git checkout -
or:
git checkout @{-1}
Get a remote branch locally
git fetch origin
git checkout <remote_branch_name>
or:
git checkout -t origin/<remote_branch_name>
Merge another local branch on current local branch
git merge <branch_name_to_merge_on_current_branch>
Merge changes from a remote branch on current local branch
(fetch from remote and merge into local)
git pull origin <remote_branch_name>
Pick a commit from a branch and apply it to another
git cherry-pick <commit_hash>
Conflicts
Resolve merge conflicts in favor of pulled changes during a pull
git checkout --theirs <path/to/file.txt>
Resolve merge conflicts in favor of my local changes during a pull
git checkout --ours <path/to/file.txt>
E.g.:
git checkout --ours package-lock.json
Push local changes to a remote branch
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>
Force push and ensure you don't overwrite work from others
git push --force-with-lease origin <branch_name>
List all operations made on local repository
e.g.: commits, checkouts, pull, ... (also list removed commits with git reset
, git rebase
, ...)
git reflog
Have to work on another branch. What to do with the changes on current branch?
Move them to stash: a place to temporarily store the modified and staged files in order to change branches.
Put the current working directory changes into stash, for later use
git stash
Put the current working directory changes into stash with a message
git stash push -m <message>
Put the current working directory changes into stash, including untracked files
git stash -u
or
git stash push -u
or
git stash push --include-untracked
Add all changed files of the current working directory into stash (ignored, untracked, and tracked)
git stash -a
or
git stash --all
or
git stash push --all
List all saved stashes
git stash list
Show the contents of a specific stash in patch form
git stash show -p <stash@{n}>
Get the stored stash content into working directory, and drop it from stash.
git stash pop
Apply the content of a specific stash without removing it from the stashed list
git stash apply <stash@{n}>
Checking changes
Unstaged changes
git diff
Changes staged but not commited
git diff --staged
or
git diff --cached
Staged and unstaged changes
git diff HEAD
All files with conflict
git diff --name-only --diff-filter=U
Show only changed files
git show --name-only
Changes since a provided period
git log --no-merges --raw --since='2 weeks ago'
or:
git whatchanged --since='2 weeks ago'
Search commits by content
git log -S '<content to search>'
Search by commit message
git log --all --grep='content to search'
or
git log --oneline | grep -F 'content to search'
Search all changes for specific file
git log -p <path/to/file.txt>
All changed files on specific commit
git diff-tree --no-commit-id --name-only -r <commit_sha>
Show all commits (Git history)
(history as a one-line short message - sha & message)
git log --oneline --graph --all
Summary of the commits grouped by author
(with the first line of each commit message)
git shortlog
Rename Things
Rename a local branch
git branch -m <old_name> <new_name>
Rename a remote branch
- 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>
Rename an existing remote 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)
Rename the remote url of an existing local repository to match the renamed remote repository
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 [email protected]: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)
Rename a file
git mv <old_name> <new_name>
git commit -m "renamed"
git push origin main
Undo Things
Unstage a file
(retain the changes in working directory)
git reset HEAD <path/to/file.txt>
Unstage all files
(retain the changes in working directory)
git reset HEAD -- .
Discard changes on unstaged file in working directory
(changes to the modified file are discarded)
git checkout -- <path/to/file.txt>
Discard changes on all unstaged files in working directory
(changes to the modified files are discarded)
git checkout .
Undo local unpushed commit
(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
Reverting changes
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
Reverting changes of a specific commit
(Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.)
git revert <commit_sha>
Reverting local changes to a relative time
git reset --hard HEAD@{3.minutes.ago}
Change the last (unpushed) commit message
git commit --amend -m "New message here"
Untrack files without deleting on working directory
git rm --cached <path/to/file.txt>
Discard all uncommitted changes on local working directory
(uncommitted changes will be removed)
git restore .
Revert local commits added on wrong branch and add them to the correct branch
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
Removing
Remove local branch
git branch -d <branch_name>
-
-D
instead of-d
forces deletion
Remove remote branch
git push --delete <remote_name> <branch_name>
e.g.:
git push --delete origin my_remote_branch
Remove file from working directory and Git repo
git rm <path/to/file.txt>
Remove a tag from local repository
git tag -d <name>
Remove a tag from remote repository
git push --delete origin <tag_name>
or:
git push origin :refs/tags/tag_name
Remove changes from stash
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}
Remove all stored stashes
git stash clear
Remove untracked files
(Remove untracked files. Modified files are unchanged)
git clean -f
Remove untracked files and directories
(Remove untracked files and directories. Modified files are unchanged)
git clean -f -d
Tagging
Types of tags:
-
lightweight
: just the commit checksum stored in a file, i.e., a pointer to a specific commit -
annotated
: 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).
List all tags
git tag
Show the current tag you are
git describe --tags
Create a lightweight tag for current commit or for [commit sha], if informed.
git tag <tag_name> [commit_sha]
Create an annoted tag for current commit if [commit sha] it's not informed
git tag -a <tag_name> [commit_sha] [-m "tagging_message"]
e.g.:
git tag -a v2.1 -m "version 2.1"
Show tag data with the commit that was tagged
git show <tag_name>
e.g.:
git show v2.1
Remote
List all remote references
git remote
Change the remote's URL from SSH to HTTPS:
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
Change the remote's URL from HTTPS to SSH:
git remote set-url origin [email protected]:USERNAME/REPOSITORY.git
Notes
Add object notes
git notes add -m 'Note message here'
Show all notes
git log --show-notes='*'
More useful commands
Git help guide
git help -g
Git web-based UI
git instaweb --httpd apache2
git instaweb --httpd nginx
git instaweb --httpd=webrick
Sync with remote, overwrite local changes
git fetch origin && git reset --hard origin/<branch_name> && git clean -f -d
Find the commit that has introduced a bug in the code (using binary search)
git bisect start
git bisect bad
git bisect good
References
- https://git-scm.com/
- https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories