Git Cheat Sheet
Reference: GeekHour youtube video Get repo cover image https://opengraph.githubassets.com/1/{username}/{repository} Get code online https://raw.githubusercontent.com/benson1231/{repo-name}/{file-name}
1. Basic Git Settings
Get Git Version
git --versionInitialize a Git Repository
git initSet Author Name and Email
git config --global user.name "USER_NAME"
git config --global user.email "USER_EMAIL"
git config --global pull.rebase true
# Use rebase as the default behavior for 'git pull'
# - Prevents divergent-branch errors
# - Keeps commit history clean and linear
# - Recommended for most workflowsList Git Configurations
Add a Git Alias
2. Git Areas and File Status
Git Areas
Working Directory: The actual files on your local system that you edit.
Staging Area (Index): A place where changes are staged before committing.
Local Repository: The committed changes stored on your local machine.
Remote Repository: A repository hosted on a remote server (e.g., GitHub, GitLab).
File Status
Untracked: New files that are not yet being tracked by Git.
Modified: Files that have been edited but not yet staged.
Staged: Changes that are added to the staging area and ready to commit.
Committed: Changes that have been saved to the local repository.
Commands to Check Status
3. Basic Git Commands
Stage Files
Commit Changes
View Commit History
View Changes
Restore File to Last Commit
Reset Changes
Discard Local Changes
Use .gitignore to Ignore Files
.gitignore to Ignore Files4. Branch Commands
List and Create Branches
Switch Branches
建議使用 git switch 指令,這是較新且語意更清晰的分支切換方式(自 Git 2.23 起支援)。
備註:
git switch是專門為切換分支設計的命令,與git checkout相比語意更明確。git restore可取代git checkout HEAD FILE的用途,用於還原檔案內容。
若你使用的是 Git 2.23 以下的版本,則仍須使用 git checkout。
Merge Branches
5. Remote Commands
Clone a Repository
Remote Repository Management
Change remote URL to 'ssh'
Push Changes
Pull Changes
Fetch Changes
6. Advanced Commands
Save Changes Temporarily
List Stashed Changes
Restore Stashed Changes
Drop Stashed Changes
Garbage Collection
Remove Untracked Files
7. Git Workflow and GitFlow
GitFlow Overview
Main Branch (
main/master): Represents the stable version of the project.Development Branch (
develop): Used for ongoing development.Feature Branches (
feature): For developing individual features.Release Branches (
release): Used for preparing releases.Hotfix Branches (
hotfix): For emergency fixes on the main branch.
8. Git Rebase and References
Git Rebase
Rebase: Moves or combines commits from one branch to another to maintain a linear history.
Interactive Rebase: Allows editing, squashing, or reordering commits.
Git References Table
HEAD
Points to the current commit
HEAD~4
Refers to the 4th commit before HEAD
HEAD^
Refers to the parent of HEAD
main
The default primary branch
origin
The default name for the remote repo
Summary
This cheat sheet provides essential Git commands and concepts to help manage repositories effectively. Use it as a quick reference to streamline your Git workflow.
Last updated