配置相关
git config --global user.name "Your Name"
:设置全局用户名。git config --global user.email "your.email@example.com"
:设置全局用户邮箱。
仓库操作
git init
:初始化一个新的 Git 仓库。git clone <repository-url>
:克隆一个远程仓库到本地。
基本操作
git status
:查看当前仓库的状态。git add <file>
:将文件添加到暂存区。git add .
:将所有更改的文件添加到暂存区。git commit -m "commit message"
:提交暂存区的文件,并添加提交信息。git commit -a -m "commit message"
:跳过git add
步骤,直接提交所有已跟踪的文件。
分支操作
git branch
:列出所有本地分支。git branch <branch-name>
:创建一个新分支。git checkout <branch-name>
:切换到指定分支。git checkout -b <branch-name>
:创建并切换到一个新分支。git merge <branch-name>
:合并指定分支到当前分支。git branch -d <branch-name>
:删除本地分支。
远程操作
git remote -v
:查看所有远程仓库。git remote add <name> <url>
:添加一个远程仓库。git fetch <remote>
:从远程仓库获取最新代码,但不合并。git pull <remote> <branch>
:从远程仓库获取代码并合并到当前分支。git push <remote> <branch>
:将当前分支推送到远程仓库。
查看历史
git log
:查看提交历史。git log --oneline
:简洁的查看提交历史。git diff
:查看工作目录和暂存区之间的差异。git diff <branch1> <branch2>
:查看两个分支之间的差异。
撤销更改
git checkout -- <file>
:撤销对文件的修改(未提交的更改)。git reset HEAD <file>
:将文件从暂存区移除,但保留工作目录中的更改。git reset --hard
:重置工作目录和暂存区到最近的一次提交状态。
标签操作
git tag
:列出所有标签。git tag <tag-name>
:创建一个新标签。git tag -d <tag-name>
:删除本地标签。git push <remote> <tag-name>
:推送标签到远程仓库。git push <remote> --tags
:推送所有标签到远程仓库。
其他有用命令
git stash
:保存当前未提交的更改以便稍后恢复。git stash pop
:恢复最近一次保存的未提交更改。git rebase <branch>
:将一个分支的更改应用到另一个分支的基础上。