Before publishing your changes, get everybody else's changes first:
git pull
Publish your changes:
git push
Getting Things Done (faster)
Stage all changes:
git add -A .
Commit all modified and deleted files (but not new files):
git commit -a
.gitignore
Add a .gitignore file to exclude files from the shortcut commands (e.g. git add -A).
# Ignore files in this directory and all subdirectories.
*.class
*.pyc
# Ignore specific files and directories
# (relative to this directory)
/target/
/test/output/*.xml
Getting Things Done (smarter)
Stage only part of a file:
git add -p file.html
Show your changes in the commit editor:
git commit -v
Temporarily save what you're working on…
git stash
… do something else …
… then pick up where you left off:
git stash pop
Branches
Create a new local branch:
git checkout -b fixes
Create a local branch based on a published branch:
git checkout --trackorigin/fixes
Switch between branches:
git checkout master git checkout fixes
Merging
Merge "master" to your branch:
git checkout fixes # Switch to your branch.
git merge master # Merge changes from master
Merge your branch back into "master":
git checkout master # Switch back to master.
git merge fixes
All done with the branch, delete it:
git branch -d fixes
Sharing Branches
Publish your branch:
git push -u origin fixes
Delete a published branch:
git push origin --delete fixes
Pull Requests
GitHub
Review & discuss changes before merging
Pull Request
Gather your changes in a branch.
Publish the branch.
Create a Pull Request on GitHub.
intermediate ± git
Here be bobcats
Rewriting History
Optimist:
If you haven't pushed it yet, you can still change it
Pessimist:
If you change a pushed commit, bad things happen
(don't change published history)
Oops, I made a typo…
Stage your fixes like usual...
Then amend the previous commit instead of creating a new one:
git commit --amend
pull + rebase
Pull in other people's commits, but move your unpushed
commits to the end.
git pull --rebase
Some teams prefer this since it results in more linear history.
Rebase Whole Branches
Move all of the commits in your local branch to the end of the
branch it split off from:
git checkout mybranch
git rebase master
Rewind History
Undo the previous commit:
git reset HEAD~
Undo the previous two commits:
git reset HEAD~2
Heavy-Duty History Rewriting
Reorder, edit, reword, drop, split, combine history.
git rebase -i
This is interactive — it will open an editor with
a list of your commits along with instructions.
Repository Maintenance
Save disk space by clearing out stuff that's no longer important.
Clean out old deleted upstream branches:
git remote prune origin
Clean out abandoned commits, recompress files, and much more: