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 --version

Initialize a Git Repository

git init

Set Author Name and Email

git config --global user.name "USER_NAME"
git config --global user.email "USER_EMAIL"

List Git Configurations

git config --list

Add a Git Alias

git config --global alias.glop "log --pretty=format:'%h %s' --graph"
# Use alias to view commit history:
git glop

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

git status              # Check file status

3. Basic Git Commands

Stage Files

git add FILE_NAME        # Add a specific file
git add *.md             # Add all Markdown files
git add .                # Add all files

Commit Changes

git commit -m "UPDATE_MESSAGE"   # Commit with a message
git commit --amend               # Amend the last commit

View Commit History

git log                     # Show detailed commit history
git log --oneline           # Show each commit in one line
git log --oneline --graph   # Show commit history as a graph

View Changes

git diff FILE_NAME                  # View changes in a file
git diff COMMIT_SHA -- FILE_NAME    # Compare a specific commit with a file

Restore File to Last Commit

git restore FILE_NAME

Reset Changes

git 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 file

Discard Local Changes

git checkout COMMIT_SHA -- FILE_NAME

Use .gitignore to Ignore Files

*.png   # Ignore all .png files

4. Branch Commands

List and Create Branches

git 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'

Switch Branches

建議使用 git switch 指令,這是較新且語意更清晰的分支切換方式(自 Git 2.23 起支援)。

# 建立並切換到新分支
git switch -c BRANCH_NAME

# 切換到已存在的分支
git switch main

# 還原目前分支的特定檔案
git restore FILE_NAME

備註:

  • git switch 是專門為切換分支設計的命令,與 git checkout 相比語意更明確。

  • git restore 可取代 git checkout HEAD FILE 的用途,用於還原檔案內容。

若你使用的是 Git 2.23 以下的版本,則仍須使用 git checkout

Merge Branches

git merge BRANCH_NAME

5. Remote Commands

Clone a Repository

git clone https://github.com/benson1231/git_test.git

Remote Repository Management

git remote -v                       # View remote repositories
git remote add origin REPO_NAME     # Add a remote repository
git remote remove REPO_NAME         # Remove a remote repository

Push Changes

git push origin BRANCH_NAME         # Push to a remote branch
git push origin --tags              # Push local tags to remote

Pull Changes

git pull origin BRANCH_NAME         # Pull the latest changes and merge

Fetch Changes

git fetch                           # Fetch updates without merging

6. Advanced Commands

Save Changes Temporarily

git 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 ones

List Stashed Changes

git stash list                   # View all stashes

Restore Stashed Changes

git 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 stash

Drop Stashed Changes

git stash drop stash@{2}         # Remove a specific stash
git stash clear                  # Remove all stashes

Garbage Collection

git gc

Remove Untracked Files

git clean -f

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

git rebase BASE_BRANCH
  • 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

Git References Table

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


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