Git 常用命令
日常提交、分支切换、暂存、回退、远程仓库与 log 别名等高频命令。
常用命令
<localBranchName> -> 本地分支名<localNewBranchName> -> 本地新分支名<remoteBranchName> -> 远程仓库分支名
首次提交:
git initgit add ./git push -u origin master日常提交:
git add ./-> 变更加入到暂存区域等待提交git commit -m "xxxx"-> 对变更进行说明git pull origin <remoteBranchName>-> 同步远程仓库代码git push origin <remoteBranchName>-> 将此次变更提交到远程仓切换分支:
git checkout <localBranchName>git checkout -b <localNewBranchName>(新建并切换到新建分支)git checkout -b <localNewBranchName> <remoteBranchName>(新建并拉取,本地分支名与远程之间用空格隔开)暂存:
git add ./git stashgit stash pop(弹出暂存文件)在本地目录下关联远程repository :
git remote add origin git@github.com:git_username/repository_name.git删除已有的GitHub远程库:
git remote remove origin查看远程库信息:
git remote -v查看一下本地的分支状态:
git branchgit branch -a(包括本地和远程)git branch -vv(本地分支对应哪个远程分支)git branch -d <localBranchName>删除分支 (不能删除当前所指向的分支)本地分支push到远程指定分支:
git push origin <localBranchName> : <remoteBranchName>git push origin --delete <remoteBranchName>(删除指定的远程分支,谨慎操作!!)查看当前分支历史提交记录:
git loggit reflog(所有记录,包含删除操作)对当前分支版本回退:
git reset –hard 版本号git push -f(远程仓库同步回退,紧接上面命令)拉取远程分支(不会进行分支切换):
git fetch origin <remoteBranchName> : <localNewBranchName>合并远程分支到当前本地分支:
git merge origin/<remoteBranchName>仓库迁移:
git clone --bare [old remote]cd [project].gitgit push --mirror [new remote]自定义彩色log
- 输出
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --- 配置命令(git lg)
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
速查
# 首次提交
git init
git add ./
git commit -m "init"
git push -u origin master
# 日常提交
git add ./ # 变更加入暂存区
git commit -m "xxx" # 提交并说明
git pull origin main # 同步远程
git push origin main # 推送到远程
# 分支
git branch # 本地分支
git branch -a # 含远程分支
git branch -vv # 本地分支对应的远程分支
git checkout <branch> # 切换分支
git checkout -b <newBranch> # 新建并切换
git checkout -b <newBranch> <remoteBranch> # 新建并关联远程分支
git branch -d <branch> # 删除分支(不能删当前分支)
git push origin --delete <remoteBranch> # 删除远程分支(谨慎)
# 暂存
git stash
git stash pop
# 远程仓库
git remote add origin git@github.com:<user>/<repo>.git
git remote -v
git remote remove origin
# 历史 / 回退
git log
git reflog
git reset --hard <commit>
git push -f
# 自定义彩色 log(配置后可直接用 git lg)
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
| 场景 | 关键点 |
|---|---|
| 拉取远程分支但不切换 | git fetch origin <remoteBranch>:<localBranch> |
| 合并远程分支到当前 | git merge origin/<remoteBranch> |
| 仓库整体迁移 | git clone --bare <old> → git push --mirror <new> |
| 回退已推送的提交 | git reset --hard 之后必须 git push -f |
相关
- VS Code 常用插件:Git 相关编辑器辅助