git简明教程

一 配置

(1) git config –global user.name “username” : 配置 git 的全局账号

(2) git config –global user.email “email” : 配置 git 的全局账号邮箱

查看配置的结果: git config –global -l

(3) git config user.name “username” : 配置当前工程的 git 账号

(4) git config user.email “email” : 配置当前工程的 git 账号邮箱

查看配置的结果: vim .git/config

二 工作流

Working Dir —add—> Index —commit—> HEAD —push—> Remote

三 仓库

(1) git init : 新建本地仓库

(2) git clone url : 克隆远程仓库

(3) git remote -v : 查看所有远程仓库信息

(4) git remote add name url : 添加远程仓库

(5) git remote remove name : 删除远程仓库

四 基本命令

1: git add

(1) git add . : 提交所有 Working Dir 修改到 Index

(2) git add dir/file : 提交所有 Working Dir 中 dir 目录/file 文件的修改到 Index

“git add .” 和 “git add -A” 的区别?

git add . : stages new and modified, without deleted

git add -A : stages All

2: git commit

(1) git commit : 提交所有 Index 修改到 HEAD

(2) git commit . : 提交所有修改(包含 Working Dir 和 Index)到 HEAD

(3) git commit dir/file : 提交 dir 目录/file 文件修改到 HEAD

git commit –amend : 修订 HEAD 的修改

3: git push

(1) git push origin master: 提交本地仓库修改至 Remote 仓库的 master 分支

4: git reset

(1) git reset : 从 Index 中恢复所有修改

(2) git reset HEAD^(HEAD~1): 恢复到 HEAD 的上一次提交

5: git checkout

(1) git checkout file(dir) : 检出相应的文件(文件夹)至 HEAD

(2) git checkout d928a3: 检出某一 commit ID 的修改

6: git clean

(1) git clean -f : 删除 untracked files

(2) git clean -fd : 连 untracked 的目录也一起删掉

(3) git clean -nfd: 查看哪些文件会被删除

五 查看和检索

1: git diff

(1) git diff : 查看 Working Dir 与 Index 的区别

(2) git diff –-cached : 查看 Index 与 HEAD 的区别

(3) git diff HEAD : 查看 Working Dir 与 HEAD 的区别

2: git log

(1) git log -p : 显示代码差异

(2) git log –stat : 概要显示

(3) git log –pretty=oneline : 单行显示

(4) git log - : 显示最近 n 条提交

(5) git log –since=”24 hours” : 显示最近 24h 的提交

(6) git log 18f822e..0bb3dfb / git log HEAD~10..HEAD : 显示范围内的提交(前面的起点,后面是终点) / 显示 HEAD 的近 10 个提交

(7) git log file : 显示某 file 文件修改的记录

(8) git log –-grep=”pattern” : 通过信息检索提交

(9) git log –-author=”pattern” : 通过作者检索提交

1
2
3
4
5
实例1:

打印出最近一次提交的代码改动

git log -p -1
1
2
3
4
5
实例2:

打印出CMakeList.txt的代码修改记录

git log -p CMakeLists.txt
1
2
3
4
5
实例3:

打印出提交信息中包含”fix stt”的代码改动

git log -p --grep="fix stt"

3: git show

(1) git show : 查看 HEAD 的所有代码改动

(2) git show d928a3 : 查看某一 commit ID 的所有代码改动

1
2
3
4
5
实例4:

打印出commit ID为d928a345bd93112b0bc1be62e7e935bb2b39dffe的代码修改记录

git show d928a3

4: git blame

(1) git blame file : 查看 file 文件每一行的最近一次修改的信息

(2) git blame -L 50,60 file : 查看 file 文件 50 行至 60 行的最近一次修改的信息

1
2
3
4
5
实例5:

打印出CMakeList.txt中50至60行最后一次修改的信息

git blame -L 50,60 CMakeLists.txt

5: git stash

(1) git stash : 将修改暂存入栈

(2) git pop : 恢复栈顶得修改

(3) git stash list : 列出栈中所有修改

(4) git stash list -p : 列出栈中所有修改的代码详情

(5) git stash apply stash@{1} : 恢复栈中指定修改

(6) git stash clear : 清空栈

(7) git stash drop stash@{1} : 删除栈中修改

六 分支

1: git branch

(1) git branch : 列出本地已经存在的分支

(2) git branch -r : 列出远程分支

(3) git branch -a : 列出本地分支和远程分支

(4) git branch new_branch : 创建新的分支 new_branch

(5) git checkout new_branch : 切换到分支 new_branch

(6) git branch -d new_branch : 删除分支 new_branch

(7) git push origin new_branch : 推送 new_branch 到 Remote 仓库

(8) git push origin :new_branch : 删除 Remote 仓库 new_branch

2: git merge & git rebase & git cherry-pick

(1) git merge master : 合并 master 到当前分支

(2) git rebase master : 变基当前分支到 master

(3) git cherry-pick commit-id : 合并某个 commit 到当前分支

七 标签

(1) git tag : 列出本地已经存在的标签

(2) git tag new_tag : 创建新的标签 new_tag

(3) git tag -d new_tag : 删除标签 new_tag

(4) git push origin new_tag : 推送 new_tag 到 Remote 仓库

(5) git push origin :refs/tags/new_tag : 删除 Remote 仓库 new_tag

(6) git tag rename_tag new_tag: 修改标签的名字

(7) git push origin –tags: 推送所有标签到 Remote 仓库

(8) git fetch origin: 获取 Remote 仓库所有的标签

八 补丁

(1) git diff > _.patch : 将修改写入到 patch 文件 _.patch

(2) patch -p1 < .patch : 将.patch 的修改恢复到当前 git 工程

九 实例

1: 添加.gitignore 不生效

在 git 中如果想忽略掉某个文件, 可以在根目录中添加.gitignore 文件, 其规则如下

1
2
3
4
5
*.a       # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

有时候在项目开发过程中, 突然心血来潮想把某些目录或文件加入忽略规则, 此时添加.gitignore 文件发现却没有生效

这是因为: .gitignore 只能忽略那些原来没有被 track 的文件, 解决办法如下

1
2
3
git rm -r --cached .
git add .
git commit -m 'update .gitignore'

2: 统计某人提交代码的次数

1
git log --pretty=oneline --author='username' | wc -l

3: 克隆 github 子文件夹

https://github.com/laravel/framework/tree/5.7/tests/Auth为例

将 tree/5.7 替换成 trunk

1
svn co https://github.com/laravel/framework/trunk/tests/Auth

4: HTTP 方式保存密码

1
2
3
4
5
# 长期保存密码
git config --global credential.helper store

# 请求地址时带上密码
http://yourname:password@git.repo/name/project.git

5: 关于路径转义显示中文

1
git config --global core.quotepath false

6: 上传完整镜像

1
git push new-remote --mirror