Skip to content

Instantly share code, notes, and snippets.

@sumpygump
Last active August 29, 2015 13:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sumpygump/9597085 to your computer and use it in GitHub Desktop.
Some notes about git
Amend commit (only can amend the latest commit)
git commit --amend -m "Message"
Note that this will add anything you have staged to the commit
Retrieving old versions of a file
git checkout 4b4a5c48 -- filename.txt
Reverting a commit
git revert 4b4a5c48
This will bring up an editor to edit the commit message
Using reset to undo commits
- git reset allows us to specify where HEAD is pointing
- Use care!
- soft: will move the HEAD pointer to the specified commit, does not change
the staging index or working directory [safest]
git reset --soft 4b4a5c48
cat .git/refs/heads/master # You can see HEAD is now at specified commit
- mixed (default): changes staging index to match repository, does not change
the working directory.
git reset --mixed 4b4a5c48
- hard: changes staging index and working directory to match repository.
git reset --hard 4b4a5c48
Removing untracked files from working directory
- The git clean command will remove all untracked files hanging around
- Running with a -n will do a dry run
- Running with a -f will force it to do it
git clean -n
git clean -f
Ignoring files
- Add a .gitignore file
- Can use very basic regular expressions
* ? [aeiou] [0-9]
- Negate expressions with !
*.php <-- ignore all .php files
!index.php <-- but don't ignore index.php
- Ignore all files in a directory with trailing slash
assets/videos/
- Comment lines begin with #, blank lines are skipped
- Global ignore
# This sets the path to the global excludes file
git config --global core.excludesfile ~/.gitignore_global
Stop tracking a file
- Tell git to stop tracking a file
git rm --cached <filename>
Tracking empty directories
- Just put a file in there, either an empty .gitignore or a .gitkeep file
Ancestry tree-ish notations
- parent commit: HEAD^, acf87504^, master^, HEAD~1 HEAD~
- grandparent: HEAD^^, HEAD~2
- great-grandparent: HEAD^^^, HEAD~3
- git ls-tree <tree-ish>
Commit log
- git log
- git log --oneline
- git log --oneline -3
- git log --since="2012-06-20"
- git log --until="2012-06-20"
- git log --since="2 weeks ago" --until="3 days ago"
- git log --since=2.weeks --until=3.days
- git log --author="Jansen"
- git log --grep="temp"
- git log --oneline 2907d12..acf8750
- git log 2907d12.. index.html
- git log -p
-p option shows a patch for each log entry
- git log --stat
Shows the stats (files affected)
- git log --summary
Shows longer descriptions
- git log --format=...
- git log --graph
- git log --oneline --graph --all --decorate
Viewing commits
- git show cdae0ed
- git show --format=oneline HEAD
Comparing commits
- git diff --stat --summary 432432..HEAD
- git diff -b
Ignore space changes
- git diff -w
Ignore all whitespace
Comparing branches
- git diff master..new_feature
- git diff --color-words master..new_feature
- git branch --merged
Shows all branches that are completely included in current branch
Renaming branches
- git branch --move new_feature seo_title
Deleting branches
- git branch -d branch_to_delete
Customizing prompt
- this relies on git-completion.bash
- export PS1='\W$(__git_ps1 "(%s)") > '
Strategies to avoid merge conflicts
- keep lines short
- keep commits small and focused
- beware stray edits to whitespace
- merge often
- track changes to master as you go (bring changes into your branch)
Stash
- git stash save "changed page title"
- git stash list
- git stash show stash@{0}
- git stash show -p stash@{0}
- git stash pop # pop will remove it from the stash
- git stash apply # apply will leave a copy of the patch in the stash stack
- git stash drop stash@{0}
- git stash clear # Clears out entire stash
Remotes
- git remote # Show a list of remotes
- git remote -v # Show a list of remotes with fetch/push URLs
- git remote add <alias> <url>
- git remote rm <alias> # Remove remote
- git push -u origin master # The -u (set-upstream) sets up branch tracking
- git fetch <alias> # Fetch all changes from remote server
- git branch newname origin/newbranch # Create a new branch from origin branch
- git push origin :branchname # Delete a branch on a remote
- git push origin --delete branchname # Another way to delete a remote branch
Tips about fetching
- always fetch before you work
- fetch before you push
- fetch often
Update a fork from upstream
- git remote add upstream https://github.com/whoever/whatever.git
- git fetch upstream
- git checkout master
- git rebase upstream/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment