Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Git in Practice
Mike McQuaid
Copyright
Brief Table of Contents
Table of Contents
Foreword
Preface
Acknowledgments
About this Book
Roadmap
About the code
Author Online
About the author
About the Cover Illustration
Part 1. Introduction to Git
Chapter 1. Local Git
1.1. Why do programmers use Git?
1.2. Initial setup
Listing 1.1. Setting a name and email address
How can you follow the listings?
Is there an example repository?
Technique 1 Creating a repository: git init
Listing 1.2. Initializing a Git repository
Where can you see the full syntax references for Git commands?
1.3. .git subdirectory
Why is the .git directory not visible?
Listing 1.3. Listing files created in a new repository
1.4. Creating a new commit: git add, git commit
Figure 1.1. Git add/commit/checkout workflow
Technique 2 Building a new commit in the index staging area: git add
Listing 1.4. Adding a file to the index
Does git add need to be run more than once?
Technique 3 Committing changes to files: git commit
Figure 1.2. A typical commit broken down into its parts
Why do the arrows point backward?
Listing 1.5. Committing changes staged in the index
What is a SHA-1 hash?
Listing 1.6. Making a second commit
1.4.1. Object store
Figure 1.3. Commit, blob, and tree objects
Should objects be interacted with directly?
1.4.2. Parent commits
Figure 1.4. Parent commit pointers
Technique 4 Viewing history: git log, gitk, gitx
Why are small commits better?
How should commit messages be formatted?
Listing 1.7. History output
Figure 1.5. Gitk on Windows 8.1
Figure 1.6. GitX-dev on OS X Mavericks
Figure 1.7. GitX history output
Figure 1.8. GitX history graph output
1.5. Rewriting history
Figure 1.9. Squashing multiple commits into a single commit
Technique 5 Viewing the differences between commits: git diff
Listing 1.8. Differences between the previous commit and the latest
1.6. Diff formats
Listing 1.9. Diffstat format
Listing 1.10. Word diff format
1.7. Refs
Figure 1.10. HEAD, master, and modified refs
Listing 1.11. Parsing refs
1.8. Summary
Chapter 2. Remote Git
Technique 6 Adding a remote repository: git remote add
Figure 2.1. Git add/commit/checkout cycle
Figure 2.2. Git add/commit/push/pull/checkout cycle
Listing 2.1. Output: creating a remote repository
What happens when the fetch and push URLs differ?
What is the default name for a remote?
2.1. Authoritative version storage
Technique 7 Pushing changes to a remote repository: git push
Listing 2.2. Output: pushing and setting an upstream branch
Figure 2.3. Local repository after git push
Figure 2.4. GitHub repository after git push
Technique 8 Cloning a remote/GitHub repository onto your local ma- achine: git clone
Listing 2.3. Output: cloning a remote repository
Listing 2.4. Output: remote repository
Figure 2.5. Local repository after git clone
Technique 9 Pulling changes from another repository: git pull
Listing 2.5. Output: pulling new changes
Why did a merge happen?
Figure 2.6. Local repository after git pull
Technique 10 Fetching changes from a remote without modifying loc- cal branches: git fetch
Listing 2.6. Output: fetching new changes
Figure 2.7. Remote repository after git fetch
Listing 2.7. Output: pull after fetch
Figure 2.8. Local repository after git fetch and then git pull
Should you use pull or fetch?
Technique 11 Creating a new local branch from the current branch: git branch
Figure 2.9. Committing without using branches
Figure 2.10. Committing to multiple branches
Listing 2.8. Output: listing branches
Figure 2.11. Local repository after git branch chapter-two
Figure 2.12. Branch pointers
Can branches be named anything?
What names should you use for branches?
Technique 12 Checking out a local branch: git checkout
Figure 2.13. Git add/commit/checkout workflow
Figure 2.14. HEAD pointer with multiple branches
Technique 13 Pushing a local branch remotely
Listing 2.9. Output: pushing and setting an upstream branch
Figure 2.15. Local repository after git push --set-upstream origin chapter-two
Technique 14 Merging an existing branch into the current branch: git merge
Figure 2.16. Merging a branch into master
Listing 2.10. Output: merging a branch
What if you try to merge the same commit into a branch multiple times?
Figure 2.17. Local repository after git merge chapter-two
2.2. Merge conflicts
Listing 2.11. Merge conflict in Git
How can conflict markers be found quickly?
2.3. Rebasing
Figure 2.18. Rebasing a branch on top of master
Technique 15 Deleting a remote branch
Why delete the branches?
Listing 2.12. Output: deleting a remote branch
Figure 2.19. Local repository after git push origin :chapter-two
Technique 16 Deleting the current local branch after merging
Figure 2.20. Local repository after git branch --delete chapter-two
Why delete the remote branch before the local branch?
2.4. Summary
Part 2. Git essentials
Chapter 3. Filesystem interactions
Technique 17 Renaming or moving a file: git mv
Listing 3.1. Output: renamed commit
What if the New Filename Already Exists?
Technique 18 Removing a file: git rm
Listing 3.2. Output: removed commit
What if a file has uncommitted changes?
Technique 19 Resetting files to the last commit: git reset
Listing 3.3. Output: hard reset
Dangers of using git reset -- hard
Technique 20 Deleting untracked files: git clean
Listing 3.4. Output: force-cleaned files
Technique 21 Ignoring files: .gitignore
Listing 3.5. Output: ignore file commit
Listing 3.6. Output: trying to add an ignored file
Technique 22 Deleting ignored files
Listing 3.7. Output: force-cleaning ignored files
Technique 23 Temporarily stashing some changes: git stash
Listing 3.8. Output: stashing uncommitted changes
Listing 3.9. List of stashes
Do you need to use git add before git stash?
Does git stash work without the save argument?
Technique 24 Reapplying stashed changes: git stash pop
Listing 3.10. Output: reapplying stashed changes
Technique 25 Clearing stashed changes: git stash clear
Technique 26 Assuming files are unchanged
Technique 27 Listing assumed-unchanged files
Listing 3.11. Output: listing assumed-unchanged files
Listing 3.12. Output: assumed-unchanged files
How do pipes, grep, and cut work?
Technique 28 Stopping assuming files are unchanged
Listing 3.13. --no-assume-unchanged output
3.1. Summary
Chapter 4. History visualization
Technique 29 Listing only certain commits
Listing 4.1. Output: filtered log
What are regular expressions?
4.1. git show
Listing 4.2. Output: showing a commit
Technique 30 Listing commits with different formatting
Why are commits structured like emails?
Listing 4.3. Output: email-formatted log
Listing 4.4. Fuller log snippet
Why do commits have an author and a committer?
4.2. Custom output format
Listing 4.5. Custom prose log format
4.3. Releasing logs: git shortlog
Listing 4.6. Output: short log
4.4. The ultimate log output
Listing 4.7. Output: graph log
Figure 4.1. GitX graph output
Listing 4.8. Graph log merge commit snippet
Technique 31 Showing who last changed each line of a file: git blame
Listing 4.9. Blame output
Technique 32 Finding which commit caused a particular bug: git bisect
Listing 4.10. Output: first good bisect
Listing 4.11. Output: final bad bisect
Figure 4.2. GitX bisect output before git bisect reset
Table 4.1. Bisect binary search performance
Listing 4.12. Output: bisect log
4.5. Automating git bisect
Listing 4.13. Output: bisect run
How can you stop git bisect from overwriting your test?
4.6. Summary
Chapter 5. Advanced branching
Technique 33 Merging branches and always creating a merge commit
Why would you want to force the creation of a merge commit?
Listing 5.1. Output: fast-forward branch commit
Figure 5.1. Local repository before merge without a merge commit
Listing 5.2. Output: forced merge commit
Figure 5.2. Local repository after git merge --no-ff chapter-spacing
5.1. Merge strategies
Technique 34 Resolving a merge conflict
Listing 5.3. Output: committing separate files
Listing 5.4. Output: committing chapter 1 content
Listing 5.5. Current state on branches
Figure 5.3. Local repository before merge-conflict resolution
Listing 5.6. Output: merge with a conflict
Listing 5.7. Before merge-conflict resolution
Listing 5.8. After merge-conflict resolution
Listing 5.9. Output: committing the merge conflict
Listing 5.10. Output: merge commit
Figure 5.4. Local repository after merge-conflict resolution
5.2. Using a graphical merge tool
Figure 5.5. opendiff merge-conflict resolution
Who should resolve merge conflicts?
Technique 35 Resolving each merge conflict only once: git rerere
Listing 5.11. rerere merge-conflict storage
Listing 5.12. rerere merge-conflict retrieval
How can you make git rerere forget an incorrect resolution?
Technique 36 Creating a tag: git tag
Figure 5.6. Local repository before git tag
Listing 5.13. Output: tag listing
Figure 5.7. Local repository after git tag
How can you update remote tags?
Technique 37 Generating a version number based on previous tags: git describe
Listing 5.14. Output: committing the preface
Listing 5.15. Output: tag describe
Technique 38 Adding a single commit to the current branch: git cherry-pick
Listing 5.16. Output: committing the release branch
Listing 5.17. Output: commit cherry-pick
Why does the SHA-1 change on a cherry-pick?
How many commits should you cherry pick?
When would you sign off a commit?
Technique 39 Reverting a previous commit: git revert
Listing 5.18. Output: revert
Listing 5.19. Output: revert show
Technique 40 Listing what branches contain a commit: git cherry
Listing 5.20. Output: committing the release preface
Listing 5.21. Output: cherry
5.3. Summary
Chapter 6. Rewriting history and disaster recovery
Technique 41 Listing all changes including history rewrites: git reflog
Listing 6.1. Reflog output
Figure 6.1. GitX reflog comparison output
Are reflogs shared between repositories?
6.1. Avoiding and recovering from disasters
Technique 42 Resetting a branch to a previous commit: git reset
Listing 6.2. Output: commit to be reset
Figure 6.2. Commit to be reset in GitX
Listing 6.3. Output: resetting a commit
Figure 6.3. GitX after resetting a commit
Listing 6.4. Reflog output after resetting a commit
Figure 6.4. Restored commit in GitX
When are commits removed from the reflog?
What’s the difference between git reset and git checkout?
Technique 43 Rebasing commits on top of another branch: git rebase
Listing 6.5. Output: commit to be rebased
Figure 6.5. Newly created inspiration branch
Listing 6.6. Rebase output
Figure 6.6. Rebased inspiration branch
Listing 6.7. Reflog output after rebase
Listing 6.8. Output: rebase conflict
Technique 44 Rebasing commits interactively: git rebase --interactive
Listing 6.9. Interactive rebase git-rebase-todo file
Listing 6.10. Rebase reorder and fixup
Listing 6.11. Output: interactive rebase
Figure 6.7. Interactively rebased inspiration branch
Technique 45 Pulling a branch and rebasing commits: git pull --rebase
Listing 6.12. Output: commit to be reset
Figure 6.8. Commit before pull rebase
Listing 6.13. Output: rebase pull
Figure 6.9. Commit after pull rebase
Technique 46 Rewriting history on a remote branch: git push --force
Listing 6.14. Output: rewritten history push failure
Listing 6.15. Output: rewritten history push
Technique 47 Rewriting the entire history of a branch: git filter-branch
Figure 6.10. GitX before filter-branch
Listing 6.16. Output: filter-branch file removal
Figure 6.11. GitX after filter-branch
6.2. Summary
Part 3. Advanced Git
Chapter 7. Personalizing Git
Technique 48 Setting the configuration for all repositories
Where is the $HOME directory?
Technique 49 Setting the configuration for a single repository
Listing 7.1. Sample .git/config file
7.1. Useful configuration settings
7.1.1. Colored output in Git
7.1.2. Git 2.0’s push defaults
7.1.3. Pruning branches automatically
Does pruning affect local or remote branches?
7.1.4. Ignoring files across all repositories: global ignore file
7.1.5. Displaying help output in your web browser
7.1.6. Storing passwords in the OS X keychain
7.1.7. Storing arbitrary text in Git configuration
7.1.8. Autocorrecting misspelled commands
Technique 50 Aliasing commands
Listing 7.2. Output: ultimate log alias
7.2. Sharing your Git (or other) configuration between machines
Technique 51 Showing the current branch in your terminal prompt
Figure 7.1. Shell branch output
7.3. Summary
Chapter 8. Vendoring dependencies as submodules
8.1. When are submodules useful?
What is git subtree?
Technique 52 Adding a git submodule: git submodule add
Listing 8.1. Output: creating a new submodule repository
Listing 8.2. Output: adding a submodule
Listing 8.3. Output: git show submodule
Technique 53 Showing the status of submodules: git submodule status
Listing 8.4. Output: submodule status
Technique 54 Updating and initializing all submodules: git submod- dule update --init
Listing 8.5. Output: removing a submodule clone
Listing 8.6. Output: initializing and updating a submodule
Technique 55 Running a command in every submodule: git submodule foreach
Listing 8.7. Output: submodule loop
8.2. Summary
Chapter 9. Working with Subversion
Technique 56 Importing an SVN repository into a Git repository
How can you install git-svn?
Listing 9.1. Output: cloning a Subversion repository
Listing 9.2. Output: Subversion commit message in a Git repository
9.1. Subversion branches and tags
Listing 9.3. Output: clone branch detection
Listing 9.4. Output: remote branches
Why are there tags in the branch output?
9.2. Subversion ignore rules
Listing 9.5. Ouput: Subversion ignore rules
9.3. Updating a Subversion repository
Figure 9.1. Git SVN add/commit/dcommit/rebase/checkout cycle
Listing 9.6. Output: no new Subversion revisions
Listing 9.7. Output: one new Subversion revision
9.4. Subversion authors and committers
Listing 9.8. Subversion commit metadata in a Git repository
9.5. Viewing a Subversion repository in GitX
Figure 9.2. GitX on import Subversion repository
9.6. Migrating a Subversion repository to Git
Technique 57 Committing and pushing to an SVN repository from a Git repository
Listing 9.9. Output: Subversion push
9.7. Local branching and tagging
How should you collaborate when using Git SVN?
Technique 58 Accessing a GitHub repository with Subversion
Listing 9.10. Partial output: checking out a GitHub repository with Subversion
9.8. Summary
Chapter 10. GitHub pull requests
10.1. What are pull requests and forks?
10.2. Interacting with GitHub from the command-line: gh
Technique 59 Making a pull request in the same repository: gh pull-request
Listing 10.1. Output: creating a pull request with gh
How do you use branches with pull requests?
Figure 10.1. New pull request
How can you view a pull request without whitespace changes?
Technique 60 Making a pull request from a forked repository: gh fork
Listing 10.2. Output: creating a fork and a pull request with gh
Technique 61 Merging a pull request from the same repository
Listing 10.3. Output: merging a pull request
Figure 10.2. Merged pull request
How can you close a pull request without a merge or issue from a commit?
Technique 62 Merging a pull request from a forked repository: gh merge
Listing 10.4. Output: merging a pull request from a forked repository
10.3. Summary
Chapter 11. Hosting a repository
Technique 63 Initializing a local repository in a server hosting - format: git init --bare
Figure 11.1. Typical repository layout
Listing 11.1. Output: initializing a bare repository
How should you name bare repositories?
Figure 11.2. Bare repository layout
Technique 64 Mirroring a repository: git clone --mirror
Listing 11.2. Output: cloning a mirror
Technique 65 Sharing a repository with other users on the same ne- etwork: git daemon
Listing 11.3. Output: daemon
Technique 66 Displaying a repository in a browser: git instaweb
How can you install gitweb?
Figure 11.3. Gitweb projects
Figure 11.4. Gitweb summary
11.1. Advanced Git hosting
11.2. Summary
Part 4. Git best practices
Chapter 12. Creating a clean history
12.1. Writing a good commit message
Listing 12.1. Good commit message format
12.2. Building a commit from parts of files: git add --patch
Listing 12.2. Patch add output
Listing 12.3. Output: patch split
Listing 12.4. Output: status after patch add
Listing 12.5. Output: patch commit
12.3. Graphically building a commit from parts of files
12.3.1. Graphically building a commit in GitX
Figure 12.1. GitX stage mode
Figure 12.2. GitX staged hunk
Figure 12.3. GitX stage-mode commit
12.3.2. Graphically building a commit in Git Gui
Figure 12.4. Git Gui on Windows 8.1
Figure 12.5. Git Gui staged line
12.4. Avoiding whitespace issues: git diff --check
Listing 12.6. Output: diff whitespace check
12.5. Summary
Chapter 13. Merging vs. rebasing
13.1. CMake’s workflow
Figure 13.1. CMake repository history
Figure 13.2. CMake branch/merge workflow
13.1.1. Workflow commands
13.2. Homebrew’s workflow
Figure 13.3. Homebrew repository history
Figure 13.4. Homebrew’s branch/rebase/squash workflow
13.2.1. Workflow commands
13.3. CMake workflow pros and cons
13.4. Homebrew workflow pros and cons
Why is a readable history important for Homebrew?
13.5. Picking your strategy
What is the author’s preferred approach?
13.6. Summary
Chapter 14. Recommended team workflows
14.1. GitHub Flow
Figure 14.1. GitHub Flow
14.1.1. Pros and cons
14.2. Git Flow
Figure 14.2. Git Flow
14.2.1. Pros and cons
14.3. Mike Flow
14.3.1. Mike Flow Single
Figure 14.3. Mike Flow Single
14.3.2. Mike Flow Multiple
Figure 14.4. Mike Flow Multiple
14.3.3. Pros and cons
14.4. Which workflow is for you?
14.5. Summary
Appendix A. Git installation
Why are there different versions of Git in this appendix?
A.1. How to install and run Git on Apple OS X
Figure A.1. git --version in Terminal.app on OS X Mavericks
A.1.1. Installing Git on Apple OS X Mavericks or newer
Figure A.2. OS X Mavericks Git installation
A.1.2. Installing Git on Apple OS X Mountain Lion or older
A.2. How to install and run Git on Linux or Unix
Figure A.3. git --version in XFCE Terminal on Debian 7.2 (Wheezy)
A.3. How to install and run Git on Microsoft Windows
Figure A.4. git --version in Git Bash on Windows 8.1
A.4. Verifying that Git has installed correctly
Appendix B. Creating a GitHub account and repository
B.1. Signing up for a GitHub account
Figure B.1. Join GitHub form
Figure B.2. Choosing a GitHub plan
B.2. Creating a new repository on GitHub
Figure B.3. Dashboard buttons to create a new GitHub repository
Figure B.4. Creating a new GitHub repository
Figure B.5. A new GitHub repository
Appendix C. Commented Git configuration
Listing C.1. ~/.gitconfig
Listing C.2. ~/.gitconfig-user
Listing C.3. ~/.gitignore
Listing C.4. ~/.gitattributes
Appendix D. Why use version control?
D.1. Handling changes
Figure D.1. Versioning with multiple files
Figure D.2. Versioning with a version control system
D.2. Version control workflow
Figure D.3. Git add/commit/checkout workflow
D.3. Version control for programmer collaboration
Figure D.4. Committing on multiple branches
Index
A
B
C
D
E
F
G
H
I
K
L
M
N
O
P
R
S
T
U
V
W
Y
Index of Git Methods
List of Figures
List of Tables
List of Listings
← Prev
Back
Next →
← Prev
Back
Next →