G

Git 命令介绍

2025-06-05

Git 命令介绍

Git 是一款功能强大、免费、开源且广受欢迎的版本控制系统,全球数百万开发者和大型公司都在使用它。它已用于数百万个软件的版本控制,以避免冲突,并真正简化开发人员的工作。

这是一本 Git 命令手册,更像是一份各种命令的备忘单,方便参考。

创建一个 Repo

# Creates a local repo
git init [name] 

# Clones a remote repo
git clone [remote_project_url] 
Enter fullscreen mode Exit fullscreen mode

将文件或目录添加到暂存区

# Stage a changed file
git add [file_or_directory]

# Stage all changed files
git add .

# Stage some changes but not all changes
git add --patch [file_name]
Enter fullscreen mode Exit fullscreen mode

提交到本地仓库

git commit
git commit -m "[commit_message]"
Enter fullscreen mode Exit fullscreen mode

工作目录状态

# Status of the local repository
git status 

# Changes to the file name
git diff [file_name] 
Enter fullscreen mode Exit fullscreen mode

分支

# Create a new branch
git branch [new_branch_name]

# List all branches 
git branch  
git branch -a
git branch --list

# Switch to branch
git checkout [branch_name] 
git switch [branch_name]

# Creates a new branch and switch to the branch  
git switch -c [new_branch_name]
git checkout -b [new_branch_name] 

# Merges a branch with another
git merge [branch_name]

# Delete a branch 
git branch -d [branch_name] 

# Branch newFeature has all the commits of branch main
git rebase main newFeature 
Enter fullscreen mode Exit fullscreen mode

使用远程存储库

# Clones a remote repo
git clone [remote_project_url] 

# List all remotes repositories to the local repo
git remote -v

# Add a remote repository with local repository 
git remote add [remote_project_url] 

# Fetches changes from the remote repository
git fetch [remote_project_url] 

# Fetches changes from the remote repository and merge it to local
git pull 
git pull origin [main_branch_name] 

# Publish local changes to remote repository
git push 
Enter fullscreen mode Exit fullscreen mode

配置

# List configuration options
git config --list 

# Set your username
git config --global user.name "Ephraim Atta-Duncan"

# Set your email
git config --global user.email 0x10@gmail.com

# Set your global branch names
git config --global init.defaultBranch [new_default_branch_name]
Enter fullscreen mode Exit fullscreen mode

重置

# Revert the changes to exactly what you had
# Go back to HEAD
git reset --hard HEAD

# Go back the commit before head
git reset --head HEAD^

# Go back to two commits before head
git reset --head HEAD~2 

# Revert changes to commits only
# Go back to HEAD
git reset --soft HEAD

# Go back the commit before head
git reset --soft HEAD^

# Go back to two commits before head
git reset --soft HEAD~2 
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/ephraimduncan/git-commands-introduction-36gh
PREV
我们如何将无服务器 API 提升 300 倍
NEXT
每个开发人员的 50 条基本技巧。