Git Cheatsheet
Git Branch Management
Show the current branch
git rev-parse --abbrev-ref HEAD
Show just the current branch in Git
Delete a local branch
git branch -d feature/login
How can I delete branches in Git?
Delete a remote branch
git push origin --delete feature/login
Can you delete multiple branches in one command with Git?
Move uncommitted work to a new branch
git checkout -b development
git add .
git commit -m "First commit in the new branch"
Move existing, uncommitted work to a new branch in Git
Push branch to remote repo
git push -u origin feature_branch_name
-u
is short for --set-upstream
How do I push a new local branch to a remote Git repository and track it too?
git-push - Update remote refs along with associated objects
Clone a single branch
git clone <url> --single-branch
# Longer form
git clone <url> --branch <branch> --single-branch [<folder>]
How do I clone a single branch in Git?
Show changes to branch
git log master..
git log master..
How do I run git log to see changes only for a specific branch?
git - changes to branch since created?
Check out fresh local branch
If the branch exists on origin but not yet locally, do the following.
git fetch
git checkout test
Fetch remote branch
git checkout --track origin/daves_branch
Resolve your branch is ahead of 'origin/master' by x commits
git rebase -i origin/master
Typically, you would use git reset --hard origin/master
but in this case I wanted to see how the local master differed from the remote one. See Your branch is ahead of 'origin/master' by 3 commits.
Rename a local and remote branch
git checkout
git branch -m
git push origin -u
push origin --delete
Or
git branch -m new-name
git push origin :old-name new-name
git push origin -u new-name
See Rename a local and remote branch in git
How To Rename a Local and Remote Git Branch
Undoing Changes and Operations
Undo a delete
git checkout -f
CAUTION: commit uncommitted files before executing this command, otherwise you're going to lose them all.
Undo a git add
git reset
Undo a git add - remove files staged for a git commit
Undo the last commit
git reset HEAD~
Discard changes in working directory
git checkout --
Undo git revert HEAD
git reset --hard HEAD^
See Is there any way to undo the effects of “git revert head”?.
Remove a file from a Git repository without deleting it from the local
For single file:
git rm --cached mylogfile.log
For single directory:
git rm --cached -r mydirectory
Remove and then ignore IDE settings
git rm --cached .project
git rm --cached .classpath
git rm --cached -r .settings
Then add to .gitignore
.project
.classpath
.settings/
Example .gitignore
# Eclipse
.classpath
.project
.settings/
# Maven
log/
target/
How to ignore IDE settings on Git?
A .gitignore file for Intellij and Eclipse with Maven
Repository Synchronization and Remotes
Sync local repo with remote one
git fetch --prune
Also removes completed branch. Sync local repo with remote one
Syncing a fork
git fetch upstream
git checkout master
git merge upstream/master
Working with forks / Syncing a fork
Sync your Git Fork to the Original Repo
How do I update a GitHub forked repository?
Or
git pull upstream develop
Switch to a branch and sync
git pull origin other-branch
Which is equivalent to
git fetch origin other-branch && git merge other-branch
See Git pull a certain branch from GitHub.
Determine from where a local repository was cloned
git remote show origin
Or if referential integrity has been broken:
git config --get remote.origin.url
Or simply
git remote -v
How can I determine the URL that a local Git repository was originally cloned from?
Change remote URL
Origin
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
Upstream
git remote set-url upstream https://github.com/[Original Owner Username]/[Original Repository].git
git remote set-url upstream git@github.com:[Original Owner Username]/[Original Repository].git
SSH URLs
git@github.com:/.git
Push an existing repository from the command line
git remote add origin https://github.com/gkhays/orientdb-testdrive.git
git push -u origin master
Push the curent branch and set the remote as upstream
git push --set-upstream origin developer
Import repository to Bitbucket
Create a new repo in bitbucket.
git clone
cd
git remote add bitbucket
git push bitbucket master
How to import gitlab repository to bitbucket Repository
Repository Creation and Configuration
Show your configuration
git config --list
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/gkhays/orientdb-testdrive.git
git push -u origin master
git fetch --prune
-p, --prune
After fetching, remove any remote-tracking branches which no longer exist on the remote -- http://stackoverflow.com/a/15124916/6146580
Git Hashes
$ git describe --tags
v1.0.1-2-g39918b9
$ git rev-parse --short HEAD
39918b9
Get the short Git version hash
git-rev-parse - Pick out and massage parameters
Diff and Merge Operations
Compare Two Git Branches
gti diff patch/feat36..develop
git log patch/feat36..develop
git log --oneline --graph --decorate --abbrev-commit patch/ig36..develop
Compare a file between two Git branches
git diff patch/feat35...patch/feat36 Jenkinsfile
Or
git diff mybranch..master -- myfile.cs
How To Compare Two Git Branches
Comparing two branches in Git?
Showing which files have changed between two revisions
git-diff - Show changes between commits, commit and working tree, etc
How can I see a “three way diff” for a Git merge conflict?
git-merge(1) Manual Page
How to compare files from two different branches
Diff Tools
Add the following to ~/.gitconfig
.
[merge]
tool = bc3
[diff]
tool = bc3
[difftool "bc3"]
cmd = "\"C:/Program Files (x86)/Beyond Compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\""
[mergetool "bc3"]
cmd = "\"c:/program files (x86)/beyond compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\""
Invoke the diff tool.
D:\src\test-remote (master -> origin)
λ git difftool -t bc3 src/main/org/gkh/test/remote/JarCommand.java
Viewing (1/1): 'src/main/java/org/gkh/test/remote/JarCommand.java'
Launch 'bc3' [Y/n]? y
Flight rules for git
Git and Credentials
Git and Passwords
git config --global --unset user.password
Caching your Git Password On Windows
$ git config --global credential.helper wincred
Cachiang your Git Password on Linux
git config --global credential.helper cache
Add optional timeout
git config --global credential.helper cache -timeout=3600
- How do I update the password for Git?
- https://git-scm.com/book/gr/v2/Git-Tools-Credential-Storage
- git credential helper - update password
- Caching Your Password
Git Aliases and Configuration
Possible Aliases
alias ga='git add'
alias gaa='git add .'
alias gaaa='git add --all'
alias gau='git add --update'
alias gb='git branch'
alias gbd='git branch --delete '
alias gc='git commit'
alias gcm='git commit --message'
alias gcf='git commit --fixup'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout master'
alias gcos='git checkout staging'
alias gcod='git checkout develop'
alias gd='git diff'
alias gda='git diff HEAD'
alias gi='git init'
alias glg='git log --graph --oneline --decorate --all'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias gm='git merge --no-ff'
alias gma='git merge --abort'
alias gmc='git merge --continue'
alias gp='git pull'
alias gpr='git pull --rebase'
alias gr='git rebase'
alias gs='git status'
alias gss='git status --short'
alias gst='git stash'
alias gsta='git stash apply'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gsts='git stash save'
From Git Command-Line Shortcuts.
Handling SSL Certificates
Self-Signed Certificates
To initially clone the repository, set an environment variable.
$ export GIT_SSL_NO_VERIFY=1
Then clone:
$ git clone https://.git
Alternatively, using the -c
switch
$ git -c http.sslVerify=false clone https://github.com/gkhays/cheatsheets.git
It is possible to globally disable certificate verification, --global
, but the best practice is to only do it on repos you trust.
$ git config http.sslVerify false
or
$ git -c http.sslVerify=false
It would be insecure to disable certificate validation on a global basis, so as a rule don't do it, e.g. avoid $ git config --global http.sslVerify false
.
$ git config --system http.sslCAPath /path/to/cacerts
Expected Committer Email
git commit --amend --allow-empty --author="FirstName LastName "
git commit --amend --reset-author
- https://stackoverflow.com/a/28425852/6146580
- https://stackoverflow.com/a/33009142/6146580
From WikiLeaks Vault 7
# Issue: When attempting to clone (or any other command that interacts with the
# remote server) git by default validates the presented SSL certificate by the
# server. Our server's certificate is not valid and therefore git exits out with
# an error. Resolution(Linux): For a one time fix, you can use the env command
# to create an environment variable of GIT_SSL_NO_VERIFY=TRUE.
$ env GIT_SSL_NO_VERIFY=TRUE git
# If you don't want to do this all the time, you can change your git configuration:
$ git config --global http.sslVerify false
Miscellaneous Git Commands
Use CRLF Line Endings
Create a .gitattributes
file. Then
# Match files that will always have CRLF line endings on checkout
*.bat text eol=crlf
Track an Empty Directory
You cannot commit a completely empty directory in Git. The convention is to add a placeholder file named .gitkeep
to the target directory.
- How can I add an empty directory to a Git repository?
- What are the differences between .gitignore and .gitkeep?
References
Git vs SVN commands
How can I make git accept a self signed certificate?
How do I set GIT_SSL_NO_VERIFY for specific repos only?
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
configure Git to accept a particular self-signed server certificate for a particular https remote
Https certificate errors for GitHub using git on Windows 7
Unable to clone Git repository due to self signed certificate
https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh
Setting up a repository