| Command | Description |
|---|---|
git clone <url> |
Clone repo from in current directory |
git clone <url> new-name |
Clone repo from in current directory name sub-dir new-name |
git status |
Show the current state of the repo, including changed and uncommitted files |
| Command | Description |
|---|---|
git log |
Output all commit history |
git log --oneline |
Output all history with short SHA and commit message only |
git log --pretty=oneline |
Same as above but outputs the full SHA |
git log -5 |
Log 5 most recent commits |
git log ref1..ref2 |
Log commit present in ref2 that don't exist in ref1 |
Set a named pretty format string:
# Outputs with custom colors the author short-date, short sha,
# commit title fixed to 40 characters, and author email
git config pretty.column \
'%C(#e500ff)%as%C(reset) %C(#ffff00)%h%C(reset) %<(40,trunc)%s %C(#00a1ff)%ae%C(reset)'To use this formatter, specify it by name in the --pretty argument of git log:
git log --pretty=column| Command | Flag | Description |
|---|---|---|
git branch |
List local branches | |
| -v | Verbose output - includes short hash and commit message | |
| -a | All - includes branches on remote repositories | |
| -d | Delete branch | |
| -D | Force delete - will delete branch even if unmerged | |
git branch <branch name> |
Create a new branch from current working tree. | |
git checkout -b <branch name> |
Create a new branch and immediately switch to it. | |
git checkout <branch name> |
Switch to a different branch. | |
git push -d <remote name> <branch name> |
Delete a remote branch | |
git push <remote name> <branch name> |
Push to a remote branch, will publish a new remote branch if one doesn't already exist | |
git push -u <remote name> |
Push currently checked out branch to remote and set as remote tracking branch |
git diff-tree ref1..ref2Use recursive flag, -r, to list files changed in subdirectories.
--name-status flag fill output the name and status (add, remove, modify) of files changed between
two references (branch, commit, tag, etc) - Order matters when using --name-status.
--name-only will output only the names of files changed.
The git merge-base command can be used to find the point at which 2 branches diverged.
git merge-base <branch1> <branch2> will output the full SHA of the most recent common ancestor of 2
branches.
Use the following to output the commit as a short SHA and commit message.
git log --oneline -1 `git merge-base <branch1> <branch2>`| Command | Description |
|---|---|
git stash |
Create a stash of the currently modified tracked files and staged changes |
git stash save "<message>" |
Create an annotated stash with a message |
git stash list |
List all stashes currently on the stack |
git stash apply |
Apply the most recently created stash to the currently checked out branch |
git stash apply stash@{n} |
Apply the stash at position n |
git stash pop |
Apply the most recently created stash then delete it. (Optional stash@{n} param) |
git stash clear |
Delete all stashes on the stack |
git stash drop |
Delete the stash on the top of the stack. (Optional stash@{n} param) |
| Command | Description |
|---|---|
git tag |
List all the current tags |
git tag -l "<pattern>" |
Search for a tag with a wildcard search e.g. v1.8.* |
git show-ref --tags |
List all tags with the commit they point to |
git tag -a <tag> -m "<message>" |
Created an annotated tag |
git tag -a <tag> -m "<message>" <hash> |
Create an annotated tag pointing to a specific hash |
git tag <tag> |
Create a lightweight tag (does not include tagger info or message) |
git push origin <tag> |
Publish a tag to remote named origin |
git push origin --tags |
Publish all tags to remote named origin |
git tag --delete <tag> |
Delete a local tag |
git push origin :<tag> |
Delete a remote tag (pushes empty reference to remote tag) |
git push --delete origin <tag> |
Delete a remote tag, more expressive than above |
| Command | Description |
|---|---|
git remote -v |
List all remote repos and their push/pull URLs |
git remote add <remote_name> <url> |
Add a new remote repo |
git remote set-url <remote_name> <new_url> |
Update the URL for a remote repo |
| Command | Description |
|---|---|
git config --list |
List all non-default config settings |
git config --list --show-origin |
List all non-default settings and their file location |
[user]
name = Bryce Jech
email = [email protected]
[credential]
helper = osxkeychain
[core]
editor = nvim
pager = less -FX
ignorecase = false
autocrlf = input
eol = lf
[pager]
branch = false
[pull]
rebase = true
[init]
defaultBranch = main
[pretty]
col = "%C(#00a1ff)%<(12,trunc)%an%C(reset) %C(#e500ff)%as%C(reset) %C(#ffff00)%h%C(reset) %<(60,trunc)%s"As early in the history of a repository as possible (preferably as part of the initial commit) add a
.gitattributes file to the repository root directory with the following contents. Setting
text=auto for all files is essentially the same as setting core.autocrlf=input in a local/global
user config file.
* text=auto
If you have repositories in multiple upstream version control systems, you may want to set up and manage multiple SSH keys. Create a ~/.ssh/config file that names each host and the configurations you want for them.
If you need to use different keys for the same host you can append a - character plus some text to identify which config block you want to use when cloning a repository. For example, in the below config there are 2 blocks for github.com that have -account1 and -account2 appended to the host. When cloning a repo, to use account1 you would append it to the hostname in the clone command like so: git clone [email protected]:brycejech/git-cheat-sheet.git
Host gitlab.com
HostName gitlab.com
User git
AddKeysToAgent yes
ForwardAgent yes
UseKeychain yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
Host github.com-account1
HostName github.com
User git
AddKeysToAgent yes
ForwardAgent yes
UseKeychain yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_account1
Host github.com-account2
HostName github.com
User git
AddKeysToAgent yes
ForwardAgent yes
UseKeychain yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_account2If you've accidentally added a file to the repo that should not be there, e.g. security related
files, user names/passwords, etc, and have made several commits since adding the file, the
git filter-branch command can be your friend.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch <filename>'
The --ignore-unmatch in the inner command means that git won't fail if the given filename is
missing from any given change. You can optionally use --tree-filter instead of --index-filter.
--index-filter is very similar to --tree-filter except that it does not check out the tree,
giving a performance boost.
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Bryce Jech'; GIT_AUTHOR_EMAIL='[email protected]'; GIT_COMMITTER_NAME='Bryce Jech'; GIT_COMMITTER_EMAIL='[email protected]';" HEAD
If getting a 404 when attempting to push to a repo, it may be that you don't have permission or that the repo is simply not configured properly.
If you do not have the ability to use ssh, you can modify the remote reference url to include your
username and, optionally, your password. Note that if you must use an @ symbol in your username,
it must be URL encoded as %40. All special characters in the username/password string should be
safely URL encoded.
git remote set-url origin https://[email protected]/path/to/repo.git
To include your password (not recommended) the command would look like this:
git remote set-url origin https://username:[email protected]/path/to/repo.git