- 参考书籍
- 初次运行前配置
- 检查配置信息
- 在现有目录中初始化仓库
- 检查当前文件状态
- 常用操作
- 跟踪新文件
- 运行 git diff 看暂存前后的变化:
- 图形化git difftool 比较不同的变化
- $ git commit
- git log 查看提交历史
- git remote远程仓库
- git tag 标签
- 分支 git branch
参考书籍
https://git-scm.com/book/zh/v2
初次运行前配置
在.gitconfig下配置用户信息
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
–global 全局配置,所有的项目都采用这个,当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置。
检查配置信息
如果想要检查你的配置,可以使用 git config --list 命令来列出所有 Git 当时能找到的配置。
$ git config --list
user.name=John Doe
[email protected]
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
#获取帮助
若你使用 Git 时需要获取帮助,有三种方法可以找到 Git 命令的使用手册:
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
例如,要想获得 config 命令的手册,执行
$ git help config
在现有目录中初始化仓库
如果你打算使用 Git 来对现有的项目进行管理,你只需要进入该项目目录并输入:
$ git init
该命令将创建一个名为 .git 的子目录,这个子目录含有你初始化的 Git 仓库中所有的必须文件,
这些文件是Git 仓库的骨干。 但是,在这个时候,我们仅仅是做了一个初始化的操作,
你的项目里的文件还没有被跟踪。
检查当前文件状态
$ git status
On branch master
nothing to commit, working directory clean
这说明你现在的工作目录相当干净
#忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文
件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore
的文件,列出要忽略的文件模式。 来看一个实际的例子:
$ cat .gitignore
*.[oa]
*~
第一行告诉 Git 忽略所有以 .o 或 .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二
行告诉 Git 忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副
本。 此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等。 要养成一开始就设置好
.gitignore 文件的习惯,以免将来误提交这类无用的文件。
文件 .gitignore 的格式规范如下:
• 所有空行或者以 # 开头的行都会被 Git 忽略。
• 可以使用标准的 glob 模式匹配。
• 匹配模式可以以(/)开头防止递归。
• 匹配模式可以以(/)结尾指定目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号()匹配零个或多个任意字符;[abc] 匹配
25任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?
)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配
(比如 [0-9] 表示匹配所有 0 到 9 的数字)。 使用两个星号() 表示匹配任意中间目录,比如a/**/z
可以匹
配 a/z, a/b/z 或 a/b/c/z
等。
我们再看一个 .gitignore 文件的例子:
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
TIP
GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表,你可以在
https://github.com/github/gitignore 找到它
常用操作
git clone https://github.com/liufree/Liufree-Notes.git
git add .
git commit -m "提交"
git push
跟踪新文件
使用命令 git add 开始跟踪一个文件。 所以,要跟踪 README 文件,运行:
$ git add README
此时再运行 git status 命令,会看到 README 文件已被跟踪,并处于暂存状态:
运行 git diff 看暂存前后的变化:
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 643e24f..87f08c8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,3 +119,4 @@ at the
## Starter Projects
See our [projects
list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).
+# test line
图形化git difftool 比较不同的变化
git difftool 命令来用 Araxis ,emerge 或 vimdiff 等软件输出 diff 分析结果
git difftool
我的电脑是用meld来比较
$ git commit
这种方式会启动文本编辑器以便输入本次提交的说明。 (默认会启用 shell 的环境变量 $EDITOR 所指定的软件,
一般都是 vim 或 emacs。当然也可以按照 起步 介绍的方式,使用 git config --global core.editor 命
令设定你喜欢的编辑软件。)
另外,你也可以在 commit 命令后添加 -m 选项,将提交信息与命令放在同一行,如下所示:
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
2 files changed, 2 insertions(
给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤:
git commit -a -m 'added new benchmarks'
git log 查看提交历史
Table 2. git log 的常用选项
一个常用的选项是 -p,用来显示每次提交的内容差异。 你也可以加上 -2 来仅显示最近两次提交:
$ git log -p -2
选项 | 说明 |
---|---|
-p | 按补丁格式显示每个更新之间的差异。 |
–stat | 显示每次更新的文件修改统计信息。 |
–shortstat | 只显示 --stat 中最后的行数修改添加移除统计。 |
–name-only | 仅在提交信息后显示已修改的文件清单。 |
–name-status | 显示新增、修改、删除的文件清单。 |
–abbrev-commit | 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。 |
–relative-date | 使用较短的相对时间显示(比如,“2 weeks ago”)。 |
–graph | 显示 ASCII 图形表示的分支合并历史。 |
–pretty | 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和format(后跟指定格式)。 |
git remote远程仓库
- git remote add upstream https://github.com/liufree/Liufree-Notes.git
现在你可以在命令行中使用字符串 upstream 来代替整个 URL。 例如,如果你想拉取 Paul 的仓库中有但你没有的信
息,可以运行 git fetch upstream:
$ git remote -v
origin https://github.com/liufree/Liufree-Notes.git (fetch)
origin https://github.com/liufree/Liufree-Notes.git (push)
$ git remote add upstream https://github.com/liufree/Liufree-Notes.git
[liufree@localhost Liufree-Notes]$ git remote -v
origin https://github.com/liufree/Liufree-Notes.git (fetch)
origin https://github.com/liufree/Liufree-Notes.git (push)
upstream https://github.com/liufree/Liufree-Notes.git (fetch)
upstream https://github.com/liufree/Liufree-Notes.git (push)
从远程仓库中抓取与拉取
就如刚才所见,从远程仓库中获得数据,可以执行:
$ git fetch [remote-name]
推送到远程仓库
git push origin master
当你想分享你的项目时,必须将其推送到上游。 这个命令很简单:
git push [remote-name] [branch-ame]
当你想要将 master 分支推送到 origin 服务器时
git remote show origin 查看远程仓库
$ git remote show orgin
* remote origin
Fetch URL: https://github.com/liufree/Liufree-Notes.git
Push URL: https://github.com/liufree/Liufree-Notes.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
远程仓库的移除与重命名
$ git remote rename upstream dev
$ git remote
dev
origin
$ git remote rm dev
[liufree@localhost Liufree-Notes]$ git remote
origin
git tag 标签
添加标签
$ git tag 显示所有的标签
v0.1
v0.2
$ git show v0.1 会切换到v0.1版本中
传送标签到远程仓库服务器上
默认情况下,git push 命令并不会传送标签到远程仓库服务器上。 在创建完标签后你必须显式地推送标签到
共享服务器上。 这个过程就像共享远程分支一样 - 你可以运行 git push origin [tagname]。
git push origin v0.1 只推送v0.1版本
git push origin --tags 推送所有版本
删除标签
git tag -d <tagname>
应该注意的是上述命令并不会从任何远程仓库中移除这个标签,你必须使用 git push
:refs/tags/ 来更新你的远程仓库:
git push origin :refs/tags/v0.1
分支 git branch
创建分支
git branch dev 创建dev分支
git branch test 创建test分支
git branch 查看当前所有分支
git checkout dev 切换到dev分支
git branch -d test 删除test分支
分支合并
切换到dev分支
git checkout master
git merge dev 合并dev分支
解决合并冲突的方法
为什么会产生冲突
比如说master下面的liu.txt文件发生了更改并commit了,
dev下面的liu.txt也发生了改变并commit了,
这时在master下去合并dev分支时就会产生冲突
git status查看冲突文件
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both added: liu.txt
no changes added to commit (use "git add" and/or "git commit -a")
git mergetool
启用图形化比较不同的文件