Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Last active February 28, 2018 02:36
Show Gist options
  • Save carlessanagustin/31c654b502e1fce23afb to your computer and use it in GitHub Desktop.
Save carlessanagustin/31c654b502e1fce23afb to your computer and use it in GitHub Desktop.
This is a GIT Cheat Sheet

GIT Cheat Sheet

SETUP

git config --global user.name “[firstname lastname]”

set a name that is identifiable for credit when review version history

git config --global user.email “[valid-email]”

set an email address that will be associated with each history marker

git config --global color.ui auto

set automatic command line coloring for Git for easy reviewing

SETUP & INIT

git init

initialize an existing directory as a Git repository

git clone [url]

retrieve an entire repository from a hosted location via URL

STAGE & SNAPSHOT

git status

show modified files in working directory, staged for your next commit

git add [file]

add a file as it looks now to your next commit (stage)

git reset [file]

unstage a file while retaining the changes in working directory

git diff

diff of what is changed but not staged

git diff --staged

diff of what is staged but not yet committed

git commit -m “[descriptive message]”

commit your staged content as a new commit snapshot

BRANCH & MERGE

git branch

list your branches. a * will appear next to the currently active branch

git branch [branch-name]

create a new branch at the current commit

git checkout

switch to another branch and check it out into your working directory

git merge [branch]

merge the specified branch’s history into the current one

git log

show all commits in the current branch’s history

INSPECT & COMPARE

git log

show the commit history for the currently active branch

git log branchB..branchA

show the commits on branchA that are not on branchB

git log --follow [file]

show the commits that changed file, even across renames

git diff branchB...branchA

show the diff of what is in branchA that is not in branchB

git show [SHA]

show any object in Git in human-readable format

TRACKING PATH CHANGES

git rm [file]

delete the file from project and stage the removal for commit

git mv [existing-path] [new-path]

change an existing file path and stage the move

git log --stat -M

show all commit logs with indication of any paths that moved

IGNORING PATTERNS

logs/
*.notes
pattern*/

Save a file with desired patterns as .gitignore with either direct string matches or wildcard globs.

git config --global core.excludesfile [file]

system wide ignore pattern for all local repositories

SHARE & UPDATE

git remote add [alias] [url]

add a git URL as an alias

git fetch [alias]

fetch down all the branches from that Git remote

git merge [alias]/[branch]

merge a remote branch into your current branch to bring it up to date

git push [alias] [branch]

Transmit local branch commits to the remote repository branch

git pull

fetch and merge any commits from the tracking remote branch

REWRITE HISTORY

git rebase [branch]

apply any commits of current branch ahead of specified one

git reset --hard [commit]

clear staging area, rewrite working tree from specified commit

TEMPORARY COMMITS

git stash

Save modified and staged changes

git stash list list stack-order of stashed file changes

git stash pop

write working from top of stash stack

git stash drop

discard the changes from top of stash stack


GIT Cheat Sheet examples

Commands

Getting Started

git init
git clone url

Configuration

git config --global user.name "username"
git config --global user.email email@example.com
git config --global color.ui true
git config --global push.default current
git config --global core.editor vim
git config --global diff.tool meld

Working with Local Branch

Branching

git branch

See the list of all local branches

git checkout branchname

Switch to existing local branch

git checkout -b new-branch-name

Checkout current branch into a new branch, named new-branch-name

git merge branchname

Merge branch-name into the current branch

git branch -d branchname

Soft branch delete, will complain if the branch is not merged

Updating Current Branch

Standard Flow

git log

See all commits

git log --pretty=format:"%h %s" --graph

Pretty commit view, you can customize it as much as you want.

git log --author='Alex' --after={1.week.ago} --pretty=oneline --abbrev-commit

See what you worked on in the past week

git log --no-merges master..

See only changes made on this branch (assuming it was branched form master branch)

git status -s

Short view of status. Helpful for seeing things at a glance

git add filename

Add modified file to be commited(aka stage the file)

git add .

Add all modified files to be commited(aka stage all files)

git add '*.txt'

Add only text files, etc.

git rm filename

Tell git not to track file anymore

git commit

Record changes to git. Default editor will open for a commit message.

git commit -m 'Some commit message'

A short hand for commiting files and writing a commit message via one command

git commit --amend

Changing the history

Advanced

git reset

Unstage pending changes, the changes will still remain on file system

git reset --hard HEAD

Unstage pending changes, and reset files to pre-commit state.

git reset tag
git reset <commit-hash>

Go back to some time in history, on the current branch

git stash

Save current changes, without having to commit them to repo

git stash pop

And later return those changes

git checkout filename 

Return file to it's previous version, if it hasn’t been staged yet. Otherwise use git reset filename or git reset --hard filename

Comparing changes

git diff

See current changes, that have not been staged yet.

git diff HEAD

See current changes, that have not been commited yet (including staged changes)

git diff branch-name

Compare current branch to some other branch

git difftool -d

Same as diff, but opens changes via difftool that you have configured -d tells it to open it in a directory mode, instead of having to open each file one at a time.

git difftool -d master..

See only changes made in the current branch (compared to master branch)

git diff --no-commit-id --name-only --no-merges origin/master...

See only the file names that has changed in current branch

git diff --stat #Your diff condition

Similar to above, but see statistics on what files have changed and how

Working with Remote Branch

git remote

See list of remote repos available. If you did git clone, you'll have at least one named "origin"

git remote -v

Detailed view of remote repos, with their git urls

git remote add origin <URL>

Add a new remote. I.e. origin if it is not set

git push

Push current branch to remote branch (usually with the same name) called upstream branch

git push -u origin master

If a remote branch is not set up as an upstream, you can make it so The -u tells Git to remember the parameters

git push origin branchname

Otherwise you can manually specify remote and branch to use every time

git pull

Just like pushing, you can get the latest updates from remote. By defaul Git will try to pull from "origin" and upstream branch

git pull origin branchname

Or you can tell git to pull a specific branch

git fetch && git merge origin/remote-branch-name    

Git pull, is actually a short hand for two command. Telling git to first fetch changes from a remote branch And then to merge them into current branch

git fetch -p

If you want to update history of remote branches, you can fetch and purge

git branch -a

To see the list of remote branches -a stands for all

Resources

Reference

Viewing History

  • Source Tree
  • tig - sudo apt-get install tig or brew install tig etc
  • gitk - sudo apt-get install gitk

Merge/Diff Tools

  • Meld - sudo apt-get install meld or brew install meld

  • Open Diff

  • p4v Merge

    • Webstorm
  • More tips


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