Skip to content

Instantly share code, notes, and snippets.

@erikcox
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikcox/9df2141a315bcb0f8ea1 to your computer and use it in GitHub Desktop.
Save erikcox/9df2141a315bcb0f8ea1 to your computer and use it in GitHub Desktop.
Git notes
# Setup
git config --global user.name "Erik Cox"
git config --global user.email "erikbcox@gmail.com"
git config --global color.ui auto
# Creating a new local repository
git init
# Cloning
git clone <URL>
# ssh://user@server/git-repo.git
# user@server:git-repo.git
# http://example.com/git-repo.git
# https://example.com/git-repo.git
# git://example.com/git-repo.git
# Status of a file
# 3 areas:
# Working Copy: Your project's files
# Staging Area: Changes included in next commit
# Local Repository: The ".git" folder
# 3 file status'
# tracked, modified
# tracked, unmodified
# untracked
# Commit only related changes at the same time.
git status
git add <file>
git rm <file>
git commit -m "<message>"
# Commit history
git log
git log -p -1 # Patch or diff or latest 1 commit
# Branching
git branch -v # Overview of current local branches
git branch <branch>
git checkout <branch>
# To merge with master
# Switch to master
git checkout <master>
git merge <branch>
# Saving local changes temporarily
# Never commit half-done work
git stash
git stash list
git stash pop
# Sharing work via remote repositories
git fetch # Downloads to local repository
git pull # Downloads to working copy
git stage # Adds to staging area
git commit # Adds to local repository
git remote -v # Which remotes are connected w/ local repository
git push
# Removing commited files that have been added to .gitignore after the fact
git rm -r --cached .
git add .
git commit -am "Removing ignored files"
##########################################################
# More notes from http://rogerdudler.github.io/git-guide/
git add <filename>
git commit -m "Commit message"
git push origin master # replace master with branch name if necessary
git remote add origin <server> # If you haven't cloned an existing repository and want to connect your repository to a remote server
# Branching
git checkout -b feature_x # create a new branch named "feature_x" and switch to it
git checkout master # switch back to master
git branch -d feature_x # delete the branch
git push origin <branch> # push the branch
# Update & Merge
git pull # to update your local repository to the newest commit (fetch and merge remote changes)
git merge <branch> # merge another branch into your active branch (e.g. master)
git diff <source_branch> <target_branch> # preview changes before merging
# Tagging
git tag 1.0.0 1b2e1d63ff # new tag named 1.0.0. 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag
# Log
git log --author=bob
git log --pretty=oneline
git log --graph --oneline --decorate --all
git log --name-status # See only which files have changed
# Replace local changes
git checkout -- <filename>
# Drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it:
git fetch origin
git reset --hard origin/master
# useful hints
gitk # built-in git GUI
git config color.ui true # use colorful git output
git config format.pretty oneline # show log on just one line per commit
git add -i # use interactive adding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment