Git-For-Beginners
Get start
- Configuration
- Check File Status
- Add and Commit File to the Local Repository
- View Repository Commit History
- View File Difference
- Reset Version
- Remove Files
gitignore
- Branch
- Branch Conflict
- Rebase
- Help
Configuration
**View Git version** 查看git版本
shell
git --version
**Config**
- 设置用户名
shell
git config --global user.name "xxxx"
- 设置邮箱
shell
git config --global user.email "xxxx"
- 查看用户信息
shell
git config --list
**Initialization** 初始化本地仓库
shell
git init
Check File Status
- 查看文件处于什么状态
- ??: 未跟踪
- A: 已暂存
- M: 已修改过
- D: 已删除
- R:重命名
- U:已更新未 合并
shell
git status
shell
# 更精简的输出
git status -s # git status --short
Add and Commit File to the Local Repository
**ADD**
添加到 暂存区 Staging Area
shell
gi[](Git-Introduction.md)# --all的缩写
- 可以使用通配符
shell
git add *.txt
- 可以添加整个目录
shell
git add .
**COMMIT**
只提交暂存区Staging Area的内容,不会提交工作区Workspace的内容
View Repository Commit History
查看仓库提交历史记录
To see you commit log in the repository
sh
git log
sh
git log --oneline #查看简洁的提交记录
View File Difference
**Difference** among **WorkSpace**, **Staging Area** and **Repository**
- WorkSpace VS Staging Area
shell
git diff
- WorkSpace + Staging Area VS Repository
shell
git diff #比较工作区+暂存区与本地仓库
- Staging Area VS Repository
shell
# 比对已暂存文件与最后一次提交的文件差异
git diff --staged
sh
# 查看已经暂存起来的变化
git diff --cached
**Difference** between **Different Version**
sh
git diff <VersionID1> <VersionID2>
sh
git diff HEAD~ HEAD #最新版本与前一个版本比较
git diff HEAD^ HEAD
sh
git diff HEAD~n HEAD #最新版本与前n个版本比较
**Difference** between **Different Branch**
sh
git diff <branch_name> <branch_name>
Reset the version
**Reset** 版本回退
- 回退版本 并保存工作区和暂存区的所有内容
shell
git reset --soft [VersionID]
- 回退版本 并不保存工作区和暂存区的内容
shell
git reset --hard [VersionID]
- 回退版本 保存工作区的所有内容 但不保存暂存区的内容
shell
git reset --mixed[VersionID]
Remove Files
Method 1: Delete From Workspace then Add the change to Staging Area
shell
rm files;
git add file;
Method 2: Delete From Workspace and Staging Area
shell
git rm <file>
- Delete From Staging Area Save in the Workspace
shell
git rm --cashed<file>
- Delete All Staging Area
shell
git rm -r --cached .
- Delete Recursively all files in the content After delete need to be commit
shell
git rm -r *
- Move Files
shell
# 文件重命名
git mv xxx.txt yyy.txt
gitignore
设置忽略文件
.gitignore匹配规则
- 空行或者以#开头的行会被Git忽略。一般空行用于可读性的分隔,#一般用作注释
- 使用标准的Blob模式匹配,例如:
- * 通配任意个字符问号
- ? 匹配单个字符
- [] 表示匹配列表中的单个字符,比如:[abc]表示a/b/c
- 两个星号 ** 表示匹配任意的中间目录中括号可以使用短中线连接
- [0-9]表示任意一位数字,[a-z]表示任意一位小写字母
- ! 表示取反
shell
#忽略所有的 .a 文件
*.a
#但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
!lib.a
#只忽略当前目录下的TODO文件,而不忽略 subdir/TODO
/TODO
#忽略任何目录下名为 build 的文件夹
build/
# 忽略 doc/notes.txt,但不忽略 doc/sercer/arch.txt
doc/* txt
#忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf
+ **Disable tracking** 解除追踪
shell
git rm -r --cached example_folder/
+ **Commit Change** 提交变化
shell
git commit -m "Remove example_folder from tracking"
Branch
**Create Branch** 创建分支
shell
git branch xxxxxx(branch name)
**View Branch** 查看分支
shell
git branch
**Switch Branch** 切换分支
shell
git checkout xxxxx(branch name)
shell
git switch xxxxx(branch name)
**Create and Switch Branch** 创建并切换分支
shell
git checkout -b xxxxx(branch name)
**Merge Branch** 合并分支
先切进一个分支,然后合并另一个分支
- 把Branch2合并到Branch1
shell
git checkout branch1
shell
git merge branch2
**Delete Branch** 删除分支
shell
git branch -d dev #删除已经合并过的分支
shell
git branch -D dev #强制删除未合并的分支
Branch Conflict
- 两分支未修改同一个文件的同一个位置:Git自动合并
- 两分支修改同一个文件的同一个位置:产生冲突
- 解决方法: 1. 手工修改冲突文件,合并冲突内容 2. 添加到暂存区 $git add file
3. 提交修改 $git commit -m "message" - 终止合并:$git merge --abort
- 解决方法: 1. 手工修改冲突文件,合并冲突内容 2. 添加到暂存区 $git add file
Rebase
**Rebase**
将当前分支变基到另一个分支
shell
git rebase branchname
Merge | Rebase | |
---|---|---|
Disadvantage | 产生额外的提交节点分支图复杂 | 会改变提交历史,改变了当前分支branch out的节点,避免在共享分支使用 |
Advantage | 不会破坏元分支的提交历史方便回溯和查看 | 不会新增额外的提交记录,形成线性历史,比较直观和干净 |
Help
shell
git command -help - See all the available options for the specific command
shell
git help --all - See all possible commands
+