Git
Notes for ProfessionalsGit®
Notes for Professionals
100+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Git® group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
...................................................................................................................................................................................
1
Chapter 1: Getting started with Git
......................................................................................................................
2
Section 1.1: Create your first repository, then add and commit files
Section 1.2: Clone a repository
Section 1.3: Sharing code
Section 1.4: Setting your user name and email
Section 1.5: Setting up the upstream remote
Section 1.6: Learning about a command
Section 1.7: Set up SSH for Git
Section 1.8: Git Installation
.....................................................................................................................................
..............................................................................................................................................
........................................................................
2
..........................................................................................................
..............................................................................................................
....................................................................................................................
......................................................................................................................................
............................................................................................................................................
.........................................................................................................................
.....................................................................................................................................
...............................................................................................................................................
...........................................................................................................................................
..............................................................................................................................................
...............................................................................................................................................
Chapter 2: Browsing the history
Section 2.1: "Regular" Git Log
Section 2.2: Prettier log
Section 2.3: Colorize Logs
Section 2.4: Oneline log
Section 2.5: Log search
Section 2.6: List all contributions grouped by author name
Section 2.7: Searching commit string in git log
Section 2.8: Log for a range of lines within a file
Section 2.9: Filter logs
Section 2.10: Log with changes inline
Section 2.11: Log showing commited files
Section 2.12: Show the contents of a single commit
Section 2.13: Git Log Between Two Branches
Section 2.14: One line showing commiter name and time since commit
...................................................................................
........................................................................................................
.....................................................................................................
........................................................................................................................
.................................................................................................................
................................................................................................
...........................................................................................................
..............................................................
16
.....................................................................................................................
.....................................................................................................................
.....................................................................................................................
..............................................................................................................................
.......................................................................
17
..................................................................................................
.................................................................................................................................................
Chapter 3: Working with Remotes
Section 3.1: Deleting a Remote Branch
Section 3.2: Changing Git Remote URL
Section 3.3: List Existing Remotes
Section 3.4: Removing Local Copies of Deleted Remote Branches
Section 3.5: Updating from Upstream Repository
Section 3.6: ls-remote
Section 3.7: Adding a New Remote Repository
Section 3.8: Set Upstream on a New Branch
Section 3.9: Getting Started
Section 3.10: Renaming a Remote
Section 3.11: Show information about a Specific Remote
Section 3.12: Set the URL for a Specific Remote
Section 3.13: Get the URL for a Specific Remote
Section 3.14: Changing a Remote Repository
.................................................................................................................................................
.......................................................................................................
...........................................................................................................
........................................................................................................................................
.............................................................................................................................
........................................................................................
......................................................................................................
......................................................................................................
..........................................................................................................
Chapter 4: Staging
....................................................................................................................................................
21
Section 4.1: Staging All Changes to Files
Section 4.2: Unstage a file that contains changes
Section 4.3: Add changes by hunk
Section 4.4: Interactive add
Section 4.5: Show Staged Changes
Section 4.6: Staging A Single File
............................................................................................................................
........................................................................................................................................
...........................................................................................................................
...............................................................................................................................
...................................................................................................................
21
...................................................................................................
21
4
4
5
6
6
6
7
10
10
11
11
11
12
12
13
14
14
14
Publicité
15
15
16
17
17
17
17
18
18
18
18
19
19
20
20
20
20
21
22
22
23
Section 4.7: Stage deleted files
..................................................................................................................................
23
Chapter 5: Ignoring Files and Folders
..............................................................................................................
24
...............................................................................
...................................................................................................................
.................................................................................................................
............................................................................................................................
Section 5.1: Ignoring files and directories with a .gitignore file
Section 5.2: Checking if a file is ignored
Section 5.3: Exceptions in a .gitignore file
Section 5.4: A global .gitignore file
Section 5.5: Ignore files that have already been committed to a Git repository
Section 5.6: Ignore files locally without committing ignore rules
Section 5.7: Ignoring subsequent changes to a file (without removing it)
Section 5.8: Ignoring a file in any directory
Section 5.9: Prefilled .gitignore Templates
Section 5.10: Ignoring files in subfolders (Multiple gitignore files)
Section 5.11: Create an Empty Folder
Section 5.12: Finding files ignored by .gitignore
Section 5.13: Ignoring only part of a file [stub]
Section 5.14: Ignoring changes in tracked files. [stub]
Section 5.15: Clear already committed files, but included in .gitignore
..............................................................................................................
...............................................................................................................
.........................................................................
........................................................................................................................
.......................................................................................................
........................................................................................................
............................................................................................
.................................................................
.................................................
27
...........................................................................
............................................................
Chapter 6: Git Di
.......................................................................................................................................................
35
.....................................................................................................
..................................................................................................
..........................................................................................................
.............................................................................................................................
Section 6.1: Show dierences in working branch
Section 6.2: Show changes between two commits
Section 6.3: Show dierences for staged files
Section 6.4: Comparing branches
Section 6.5: Show both staged and unstaged changes
Section 6.6: Show dierences for a specific file or directory
Section 6.7: Viewing a word-di for long lines
Section 6.8: Show dierences between current version and last version
Section 6.9: Produce a patch-compatible di
Section 6.10: dierence between two commit or branch
Section 6.11: Using meld to see all modifications in the working directory
Section 6.12: Di UTF-16 encoded text and binary plist files
..........................................................................................
..................................................................................
.........................................................................................................
.............................................................
..........................................................................................................
........................................................................................
...........................................................
38
..................................................................................
38
Chapter 7: Undoing
....................................................................................................................................................
40
Section 7.1: Return to a previous commit
Section 7.2: Undoing changes
Section 7.3: Using reflog
Section 7.4: Undoing merges
Section 7.5: Revert some existing commits
Section 7.6: Undo / Redo a series of commits
.................................................................................................................
....................................................................................................................................
.............................................................................................................................................
.....................................................................................................................................
..............................................................................................................
.........................................................................................................
43
Chapter 8: Merging
....................................................................................................................................................
45
Section 8.1: Automatic Merging
Section 8.2: Finding all branches with no merged changes
Section 8.3: Aborting a merge
Section 8.4: Merge with a commit
Section 8.5: Keep changes from only one side of a merge
Section 8.6: Merge one branch into another
..................................................................................................................................
45
...................................................................................
45
...................................................................................................................................
.............................................................................................................................
....................................................................................
............................................................................................................
Chapter 9: Submodules
...........................................................................................................................................
47
Section 9.1: Cloning a Git repository having submodules
Section 9.2: Updating a Submodule
Section 9.3: Adding a submodule
Section 9.4: Setting a submodule to follow a branch
Section 9.5: Moving a submodule
..........................................................................................................................
..............................................................................................................................
..............................................................................................
..............................................................................................................................
.......................................................................................
Publicité
47
24
26
27
27
28
29
29
29
30
31
31
32
33
34
35
35
35
36
36
36
37
37
37
38
40
40
41
41
43
45
45
45
46
47
47
48
48
Section 9.6: Removing a submodule
.........................................................................................................................
49
Chapter 10: Committing
...........................................................................................................................................
50
..................................................................................................................
......................................................................................................................
.............................................................................................................................
...............................................................................................
...............................................................................................................
....................................................................
53
..................................................................................................................
.............................................................................................
............................................................................................................................
Section 10.1: Stage and commit changes
Section 10.2: Good commit messages
Section 10.3: Amending a commit
Section 10.4: Committing without opening an editor
Section 10.5: Committing changes directly
Section 10.6: Selecting which lines should be staged for committing
Section 10.7: Creating an empty commit
Section 10.8: Committing on behalf of someone else
Section 10.9: GPG signing commits
Section 10.10: Commiting changes in specific files
Section 10.11: Committing at a specific date
Section 10.12: Amending the time of a commit
Section 10.13: Amending the author of a commit
...................................................................................................
.............................................................................................................
........................................................................................................
.....................................................................................................
Chapter 11: Aliases
......................................................................................................................................................
57
..........................................................................................................................................
.................................................................................................................
...................................................................................................................................
Section 11.1: Simple aliases
Section 11.2: List / search existing aliases
Section 11.3: Advanced Aliases
Section 11.4: Temporarily ignore tracked files
Section 11.5: Show pretty log with branch graph
Section 11.6: See which files are being ignored by your .gitignore configuration
Section 11.7: Updating code while keeping a linear history
Section 11.8: Unstage staged files
..........................................................................................................
.....................................................................................................
.....................................................................................
.................................................
59
..............................................................................................................................
Chapter 12: Rebasing
................................................................................................................................................
61
.........................................................................................................................
.........................................................................................
................................................................................................................................
......................................................................................................
..........................................................................................................................
Section 12.1: Local Branch Rebasing
Section 12.2: Rebase: ours and theirs, local and remote
Section 12.3: Interactive Rebase
Section 12.4: Rebase down to the initial commit
Section 12.5: Configuring autostash
Section 12.6: Testing all commits during rebase
Section 12.7: Rebasing before a code review
Section 12.8: Aborting an Interactive Rebase
Section 12.9: Setup git-pull for automatically perform a rebase instead of a merge
Section 12.10: Pushing after a rebase
......................................................................................................
...........................................................................................................
...........................................................................................................
........................................................................................................................
68
.........................................
68
Chapter 13: Configuration
......................................................................................................................................
69
Section 13.1: Setting which editor to use
Section 13.2: Auto correct typos
Section 13.3: List and edit the current configuration
Section 13.4: Username and email address
Section 13.5: Multiple usernames and email address
Section 13.6: Multiple git configurations
Section 13.7: Configuring line endings
Section 13.8: configuration for one command only
Section 13.9: Setup a proxy
....................................................................................................................
.................................................................................................................................
................................................................................................
..............................................................................................................
..............................................................................................
Publicité
70
....................................................................................................................
.......................................................................................................................
.................................................................................................
.........................................................................................................................................
Chapter 14: Branching
..............................................................................................................................................
74
Section 14.1: Creating and checking out new branches
Section 14.2: Listing branches
Section 14.3: Delete a remote branch
Section 14.4: Quick switch to the previous branch
....................................................................................................................................
........................................................................................................................
...................................................................................................
...........................................................................................
74
50
51
52
53
53
54
54
55
55
55
56
56
57
57
57
58
58
60
60
61
61
63
64
64
65
65
67
69
69
70
70
71
72
72
72
75
75
76
.........................................................................................................................
Section 14.5: Check out a new branch tracking a remote branch
Section 14.6: Delete a branch locally
Section 14.7: Create an orphan branch (i.e. branch with no parent commit)
Section 14.8: Rename a branch
Section 14.9: Searching in branches
Section 14.10: Push branch to remote
Section 14.11: Move current branch HEAD to an arbitrary commit
.................................................................................................................................
..........................................................................................................................
........................................................................................................................
.........................................................................
.........................................................................
.......................................................
77
Chapter 15: Rev-List
..................................................................................................................................................
79
Section 15.1: List Commits in master but not in origin/master
...............................................................................
79
Chapter 16: Squashing
..............................................................................................................................................
80
Section 16.1: Squash Recent Commits Without Rebasing
Section 16.2: Squashing Commit During Merge
Section 16.3: Squashing Commits During a Rebase
Section 16.4: Autosquashing and fixups
Section 16.5: Autosquash: Committing code you want to squash during a rebase
........................................................................................
.......................................................................................................
.................................................................................................
....................................................................................................................
.............................................
82
Chapter 17: Cherry Picking
.....................................................................................................................................
83
Section 17.1: Copying a commit from one branch to another
Section 17.2: Copying a range of commits from one branch to another
Section 17.3: Checking if a cherry-pick is required
Section 17.4: Find commits yet to be applied to upstream
...................................................................................................
.....................................................................................
................................................................................
83
..............................................................
83
Chapter 18: Recovering
............................................................................................................................................
85
Section 18.1: Recovering from a reset
Section 18.2: Recover from git stash
Section 18.3: Recovering from a lost commit
Section 18.4: Restore a deleted file after a commit
Section 18.5: Restore file to a previous version
Section 18.6: Recover a deleted branch
.......................................................................................................................
.........................................................................................................................
...........................................................................................................
.................................................................................................
86
........................................................................................................
....................................................................................................................
Chapter 19: Git Clean
.................................................................................................................................................
88
Section 19.1: Clean Interactively
Section 19.2: Forcefully remove untracked files
Section 19.3: Clean Ignored Files
Section 19.4: Clean All Untracked Directories
.................................................................................................................................
88
.......................................................................................................
88
................................................................................................................................
...........................................................................................................
Chapter 20: Using a .gitattributes file
Publicité
.............................................................................................................
90
Section 20.1: Automatic Line Ending Normalization
Section 20.2: Identify Binary Files
Section 20.3: Prefilled .gitattribute Templates
Section 20.4: Disable Line Ending Normalization
................................................................................................
90
..............................................................................................................................
.........................................................................................................
....................................................................................................
Chapter 21: .mailmap file: Associating contributor and email aliases
Section 21.1: Merge contributers by aliases to show commit count in shortlog
............................................
91
...................................................
91
Chapter 22: Analyzing types of workflows
....................................................................................................
92
Section 22.1: Centralized Workflow
Section 22.2: Gitflow Workflow
Section 22.3: Feature Branch Workflow
Section 22.4: GitHub Flow
Section 22.5: Forking Workflow
...........................................................................................................................
..................................................................................................................................
...................................................................................................................
95
...........................................................................................................................................
.................................................................................................................................
Chapter 23: Pulling
.....................................................................................................................................................
97
Section 23.1: Pulling changes to a local repository
Section 23.2: Updating with local changes
Section 23.3: Pull, overwrite local
..................................................................................................
97
...............................................................................................................
...............................................................................................................................
76
76
77
77
77
78
80
80
80
81
84
84
85
85
86
86
87
88
88
90
90
90
92
93
95
96
98
98
Section 23.4: Pull code from remote
Section 23.5: Keeping linear history when pulling
Section 23.6: Pull, "permission denied"
.........................................................................................................................
98
...................................................................................................
98
......................................................................................................................
99
Chapter 24: Hooks
...................................................................................................................................................
100
...............................................................................................................................................
100
...................................................
101
.........................................................
Section 24.1: Pre-push
Section 24.2: Verify Maven build (or other build system) before committing
Section 24.3: Automatically forward certain pushes to other repositories
Section 24.4: Commit-msg
Section 24.5: Local hooks
Section 24.6: Post-checkout
Section 24.7: Post-commit
Section 24.8: Post-receive
Section 24.9: Pre-commit
Section 24.10: Prepare-commit-msg
Section 24.11: Pre-rebase
Section 24.12: Pre-receive
Section 24.13: Update
.......................................................................................................................................
.........................................................................................................................................
.....................................................................................................................................
........................................................................................................................................
........................................................................................................................................
..........................................................................................................................................
.........................................................................................................................................
................................................................................................................................................
.........................................................................................................................................
.......................................................................................................................
Chapter 25: Cloning Repositories
.....................................................................................................................
105
Section 25.1: Shallow Clone
Section 25.2: Regular Clone
Section 25.3: Clone a specific branch
Section 25.4: Clone recursively
Section 25.5: Clone using a proxy
......................................................................................................................................
.....................................................................................................................................
.....................................................................................................................
105
................................................................................................................................
...........................................................................................................................
Chapter 26: Stashing
...............................................................................................................................................
107
................................................................................................................................
........................................................................................................................................
....................................................................................................................
......................................................................................