常用 Git 命令集锦

118 阅读1分钟

我们每天和 git 打交道,算是老朋友了,总结常用 git 命令,温故知新。

image.png

  • Workspace:工作区
  • Index / Stage:暂存区
  • local Repository:仓库区(或本地仓库)
  • Remote Repository:远程仓库

一、代码提交

  1. 提交代码
  • 提交代码
git commit -m "commit message"
  • 忽略代码检查,强制提交
git commit --no-verify -m "commit message"
  1. 修改最后一次提交,以及 message
git commit --amend

执行命令后,终端自动打开 bash 编辑器:

image.png

其中,fix: 新增组件 CountDown 是你上次提交的 message ,直接键入:i 修改信息,修改完成后,按下 Esc 键退出编辑模式,在键入 :wq 回车退出并保存修改,完成提交。

二、新建代码仓库

  1. 在当前目录新建一个Git代码库
git init
  1. 下载仓库和它的整个 git 提交历史
git clone [url]

三、分支管理

  1. 新建分支
git branch [branch-name]
  1. 删除分支
git br -D [branch-name]
  1. 切换分支
git checkout [branch-name]
  1. 查看所有分支
git branch

四、查看信息

  1. 查看 git 提交历史
git log
  1. 显示有变更的文件
git status
  1. 显示暂存区和工作区的差异
git diff

五、远程同步

  1. 拉取远程仓库的变动,并与本地分支合并
git pull [remote] [branch]
  1. 下载远程所有的变动
git fetch [remote]

3.拉取远程仓库的变动,与与本地分支合并,并且 head 指向远程分支

git pull [remote] [branch] --rebase

六、撤销

  1. 撤销合并多个 commit 提交
git rebase --abort