Prev
|
Contents
|
Next
Beej's Guide to Git
Brian “Beej Jorgensen” Hall
v1.2.6, Copyright © June 12, 2025
1
Foreword
1.1
Audience
1.2
Official Homepage
1.3
Email Policy
1.4
Mirroring
1.5
Note for Translators
1.6
Copyright and Distribution
1.7
Dedication
2
Git Basics
2.1
What is Git?
2.1.1
Definitions
2.2
What is GitHub?
2.3
What is GitHub?
2.4
The Most Basic Git Workflow
2.4.1
Definitions
2.5
What is Cloning?
2.5.1
Definitions
2.6
How Do Clones Interact?
2.7
Actual Git Usage
2.7.1
Step 0: One-time Setup
2.7.2
Step 1: Clone an Existing Repo
2.7.3
Step 2: Make Some Local Changes
2.7.4
Step 3: Add Changes to the Stage
2.7.5
Step 4: Commit those Changes
2.7.6
Step 5: Push Your Changes to the Remote Repo
3
GitHub: How To Use It
3.1
Making a GitHub Account
3.2
Creating a New Repo on GitHub
3.3
Authentication
3.3.1
GitHub CLI
3.3.2
SSH Keys
3.3.3
Using Personal Access Tokens
3.4
Make a Local Clone of the Repo
3.4.1
Cloning from GitHub with GitHub CLI
3.4.2
Cloning from GitHub with SSH Keys
3.5
Make Changes and Push!
3.6
Collaboration on GitHub
4
The Git Log and
HEAD
4.1
An Example Log
4.2
What’s in the log?
4.3
The
HEAD
Reference
4.4
Going Back In Time and Detached
HEAD
4.5
The Old Command:
git checkout
4.6
Commits Relative to
HEAD
5
Branches and Fast-Forward Merges
5.1
What is a Branch?
5.2
A Quick Note about
git pull
5.3
HEAD
and Branches
5.4
Listing All Your Branches
5.5
Creating a Branch
5.6
Make Some Commits on a Branch
5.7
Merging: Fast-Forward
5.8
Deleting a Branch
6
Merging and Conflicts
6.1
An Example of Divergent Branches
6.2
Merging Divergent Branches
6.3
Merge Conflicts
6.4
What a Conflict Looks Like
6.5
Why Merge Conflicts Happen
6.6
Merging with IDEs or other Merge Tools
6.7
Merge Big Ideas
7
Using Subdirectories with Git
7.1
Repos and Subdirectories
7.1.1
What about Subprojects?
7.2
Accidentally Making a Repo in your Home Directory
7.3
Empty Subdirectories in Repos
8
Ignoring Files with
.gitignore
8.1
Adding a
.gitignore
File
8.2
Can I Specify Subdirectories in
.gitignore
?
8.3
Where do I Put the
.gitignore
?
8.4
Wildcards
8.5
Negated
.gitignore
Rules
8.6
How To Ignore All Files Except a Few?
8.7
Getting Premade
.gitignore
Files
9
Remotes: Repos in Other Places
9.1
Remote and Branch Notation
9.2
Getting a List of Remotes
9.3
Changing a Remote’s URL
9.4
Adding a Remote
10
Remote Tracking Branches
10.1
Branches on Remotes
10.2
Listing Remote Tracking Branches
10.3
Pushing to a Remote
10.4
Making a Branch and Pushing to Remote
10.5
Deleting Remote Tracking Branches
10.5.1
Fetching Deleted Remote Branches
10.5.2
Deleting Your Remote Tracking Branch
10.5.3
Deleting a Branch on a Remote
10.6
Multiple Remotes
11
File States
11.1
What States Can Files in Git Be In?
11.2
Unmodified to Untracked
11.3
Files In Multiple States
12
Comparing Files with Diff
12.1
Basic Usage
12.2
Diffing the Stage
12.3
More Diff Fun
12.3.1
Diff Any Commits or Branches
12.3.2
Diffing Order
12.3.3
Diffing with Parent Commit
12.3.4
More Context
12.3.5
Just the File Names
12.3.6
Ignoring Whitespace
12.3.7
Just Certain Files
12.3.8
Inter-branch Diffs
12.4
Difftool
13
Renaming and Removing Files
13.1
Renaming Files
13.2
Unrenaming Files from the Stage
13.3
Removing Files
13.4
Unremoving Files from the Stage
13.5
Unremoving Files from Earlier Commits
13.6
A Note on Removing Secrets
14
Collaboration across Branches
14.1
Communication and Delegation
14.2
Approach: Everyone Uses One Branch
14.3
Approach: Everyone Uses Their Own Branch
14.4
Approach: Everyone Merges to the Dev Branch
15
Rebasing: Moving Commits
15.1
Contrasted to Merging
15.2
How it Works
15.3
When Should I Do This?
15.4
Pulling and Rebasing
15.5
Conflicts
15.6
Squashing Commits
15.6.1
Squash versus Fixup
15.7
Multiple Conflicts in the Rebase
16
Stashing: Temporarily Set Changes Aside
16.1
Example
16.2
The Stash Stack
16.3
Conflicts
16.4
Stashing New Files
17
GitHub: Forking and Pull Requests
17.1
Making a Fork
17.2
Making Your Changes
17.3
Syncing the Upstream with Your Fork
17.4
Making a Pull Request
17.5
Flipside: Merging a Pull Request
17.6
Making Many Pull Requests with Branches
17.7
Deleting a Pull Request
17.8
Syncing on the Command Line
18
Reverting: Undoing Commits
18.1
Performing the Revert
18.2
Revert Conflicts
18.3
Reverting Multiple Commits
19
Reset: Moving Branches Around
19.1
Soft Reset
19.2
Mixed Reset
19.3
Hard Reset
19.4
Reset to a Divergent Branch
19.5
Resetting Files
19.6
Pushing Branch Changes to a Remote
19.6.1
Forcing the Push
19.6.2
Example: Rewrite Public History
19.6.3
Example: Receiving Rewritten History
19.7
Resetting Without Moving
HEAD
19.8
Resetting to Remove Credentials
20
The Reference Log, “reflog”
20.1
What Can We Use It For?
20.2
Looking Back at an Orphan Commit
20.3
Reflog Selectors
21
Patch Mode: Applying Partial Changes
21.1
Adding Files in Patch Mode
21.2
Resetting Files in Patch Mode
21.3
Other Patch Mode Commands
22
Cherry-Pick: Bringing in Specific Commits
22.1
Cherry-Pick Example
22.2
Cherry-Pick Conflicts
23
Who’s to Blame for this Code?
23.1
Fancier Blaming
24
Configuration
24.1
Local Configuration
24.2
Listing the Current Config
24.3
Getting, Setting, and Deleting Variables
24.4
Some Popular Variables
24.5
Editing the Config Directly
24.6
Conditional Configuration
24.7
Older Git Versions
25
Git Aliases
25.1
Creating an Alias
25.2
Displaying Aliases
25.3
Some Neat Sample Aliases
25.4
Seeing Git’s Alias Expansion
26
Changing Identity
26.1
Changing the User Configuration Variables
26.2
Changing the SSH Authentication Key
26.3
Changing your GPG Signing Key
26.4
Changing your SSH Signing Key
27
Amending Commits
27.1
Amending the Commit Message
27.2
Adding some Files to the Commit
28
Difftool
28.1
Configuring
28.2
Available Difftools
29
Mergetool
29.1
Merge Tool Operations
29.2
Some Example Merge Tools
29.3
Using Vimdiff as a Merge Tool
29.4
Backing up the Originals
30
Submodules
30.1
Using a Repo with Submodules
30.2
Creating a Submodule
30.3
Setting the Commit for the Submodule
30.4
Getting Submodule Latest
30.5
Updating the Actual Submodule Itself
30.5.1
Modify the Submodule Repo Elsewhere
30.5.2
Modify the Submodule Repo in the Submodule Directory
30.6
Getting the Submodule Status
30.7
Some Behind the Scenes
30.8
Deleting a Submodule
31
Tags
31.1
Lightweight Tags
31.2
Annotated Tags
31.3
Pushing Tags
31.4
Deleting Tags
32
Worktrees
32.1
Worktree Rules and Regulations
32.2
Making a New Worktree
32.3
Removing a Worktree
32.4
Listing Worktrees
32.5
Worktrees and Detached
HEAD
33
Appendix: Making a Playground
33.1
Cloning Bare Repos
33.2
Automating Playground Builds
34
Appendix: Getting Out of Editors
35
Appendix: Errors and Scary Messages
35.1
Detached Head
35.2
Upstream Branch Name Doesn’t Match Current
35.3
Current Branch Has No Upstream Branch
36
Appendix: Just Enough Vim
36.1
Launching Vim
36.2
Modal Editing
36.3
Going Between Modes
36.4
Moving the Cursor
36.5
More Modes
36.6
Command Line Mode
36.7
Exiting Vim
36.8
Learn More
37
Appendix: Other References
38
Quick Reference
38.1
Glossary
38.2
File States
38.3
Configuration
38.3.1
Set identity
38.3.2
Set default branch
38.3.3
Set default pull behavior to merge or rebase
38.3.4
Set default editor, difftool, and mergetool
38.3.5
Colorful Git output
38.3.6
Autocorrect
38.3.7
Newline translation
38.3.8
Aliases
38.4
Creating and Cloning Repos
38.5
Adding, Renaming, Deleting, Committing
38.6
Getting Status
38.7
Getting a Diff
38.8
Branches
38.9
Pulling and Pushing, and Fetching
38.10
Merging
38.11
Remotes
38.12
Ignoring Files
38.13
Rebasing
38.14
Stashing
38.15
Reverting
38.16
Resetting
38.17
The Reflog
38.18
Cherry-pick
38.19
Blame
38.20
Submodules
38.21
Tags
38.22
Worktrees
Prev
|
Contents
|
Next