git add -p
Interactively add lines of code to the next commit. You’ll be prompted with each and can approve or disapprove.
A good log format can help you understand individual commits in the context of the repository’s history.
"Always eyeball things to see if you’re in the right order of magnitude. Don’t worry about checking with too much detail, but see if things – on the back of the envelope – look right."
"If it’s something that I want to show in the history – if I want it to be represented as a merge, something that branched out and came back in – then I’ll make a branch with a short name.”
A bash-compatible shell. Notable features are an optional right-hand prompt and inline tab completion.
gem install git-smart
Provides the git smart-pull command for intelligently integrating remote commits.
“The reflog also shows a history, but it doesn’t show connections between commits. It shows a local undo history of where HEAD has been.”
If we make a commit to the master branch, the value of .git/refs/heads/master will change.
If we checkout another branch, the value of .git/HEAD will change.
Every branch, tag, and other decoration is just a reference to the ID of a specific commit (calculated with SHA, the secure hash algorithm).
“Branches don’t store anything related to the history. It’s easy to think of a branch as being some separate context in which a range of commits lives.”
“But a branch is actually only a label. A branch is just like a tag in that it just points to a single commit. The history is implied because it’s stored recursively within those commits.”
git reset --hard [SHA]
Point the current branch at a specific commit.
“All those branches are is labels. We’re looking at the same overall graph. By doing git reset --hard all we did was move the labels around. Nothing to do with the graph has changed.”
“Probably the single most important thing about understanding this stuff is that not only did the graph not change, but you actually can’t change the graph at all. All you can ever do is add to it.”
gem install omglog
Show the commit graph with live updating as commits are made, merged, and branched
“I never bother with temporary branches because you can always throw stuff away just using git reset --hard [SHA]”
git log ..[branch-name]
Show log messages in [branch-name] that are not in HEAD (the current commit).
git status --short
An easily readable summary of file changes, additions, and deletions.See the code download for a configuration that enables color.
git add --update
Stage all changes in all tracked files. All modifications in all known files will be included in the next commit.
grep -r'<<<<' *
Search for merge conflict markers.
Create a local branch that tracks a remote one.
git checkout -t origin/[branch-name]
Press Ctrl-s and start typing to search in a file. Hit ENTER to stop, or Ctrl-s again to search for the next occurrence.
Rebase doesn’t rearrange history. It adds to the graph and creates new commits. Changing the parent of any commit necessarily involves creating a new commit (and a new SHA to identify it).
A merge creates a new commit; it doesn’t change previous history.
A rebase takes a range of commits and re-commits them on top of a new point; it builds new history from existing commits.
A. Merge a branch into master
Easiest & simplest when work on a branch is complete
B. Rebase master into a branch
Risky, especially when sharing branches with other developers.
C. Merge master into branch
Most accurately reflects workflow.
Ask yourself “Does the graph communicate what happened? Or does it tell a different story?” Merging master into a branch often shows a more accurate story of the order of commits.
Show Git branches sorted by time. Use for merging and maintenance.Find a copy of this script in the code download at PeepCode
Delete a local branch
git branch -d branch-name
git push :branch-name
Delete a remote branchNOTE: There is currently no interface on GitHub for deleting remote branches.
Show Git branches sorted by time. Use for merging and maintenance.
Find a copy of this script in the code download at PeepCode
Find in the code download at PeepCode or athttp://github.com/benhoskings/dot-files
git checkout -B [branch-name] [SHA]
This will override any existing branch with the same name.
“We push all our code to our production machines using Git, and the live site is served out of a Git repository. This lets us login to the machine and know for a fact that no one has gone in there and changed some code in production.”
“Nomenclature is a big sticking point with this stuff. To be honest, I don’t really notice it anymore; I feel familiar with those commands and don’t really notice. But when you think about it, a lot of them are named quite confusingly.”
Show history of a single file
git log --patch [filename]
Search Git’s entire commit history for a word (or phrase).
git log -S'.left-round-border'
Useful for searching for old code or previous versions instead of leaving commented code around.
git log --all --simplify-by-decoration
Most useful with the shorter, train tracks style of logging shown in this screencast.
Avoid meaningless merge commits when updating your local copy of a repository. From the git-smart gem.
git clean [optional-path]
Try the -n flag for a safe preview, -d to delete directories, -f to force deletion, and -x to delete files even if they are in .gitignore.