https://github.com/yatingxu-ds/git-github-commands
This document includes frequently used Git commands and GitHub workflows, tailored to help developers effectively manage their local and remote repositories. 本文件包含常用的 Git 命令和 GitHub 工作流,旨在帮助开发者有效管理本地和远程仓库
https://github.com/yatingxu-ds/git-github-commands
git git-commands github-config go guide
Last synced: 10 months ago
JSON representation
This document includes frequently used Git commands and GitHub workflows, tailored to help developers effectively manage their local and remote repositories. 本文件包含常用的 Git 命令和 GitHub 工作流,旨在帮助开发者有效管理本地和远程仓库
- Host: GitHub
- URL: https://github.com/yatingxu-ds/git-github-commands
- Owner: YatingXu-DS
- Created: 2025-06-28T12:06:35.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-28T12:20:54.000Z (12 months ago)
- Last Synced: 2025-06-28T13:51:59.504Z (12 months ago)
- Topics: git, git-commands, github-config, go, guide
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
1. [SSH Configuration](#ssh-configuration)
2. [Local Status Management](#local-status-management)
1. [Three States](#21-three-states)
2. [Stage, Commit, and Undo Change](#22-stage-commit-and-undo-change)
3. [Temporarily Save Work with Git Stash](#23-temporarily-save-work-with-git-stash)
3. [Local Branch Management](#local-branch-management)
1. [Create and Track a New Branch](#31-create-and-track-a-new-branch)
2. [Pull and Track a Remote Branch](#32-pull-and-track-a-remote-branch)
3. [View and Switch Branches](#33-view-and-switch-branches)
4. [Merge and Delete Branches](#34-merge-and-delete-branches)
4. [Local Git → Remote GitHub](#local-git-remote-github)
1. [Push a New Project](#41-push-a-new-project)
2. [Push Local Updates](#42-push-local-updates)
5. [Remote GitHub → Local Git](#remote-github-local-git)
1. [Clone a Repository](#51-clone-a-repository)
2. [Pull Remote Changes](#52-pull-remote-changes)
6. [Rollback Pushed Changes](#rollback-pushed-changes)
7. [Conflict Handling](#conflict-handling)
# 1. SSH Configuration
```shell
git --version # Check Git installation
# If not installed, install Git on macOS
xcode-select --install
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # Generate SSH key
eval "$(ssh-agent -s)" # Start SSH agent
ssh-add ~/.ssh/id_rsa # Add private key
cat ~/.ssh/id_rsa.pub # View public key, copy and add it to GitHub
# ⬇️ Copy the full public key and add it to GitHub
# GitHub → Settings → SSH and GPG keys → New SSH key → Save
ssh -T git@github.com # Test GitHub connection
# Output: "Hi username! You've successfully authenticated..."
```
# 2. Local Status Management
## 2.1. Three States
```shell
git status # View current state: red = modified, green = staged, committed files not shown
```
| Area | Status Description |
| ----------------- | ----------------------------------------------------------------------------------------------------------------- |
| Working Directory | Modified files exist in the working directory automatically.
Git does **not** record them. |
| Staging Area | Files from the working directory are staged.
Git **records a snapshot**, but not permanently. |
| Local Repository | Staged files are formally committed to the local repository.
Git **creates a commit object with a unique ID**. |
## 2.2. Stage, Commit, and Undo Change
```shell
# Working Directory → Staging Area
git add .
git add
# Discard the Staging Area
git restore --staged
# Overwrite the Working Directory with the version from:
# (1) Staging Area if staged, otherwise (2) HEAD (last commit)
git restore
# Staging Area → Local Repository
git commit -m "your message"
# View commit history
git log --oneline
# Discard the last commit
git reset --soft HEAD^
# Discard the last commit(A) and clean both the Working Directory and Staging Area
# Set project state back to the previous commit (B)
git reset --hard HEAD^
```
## 2.3. Temporarily Save Work with Git Stash
```shell
# Save changes of Working Directory and Staging Area, then clean them
git stash
# List all stashed entries
git stash list
# Apply the most recent stash (keep it in the list)
git stash apply
# Apply and remove the most recent stash
git stash pop
# Clear all stash entries
git stash clear
```
# 3. Local Branch Management
## 3.1. Create and Track a New Branch
```shell
git switch -c branch_name # Create and switch to a new branch
git push --set-upstream origin branch_name # Link local branch to remote
```
## 3.2. Pull and Track a Remote Branch
```shell
git switch -c branch_name # Create and switch to a branch
git fetch origin # Fetch all remote branches
git switch --track origin/branch_name # Link remote branch to local
```
## 3.3. View and Switch Branches
```shell
git branch -a # List all local and remote branches
git branch -vv # Show local branches with tracking info
# Make sure working directory is clean, no red items
git status
git switch
```
## 3.4. Merge and Delete Branches
```shell
git switch main
git pull origin main
git branch -d # Delete a merged local branch
git branch -D # Force delete an unmerged branch
```
# 4. Local Git → Remote GitHub
## 4.1. Push a New Project
```shell
cd ~/your-project-folder
git init # Initialize local Git repository
git remote add origin git@github.com:your-name/repo.git # Add remote GitHub repository
git add . # Stage all files
git commit -m "Initial commit" # Commit changes
git branch -M main # Rename current branch to 'main'
git push -u origin main # Push and set upstream to 'origin/main'
```
## 4.2. Push Local Updates
```shell
cd ~/your-project-folder
git add . # Stage all files
git commit -m "your message" # Commit changes
git push
```
# 5. Remote GitHub → Local Git
## 5.1. Clone a Repository
```shell
git clone git@github.com:your-name/repo.git ~/your-project-folder
cd ~/your-project-folder
# Remove previous folder if needed
rm -rf ~/your-project-folder1
```
## 5.2. Pull Remote Changes
```shell
git fetch origin # Fetch all remote branches
git diff origin/branch # Diff with remote branch
git log origin/branch --oneline # View remote branch history
git merge origin/branch # Merge remote branch into current local branch
```
# 6. Rollback Pushed Changes
> Safely undo the changes introduced by a specific pushed commit
>
> 1. Creates a new commit that reverses the selected commit
> 2. Keeps all subsequent commits intact
>Best used when the changes in that commit have not been heavily modified by later commits
```shell
# View concise commit history
git log --oneline
# View the detail of a commit before reverting
git show
# Revert a commit safely
git revert
# Create a new commit that reverses the changes introduced by
# In effect: it "undoes" the difference between the commit before and
# Working Directory: clean
# Staging Area: clean
# A new commit is created: "Revert "
```
# 7. Conflict Handling
```shell
# Cover
git status
git checkout --ours . # retain our version (local)
checkout --theirs . # retain their version (remote or other branch)
git add .
git commit -m "Resolve conflicts: kept config.js from ours, app.js from theirs"
# Manual Conflict Resolution
git status
# Open the conflicted files and manually resolve the conflicts
git add .
git commit -m "Resolve merge conflicts"
```