GIT CHEAT SHEET

Initializing a Repository in an Existing Directory

$ cd C:/Users/user/my_project
$ git init

Start Tracking Files

$ git add *.c
$ git add LICENSE
$ git commit -m 'Initial project version'

Cloning an Existing Repository

$ git clone https://github.com/libgit2/libgit2
$ git clone https://github.com/libgit2/libgit2 mylibgit  #renames the original clone

Checking the Status of Your Files

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Tracking New File

$ git add README

Short Status

$ git status -s 


M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt

New files that aren’t tracked have a ?? next to them, new files that have been added to the staging area have an A,
modified files have an M and so on. There are two columns to the output — the left-hand column indicates the status of
the staging area and the right-hand column indicates the status of the working tree. So for example in that output, the
README file is modified in the working directory but not yet staged, while the lib/simplegit.rb file is modified and
staged. The Rakefile was modified, staged and then modified again, so there are changes to it that are both staged and
unstaged.

GIT IGNORE

You can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:
$ cat .gitignore
*.[oa] *~
https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

# ignore all .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf

Viewing Your Staged and Unstaged Changes

$ git diff
$ git diff --staged

Committing Your Changes

$ git commit
$ git commit -m "Story 182: fix benchmarks for speed" # -m flag makes an inline commit
$ git commit -a -m 'Add new benchmarks' #-a flag skips git add and commits all tracked files

Removing Files

$ git rm PROJECTS.md # next commit deletes file and removes tracking. add -f flag for force
$ git rm --cached README # keep the file in your working tree but remove it from your staging area
$ git rm log/\*.log # glob commands are valid

Moving Files

$ git mv file_from file_to

 


Viewing the Commit History

$ git log
$ git log -p -2 # -p or --patch, which shows the difference, use -2 to show only the last two entries.
$ git log --stat # some abbreviated stats for each commit
$ git log --pretty=format # https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History#pretty_format

Limiting Log Output

$ git log --since=2.weeks
$ git log -S function_name # last commit that added or removed a reference to a specific function
$ git log -- path/to/file # limit the log output to commits that introduced a change to those files

https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History#limit_options

Undoing Things

$ git commit --amend # make the additional changes you forgot, stage them, and commit again
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend

Unstaging a Staged File

$ git reset HEAD CONTRIBUTING.md  # The CONTRIBUTING.md file is modified but once again unstaged.

Showing Your Remote

$ git remote -v

Adding Remote Repositories

git remote add pb https://github.com/paulboone/ticgit
git remote add  :

Fetching and Pulling from Your Remotes

git fetch #pulls down all the data from that remote project that you don’t have yet  Does not perform any merge operations
git fetch pb # all the information other user has but that you don’t yet have in your repository

git pull             # automatically fetch and then merge that remote branch into your current branch

Pushing to Your Remotes

$ git push origin master #to push your master branch to your origin server

Inspecting a Remote

git remote show      # It lists the URL for the remote repository as well as the tracking branch information

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

Renaming and Removing Remotes

$ git remote rename pb paul # rename pb to paul $ git remote
origin
paul
$ git remote remove paul # all remote-tracking branches and configuration settings associated with that remote are also deleted $ git remote
origin

Listing Your Tags

$ git tag
$ git tag -l "v1.8.5*"

Creating Tags

Annotated Tags
$ git tag -a v1.4 -m "my version 1.4"  #The -m specifies a tagging message, which is stored with the tag
$ git show v1.4  # see the tag data along with the commit that was tagged
Lightweight Tags
$ git tag v1.4-lw # don’t supply any of the -a-s, or -m options

Tagging Later

$ git tag -a v1.2 9fceb02

Sharing Tags

git push origin
$ git push origin --tags # transfer all of your tags to the remote server that are not already there 

Deleting Tags

$ git tag -d v1.4-lw # this does not remove the tag from any remote servers. 
$ git push origin --delete  # preferred method

$ git push origin :refs/tags/v1.4-lw              #null value before the colon is being pushed to the remote tag name, effectively deleting it.

Checking out Tags

$ git checkout v2.0.0 #puts your repository in “detached HEAD” state, which has some ill side effects https://git-scm.com/book/en/v2/Git-Basics-Tagging

Git Aliases

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit # to git commit, you just need to type git ci $ git config --global alias.st status
$ git config --global alias.visual '!gitk' # useful if you write your own external tools that work with a Git repository

https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

Creating a New Branch

$ git branch testing # This creates a new pointer to the same commit you’re currently on.

 

$ git log --oneline --decorate
f30ab (HEAD -> master, testing) Add feature #32 - ability to add new formats to the central interface
34ac2 Fix bug #1328 - stack overflow under certain conditions
98ca9 Initial commit
$ git log --oneline --decorate --graph --all
* c2b9e (HEAD, master) Made other changes
| * 87ab2 (testing) Made a change

git switch -c new-branch #Create a new branch and switch to it. full flag: –create
git switch -
# Return to your previously checked out branch

git checkout -b <newbranchname> # Creating a new branch and switching to it at the same time

MERGE BRANCHES

$ git checkout master
$ git merge hotfix

DELETE BRANCH

$ git branch -d hotfix
Deleted branch hotfix (3a0874c).