Git 是当今最流行的分布式版本控制系统,掌握常用命令行操作是开发者的基本功。本文整理了从仓库初始化到日常提交流程、从分支管理到 submodule 的常用命令,适合作为速查手册。

Git 全局设置:

1
2
git config --global user.name "jeromexiong"
git config --global user.email "1540428743@qq.com"

创建 git 仓库:

1
2
3
4
5
6
7
8
mkdir bookclub
cd bookclub
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/jeromexiong/bookclub.git
git push -u origin master

已有仓库?

1
2
3
4
5
6
7
cd existing_git_repo
git remote add origin https://gitee.com/jeromexiong/bookclub.git
git push -u origin master

error: fatal: refusing to merge unrelated histories
// 把两段不相干的 分支进行强行合并
git pull origin master --allow-unrelated-histories
1
2
3
4
5
6
7
8
9
10
11
12
git checkout . && git clean -df #本地所有修改的。没有的提交的,都返回到原来的状态
git stash #把所有没有提交的修改暂存到stash里面。可用git stash pop回复。
git reset --hard HASH #返回到某个节点,不保留修改。
git reset --soft HASH #返回到某个节点。保留修改

git status #查看git状态
git add filename #暂存文件 . 当前所有
git rm --cached filename #取消暂存文件
git rm -r --cached . #取消当前所有暂存文件
git rm filename #删除文件
git checkout filename #取消文件修改
git commit -m 'commit msg'

git tag

修改.gitignore文件并让其生效

1
2
3
4
5
6
7
8
9
10
# 重置提交
git reset --hard commit

# 下载git
git clone <git url> -b <branch or tag>

git rm -r --cached .
git add .

git commit -m "Remove ignored files"

submodule

  1. 添加submodule
1
git submodule add 仓库地址 路径
  1. 下载的工程带有submodule
1
git submodule update --init --recursive