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}
git --versiongit initgit 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 workflowsgit config --listgit config --global alias.glop "log --pretty=format:'%h %s' --graph"
# Use alias to view commit history:
git glop- 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).
- 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.
git status # Check file statusgit add FILE_NAME # Add a specific file
git add *.md # Add all Markdown files
git add . # Add all filesgit commit -m "UPDATE_MESSAGE" # Commit with a message
git commit --amend # Amend the last commitgit log # Show detailed commit history
git log --oneline # Show each commit in one line
git log --oneline --graph # Show commit history as a graphgit diff FILE_NAME # View changes in a file
git diff COMMIT_SHA -- FILE_NAME # Compare a specific commit with a filegit restore FILE_NAMEgit reset --hard COMMIT_SHA # Completely undo a commit and delete changes
git reset --soft COMMIT_SHA # Undo the last commit but keep changes
git reset --mixed COMMIT_SHA # Undo the commit but retain changes (unstaged)
git reset HEAD FILE_NAME # Unstage a filegit checkout COMMIT_SHA -- FILE_NAME*.png # Ignore all .png filesgit branch # List branches
git branch BRANCH_NAME # Create a branch
git branch -d BRANCH_NAME # Delete a branch
git branch -M main # Rename the branch to 'main'建議使用 git switch 指令,這是較新且語意更清晰的分支切換方式(自 Git 2.23 起支援)。
# 建立並切換到新分支
git switch -c BRANCH_NAME
# 切換到已存在的分支
git switch main
# 還原目前分支的特定檔案
git restore FILE_NAMEgit switch是專門為切換分支設計的命令,與git checkout相比語意更明確。git restore可取代git checkout HEAD FILE的用途,用於還原檔案內容。
若你使用的是 Git 2.23 以下的版本,則仍須使用 git checkout。
git merge BRANCH_NAMEgit clone https://github.com/benson1231/git_test.gitgit remote -v # View remote repositories
git remote add origin REPO_NAME # Add a remote repository
git remote remove REPO_NAME # Remove a remote repositorygit remote set-url origin git@github.com:{USER}/{REPO_NAME}.gitgit push origin BRANCH_NAME # Push to a remote branch
git push origin --tags # Push local tags to remotegit pull origin BRANCH_NAME # Pull the latest changes and mergegit fetch # Fetch updates without merginggit stash save "message" # Save changes with a description
git stash save -u "message" # Save tracked and untracked files
git stash save -a "message" # Save all files, including ignored onesgit stash list # View all stashesgit stash apply # Restore the most recent stash without removing it
git stash pop # Restore and remove the most recent stash
git stash pop stash@{2} # Restore and remove a specific stashgit stash drop stash@{2} # Remove a specific stash
git stash clear # Remove all stashesgit gcgit clean -f- 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.
git rebase BASE_BRANCHgit config --global pull.rebase true- Rebase: Moves or combines commits from one branch to another to maintain a linear history.
- Interactive Rebase: Allows editing, squashing, or reordering commits.
git rebase -i BASE_BRANCH
| Reference | Description |
|---|---|
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 |
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.