Advance Git

by scmGalaxy.com

About Me !

Rajesh
Facebook Twitter LinkedIn
DevOps@RajeshKumar.xyz

The Log

git add -p

Interactively add lines of code to the next commit. You’ll be prompted with each and can approve or disapprove.

TIP

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."

What makes a good name for a branch?

  • bug/publish-all-passwords-in-clear-text
  • feature/login-screen

"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.”

zsh

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 Commit Graph

“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.”

TIP

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

Merging

“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.

“Sometimes something stupid and simple is good.”
git checkout -t origin/[branch-name]
Create a local branch that tracks a remote one.
“If a merge has meaning, it’s good to represent it in the history.”
“I don’t want Git to decide whether it can fast forward or not. I want it to fast forward based on the meaning of what I’m doing.”
git rebase can cause huge amounts of confusion if it’s done at the wrong time. It’s best to avoid it if in doubt.”

Ctrl-s Quick search (Emacs style)

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.

CONFUSING

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).

Merge

Merge

A merge creates a new commit; it doesn’t change previous history.

Rebase

Rebase

A rebase takes a range of commits and re-commits them on top of a new point; it builds new history from existing commits.

Three strategies for merging long-lived branches

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.

TIP

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.

gbrt

Show Git branches sorted by time. Use for merging and maintenance.

Find a copy of this script in the code download at PeepCode

Maintenance

git branch -d branch-name
Delete a local branch
git push :branch-name

Delete a remote branch

NOTE: There is currently no interface on GitHub for deleting remote branches.
gbrt
Show Git branches sorted by time. Use for merging and maintenance.

Find a copy of this script in the code download at PeepCode

~/bin/git_cwd_info

Enhanced Git prompt info.

Find in the code download at PeepCode or at

http://github.com/benhoskings/dot-files
git checkout -B [branch-name] [SHA]

Create a new branch at a specific SHA.

This will override any existing branch with the same name.

Conclusion

“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.”

“Even the fact that branches are called ‘branches’ is a confusing thing. They really are labels more than they are branches.”

On confusion over command
names and options

Other Useful Commands
git log --patch [filename]
Show history of a single file
git log -S'.left-round-border'
Search Git’s entire commit history for a word (or phrase).

Useful for searching for old code or previous versions instead of leaving commented code around.

git log --all --simplify-by-decoration

Show only commits that are tagged, branched, etc.

Most useful with the shorter, train tracks style of logging shown in this screencast.

git smart-pull

Avoid meaningless merge commits when updating your local copy of a repository. From the git-smart gem.

git clean [optional-path]

Delete untracked and ignored files.

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.

Questions !!

Thank You !!