# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## new file: Test.scala
commit3: add test3.ccommit2: add test2.ccommit1: add test1.c
commit2: add test2.ccommit1: add test1.c
commit3: add test3.ccommit2: add test2.ccommit1: add test1.c
commit2: add test2.ccommit1: add test1.c
git fetch origin master
git log -p master..origin/master
git merge origin/master
git fetch origin master:tmp
git diff tmp
git merge tmp
git pull origin master
当git clone之后,直接git pull它会自动匹配一个正确的remote url
是因为在config文件中配置了以下内容:
1 [branch "master"]
2 remote = origin
3 merge = refs/heads/master
表明:
1.git处于master这个branch下时,默认的remote就是origin;
2.当在master这个brach下使用指定remote和merge的git pull时,使用默认的remote和merge。
但是对于自己建的项目,并用push到远程服务器上,并没有这块内容,需要自己配置。
如果直接运行git pull,会得到如此结果:
1 $ git pull
2 Password:
3 You asked me to pull without telling me which branch you
4 want to merge with, and 'branch.master.merge' in
5 your configuration file does not tell me, either. Please
6 specify which branch you want to use on the command line and
7 try again (e.g. 'git pull <repository> <refspec>').
8 See git-pull(1) for details.
9
10 If you often merge with the same branch, you may want to
11 use something like the following in your configuration file:
12
13 [branch "master"]
14 remote = <nickname>
15 merge = <remote-ref>
16
17 [remote "<nickname>"]
18 url = <url>
19 fetch = <refspec>
20
21 See git-config(1) for details.
在参考[2]中,有这样一段:
Note: at this point your repository is not setup to merge _from_ the remote branch when you type 'git pull'. You can either freshly 'clone' the repository (see "Developer checkout" below), or configure your current repository this way:
1 git remote add -f origin login@git.sv.gnu.org:/srv/git/project.git
2 git config branch.master.remote origin
3 git config branch.master.merge refs/heads/master
因此通过git config进行如下配置:
1 $ git config branch.master.remote origin
2 $ git config branch.master.merge refs/heads/master
或者加上--global选项,对于全部项目都使用该配置。
git merge的基本用法为把一个分支或或某个commit的修改合并现在的分支上。
我们可以运行git merge -h查看其命令
usage: git merge [options] [<commit>...]
or: git merge [options] <msg> HEAD <commit>
or: git merge --abort
-n do not show a diffstat at the end of the merge
--stat show a diffstat at the end of the merge
--summary (synonym to --stat)
--log[=<n>] add (at most <n>) entries from shortlog to merge commi t message
--squash create a single commit instead of doing a merge
--commit perform a commit if the merge succeeds (default)
-e, --edit edit message before committing
--ff allow fast-forward (default)
--ff-only abort if fast-forward is not possible
--rerere-autoupdate update the index with reused conflict resolution if po ssible
-s, --strategy <strategy>
merge strategy to use
-X, --strategy-option <option=value>
option for selected merge strategy
-m, --message <message>
merge commit message (for a non-fast-forward merge)
-v, --verbose be more verbose
-q, --quiet be more quiet
--abort abort the current in-progress merge
--progress force progress reporting
-S, --gpg-sign[=<key id>]
GPG sign commit
--overwrite-ignore update ignored files (default)
git merge [options] <msg> HEAD <commit> 这里的 HEAD 其实就是分支名,用于说明把 HEAD 分支合并到当前分支。
git diff
可以用来比较:
1.staging area和working area的文件 (无其他参数时)
git diff
2.master分支和working area的文件 (用master参数)
git diff master
git diff HEAD
4.用远程master分支比较当前工作区
git diff refs/remotes/origin/master
5.经常还要用到master分支的某个文件的历史版本和working area的该文件的比较
git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cppgit diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp上面的命令中, diff后面的参数指的是commit id, ./socket_helper.cpp是要比较的文件路径。
diff的命令输出格式注意:
---代表源文件 +++代表目标文件通常working area的文件都是被当作目标文件来看待。
-开头的行,是只出现在源文件中的行
+开头的行,是只出现在目标文件中的行
空格开头的行,是源文件和目标文件中都出现的行
差异按照差异小结进行组织,每个差异小结的第一行都是定位语句,由@@开头,@@结尾。
chenshu@chenshu-yangzhou-home:~/kaimei/data_service/src$ git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp diff --git a/data_service/src/socket_helper.cpp b/data_service/src/socket_helper.cpp index d606452..047e213 100755 --- a/data_service/src/socket_helper.cpp +++ b/data_service/src/socket_helper.cpp @@ -4,6 +4,7 @@ #include "data/login_response.h" #include "data/heartbeat_response.h" #include "helper/parser.h" +#include "helper/time_measure.h" #include <booster/log.h> #include "exception/socket_error.h" #include "exception/data_error.h"上面的diff结果表明
1.某个提交记录0c5ee代表的socket_helper.cpp文件是源文件,当前working area的socket_helper文件是目标文件。
2.在源文件第4行开始的6行和目标文件第4行开始的7行构成一个差异小结
3.这个差异小结中,目标文件添加了一行#include "helper/time_measure.h"
4.其他空格开头的行表明没有差异。