Skip to content

Latest commit

 

History

History
579 lines (409 loc) · 14.4 KB

Git-CheatSheet.md

File metadata and controls

579 lines (409 loc) · 14.4 KB

Git Cheat Sheet

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/#

How can I delete branches in Git?

Delete a remote branch

git push origin --delete feature/#

Can you delete multiple branches in one command with Git?

Undo a delete

git checkout -f

CAUTION: commit uncommitted files before executing this command, otherwise you're going to lose them all.

Undo delete in Git

Undo a git add

git reset

Undo a git add - remove files staged for a git commit

Undo the last commit

git reset HEAD~

A more detailed example

Use CRLF Line Endings

Create a .gitattributes file. Then

# Match files that will always have CRLF line endings on checkout
*.bat text eol=crlf

Discard changes in working directory

git checkout -- <file>

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

--bdonlan

List commits with short hash

git log --oneline --decorate --abbrev-commit -n 20

Show graph.

git log --oneline --decorate --abbrev-commit -n 20 --graph

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..<branchname>

How do I run git log to see changes only for a specific branch?
git - changes to branch since created?

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.

Check out fresh local branch

If the branch exists on origin but not yet locally, do the following.

git fetch
git checkout test
  1. How do I check out a remote Git branch?
  2. How can I switch to another branch in git?

Fetch remote branch

git checkout --track origin/daves_branch

Git fetch remote branch

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.

  1. How can I add an empty directory to a Git repository?
  2. What are the differences between .gitignore and .gitkeep?

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 <old_name>
git branch -m <new_name>
git push origin -u <new_name>
push origin --delete <old_name>

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

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

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:<repo>/<project>.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 <gitlabRepoUrl>
cd <repoName>
git remote add bitbucket <bitbucketRepoUrl>
git push bitbucket master

How to import gitlab repository to bitbucket Repository

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

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

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

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.

Self-Signed Certificates

To initially clone the repository, set an environment variable.

$ export GIT_SSL_NO_VERIFY=1

Then clone:

$ git clone https://<repo>.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 <name@email.com>"
git commit --amend --reset-author

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 <command> <arguments>


# If you don't want to do this all the time, you can change your git configuration:
$ git config --global http.sslVerify false

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