Skip to content

Instantly share code, notes, and snippets.

@bavey
Last active August 29, 2015 13:57
Show Gist options
  • Save bavey/9593978 to your computer and use it in GitHub Desktop.
Save bavey/9593978 to your computer and use it in GitHub Desktop.
Common Git Commands

Common Git Commands

Configure file locations

  • System
    • /etc/gitconfig
    • Program Files\Git\etc\gitconfig
  • User
    • ~/.gitconfig
    • $HOME\.gitconfig
  • Project
    • my_project/.git/config

Editing config files

  • System
    • git config --system
  • User
    • git config --global
  • Project
    • git config

git config --list

Configure git editor
git config --global core.editor "vim"

Configure git color
git config --global colur.ui true

Git Help
git help

Initialize Git Project
git init

Display log of commits
git log

Display 5 most recent commmit logs
git log -n 5

Display commmit logs starting at specific date
git log --since=2014-03-15

Display commmit logs in a range
git log --since="2 weeks ago" --until="3 days ago"

Display commmit logs from specific author
git log --author="Brian"

Git add and commit it one step
git commit -am "Example commit message"

If you make a mistake in a file (e.g. index.html) and need to pull down fresh and unaltered copy from repository (-- indicates stay in current branch)
git checkout -- index.html

Display SHA for current location of HEAD
cat .git/HEAD

Unstage filename (index.html)
git reset HEAD index.html

Move the HEAD pointer
git reset

Safetest way, moves the HEAD pointer: does not change staging index or working directory. Staging and working directory stay in their current state.
git reset --soft

Safetest way: moves the HEAD pointer: changes staging index to match repository. Does not change working directory.
git reset --mixed

Safetest way: moves the HEAD pointer: changes staging index and working directory to match repository.
git reset --hard

Remove untracked files (-n is a test run, -f is destuctive)
git clean -f

Gitignore

Gitignore files ending with a specific file extensions
*.php

Don't ignore this file
!index.php

Ignore all files in this directory with a trailing slash
asset/videos/

Diffing

In order to ignore a tracked file you need to untrack it first. Removes file from staging index.
git rm --cached file.txt

Compare what happened between two commits
git diff 268c4a2..83e9bad

Branching

List branches
git branch

Switch branches
git checkout <branch_name>

Compare two branches
git diff master..new_feature

Rename branch
git branch -m current_branch new_branch

Deleting a branch
git branch -d branch_name_to_delete

Stashing

Stashing
git stash save "message"

Remotes

See remote aliases
git remote

See remote more info on remote aliases
git remote -v

Aliases

Create a shortcut command for git status command
git config --global alias.st status

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