Skip to content

Instantly share code, notes, and snippets.

@HendrixString
Last active October 23, 2019 15:36
Show Gist options
  • Save HendrixString/806898a4f3d14bc7c818ac3f255c486d to your computer and use it in GitHub Desktop.
Save HendrixString/806898a4f3d14bc7c818ac3f255c486d to your computer and use it in GitHub Desktop.
git cheat sheet

account

setup name and email
for a specific repository, navigate into it and type
git config user.name "your username"
git config user.email "[email protected]"
note add --global to make it global for new repositories

change your editor for commits
git config --global core.editor "pico"
get the remote repository url
git config --get remote.origin.url

staging

stage all changes
git add -A
remove files
git rm 'files'

commit

git commit -m "message"

status

the status of the repositiory
git status

show what happened in a commit

see what files changed
git show --name-status 94eeb069e27da06413a0c30da36f6b8a4ec92e24
see what files changed and a diff that it had
git show 94eeb069e27da06413a0c30da36f6b8a4ec92e24

log

log of commit in the current branch
git log regular
git log --oneline pretty one line with commit message
git log -p <branch/ref> shows diff chronologically starting from ref
git log --oneline --decorate --graph shows graph
git log --pretty=format:'%h - %an [%ar] %s' see formated
git log --pretty=format:'%C(yellow)%h%C(reset) - %an [%C(green)%ar%C(reset)] %s' formatted with colors
git log --grep 'search text' -E -i --oneline search in commit history
git log --grep='search text' -i --all search in all commit history
git log -- file same logging but for one file
git log -S"Hello, World!" --stat see commits and files affecting a source code line matching the string
git log --stat shows also the files names changed and addition/deletion counts
git log --author=<name or email> search by author

grep

git grep -i "text_to_search_in_code" search in code
git grep 'Build 0051' $(git rev-list --all)

blame

git blame path/to/file shows recent Annotates each line in the given file with information from the revision which last modified the line

show comit

git show commit_hash see the diff that the commit induced
git show --name-only commit_hash see only changed files

diff

git diff <branch_to_compare_with/hash> see the diff between current branch and the comapred
git diff <branch_to_compare_with/hash> --name-only see only the name of files changed between current branch and the comapred
git diff <branch_to_compare_with/hash> -- <file_path> see the diff of a file between current branch and the comapred

undoing changes in file

if you staged(added) a file and want to unstage it use
git reset -- path/to/file
if you want to unstage but keep the changes in the working directory
git reset --soft -- path/to/file
if you made changes to a file and haven't staged it yet and you want to undo changes use
git checkout -- path/to/file

undoing a commit

if you commited and want to undo the commit
git reset HEAD^
to make sure you uncommit but keep the changes in the working tree
git reset --soft HEAD^

change the latest commit message

git commit --amend

push

push the branch to remote repository, -u tells git to remember the upstream reference for next time
git push -u origin <branch_name_or_master>
git push is the same as git push origin

or you can define that git push will always push the current branch to a branch with the same name
git config --global push.default current

push to the upstream remote if they have the same name(default with Git 2.0)
git config --global push.default simple

add remote

add a remote repository, so we can push into
git remote add origin https://github.com/try-git/try_git.git

show remote

git remote show origin displays info for your remote origin repo.
I typically use this when I want to get the URL or the remote repo,
or to enter new username password when it has changed
git remote show origin
git remote show origin | grep "branch_name"

creating new repo and add remote and clone

two scenarios:

  • creating a new remote repo
echo "# sfly-quest" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/HendrixString/sfly-quest.git
git push -u origin master
  • push to an exiting empty remote repo, that was not cloned.
git remote add origin https://github.com/HendrixString/sfly-quest.git
git push -u origin master
  • clone a remote repository, so we can push into
git clone https://github.com/try-git/try_git.git <optional_local_folder>

fetch

The git fetch command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with. This gives you a chance to review changes before integrating them into your copy of the project.

Fetch all of the branches from the current repository
git fetch <remote> (usually origin)

fetch a specific branch.
git fetch <remote> <branch-name> (usually origin)

example

# checkout into local master branch
git checkout master 
# fetch/uodate the local remote master branch reference
git fetch origin master 
# Squash commits, fix up commit messages etc. like merge 
git rebase -i origin/master 
git push origin master

merge

merge a local branch with another.
example: the latest updates that were fetched from a remote

git checkout <branch_1>
# update the local remote branch tracking
git fetch origin <branch_2>
# review the changes that were added to the remote branch
git log --oneline <branch>..origin/<branch_2>
# merge this update
git merge origin/<branch_2>

merge the master with a local branch

git checkout master
git merge my_local_hotfix_branch

pull = fetch + merge

git pull
git pull <remote> <branch>

cherry pick a commit anywhere into the current branch

git checkout branch_to_apply_commit_onto
git cherry-pick commit_hash

branch

new branch, doesn't checkout
git branch <new-branch>

delete local existing branch
git branch -d <existing-branch>

delete remote existing branch
git push origin --delete <existing-branch>

show the remote branches

git branch -r
# origin/master
# origin/develop
# origin/some-feature

show the local branches
git branch

show all of the local and remote branches
git branch - a

show your current branch
git branch -vv | grep \*

checkout

  • checkout existing branch git checkout <existing-branch>
  • checkout a new branch from the current branch git checkout -b <new-branch>

example

# This will fetch all of the remote branches for you and update other tracked branches
git fetch origin
# see the branches available for checkout 
git branch -v -a
# checkout a remote branch
git checkout -b test origin/test

diff

git diff HEAD

discussions of fetch, rebase

How about (assuming you're currently on branch configUpdate):

git fetch
git rebase origin/master

In a nutshell:

  • git merge branchname takes new commits from the branch branchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.

  • git rebase branchname takes new commits from the branch branchname, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip of branchname, with any changes you made on top of that.

  • git pull is basically the same as git fetch; git merge origin/master.

  • git pull --rebase is basically the same as git fetch; git rebase origin/master.

So why would you want to use git pull --rebase rather than git pull? Here's a simple example:

  • You start working on a new feature.

  • By the time you're ready to push your changes, several commits have been pushed by other developers.

  • If you git pull (which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.

  • If you git pull --rebase instead, git will fast forward your master to upstream's, then apply your changes on top.

reverse stuff - single files

use git log to inspect changes and gather commit hashes
git log -p build.gradle

  1. undo unstaged changes to a file or folder git checkout <commit> file_name
  2. revert to another commit git reset <commit> path/to/unwanted_file

reverse stuff - the whole commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment