Git是一个版本控制系统,它拥有众多的命令。以下是一些常用的Git命令:
-
配置相关命令:
git config --global user.name "Your Name"
:设置用户姓名git config --global user.email "youremail@example.com"
:设置用户邮箱地址git config --list
:查看当前的配置信息
-
仓库相关命令:
git init
:初始化一个新的Git仓库git clone <repository-url>
:克隆一个远程仓库到本地
-
文件操作相关命令:
git add <file-or-directory>
:将文件或目录添加到暂存区git rm <file>
:从工作区和暂存区删除文件
-
提交相关命令:
git commit -m "commit message"
:提交暂存区的改动到仓库git commit -a
:提交工作区的所有改动到仓库
-
分支操作相关命令:
git branch
:查看所有的本地分支git branch <branch-name>
:创建一个新分支git checkout <branch-name>
:切换到指定分支git merge <branch-name>
:将指定分支合并到当前分支
-
远程操作相关命令:
git remote add <name> <repository-url>
:添加一个新的远程仓库git remote -v
:查看所有的远程仓库git push <remote-name> <branch-name>
:将本地分支的改动推送到远程分支git pull <remote-name> <branch-name>
:从远程分支拉取改动到本地分支
-
历史和日志相关命令:
git log
:查看提交日志git show <commit-hash>
:查看指定提交的详细内容git diff
:查看工作区和暂存区的差异
-
撤销操作相关命令:
git reset --hard <commit-hash>
:重置当前分支到指定提交git revert <commit-hash>
:创建一个新的提交,撤销指定的提交git stash
:临时保存当前工作区的改动git stash pop
:恢复临时保存的工作区改动
-
其他常用命令:
git status
:查看仓库当前的状态git fetch
:从远程获取最新版本到本地,但不自动合并git clean
:清除工作区的未追踪文件