Skip to content
Ed Rooth edited this page Jun 16, 2015 · 11 revisions

Config

Some useful defaults to add to your global .gitconfig file.

[color]
  ui = auto
  branch = auto
  diff = auto
  interactive = auto
  status = auto

[branch]
  autosetupmerge = true
[push]
  default = upstream

General

Stage a file
git add <file>

Commit all staged changes
git commit

See what you’ve changed but not yet staged
git diff

See what you’ve staged that will go into your next commit
git diff --staged

Diff a file across 2 branches
git diff master mybranch -- myfile.js

Remove file from git tracking and working directory
git rm <file>

Remove file from git tracking ONLY and KEEP in working directory
git rm --cached <file>

Undoing stuff

https://github.com/blog/2019-how-to-undo-almost-anything-with-git

Change last commit (used to modify the last commit, not revert)
Don’t do this if already pushed shared branch or master
git commit --amend

Undo the last commit and put the changes back into the working index.
git reset --soft HEAD^

Revert the last commit
git revert HEAD

Revert all uncommitted local working tree changes
git reset --hard HEAD

Unstage a file
git reset HEAD <file>

Undo all commits up until (moves HEAD)
git reset --soft <sha>

Revert all working changes
git checkout -f

Revert a specific modified file
git checkout -- <file>

Discard all non-staged files
git checkout -- .

Delete all untracked files
git clean -f

Branching

"In Git it’s common to create, work on, merge, and delete branches several times a day."

It’s best to have a clean working state when you switch branches.

List all local branches
git branch

List all local branches already merged into your current branch
git branch --merged

List all local branches NOT already merged into your current branch
git branch --no-merged

Create a new local branch
git branch <branch-name>

Switch to an existing local branch
git checkout <branch-name>

Delete a local branch
git branch -d <branch-name>

Push a local branch to remote
git push origin <branch-name>

Create a local tracking branch from a remote branch (when remote exists, but local doesn’t) (if local list of remotes do not already exist, must run git fetch or git remote update first to refresh your local list of remote branches)
git checkout --track <remote>/<remote-branch>

Create and switch to new branch, AND link to new remote tracking branch
git checkout -b <new-branch> git push -u origin <new-branch>

Delete a remote branch
git push origin :<remote-branch>

Reset local branch to match remote branch (if branch was force pushed)
git reset origin/<remote-branch> --hard

Merging

Term: Fast Forward: No divergent work to merge. Branch pointer simply advances forward.
Terms:

  • $LOCAL Current branch version
  • $REMOTE Version to be merged
  • $BASE Common ancestor
  • $MERGED File where results will be written

Merge (3-way)
Checkout the branch you wish to merge into then merge.
git checkout <merge-into-branch> git merge <merge-in-branch>

View unmerged conflicting files.
git status

Resolve Conflicts
Fix all conflicting files listed as "unmerged" from the git status command in the working directory. For all files changed in step 1 run:
git add <file> git commit Be sure to add a comment about the merge.

Rebase
(Same result as merge, but history is linear). Do this to make sure your commits apply cleanly on a remote branch. Essentially gets all updates to master, then applies the branch changes on top of master.
git checkout <topic-branch> git rebase <base-branch>

OR

git rebase <base-branch> <topic-branch>
git checkout <base-branch>
git merge <topic-branch>

Do not rebase commits that you have pushed to a shared branch
via: git push --force (overwriting server history)

Perform a merge using local version for all conflicts
git merge <branch> -s ours

Stashing

List all stashes
git stash list

Stash your current work
git stash

Create a branch out of a stash
git stash branch <branch-name>

Tagging

List all tags
git tag

Create tag
git tag -a <tag-name> -m '<message>'

Push all local tags to server
git push origin --tags

Links

Some useful git related links.