Type --bool value is "true" or "false" --int value is decimal number --bool-or-int value is --bool or --int --path value is a path (file or directory name)
bbb: total 8.0K drwxrwxr-x 2 magesfc magesfc 4.0K 6月 1 08:54 ./ drwxrwxr-x 6 magesfc magesfc 4.0K 6月 1 08:54 ../ 通过上面的初始化方法,我初始化了一个bbb的仓库,通过环境变量GIT_DIR指定仓库的路径,这里是aaa.git,然后我们发现bbb目录是个空目录。下面什么文件都没的。这里我们的bbb目录应该是一个工作区间目录的。 $ git status fatal: This operation must be run in a work tree $ git diff fatal: This operation must be run in a work tree 这个时候你执行一些命令会报不是个工作目录的。这个时候我们可以使用另外一个环境变量来解决GIT_WORK_TREE
$ export GIT_WORK_TREE=/home/magesfc/tmp/bbb $ git add . $ git st On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: t.txt 这个时候git就能正常的识别到哪个是仓库目录,哪个是工作目录了。
一般情况很少这样使用的。
选项 –work-tree 和 –git-dir 和上面的环境变量起到了同样的作用。
1 2 3 4 5 6 7 8 9 10 11
git --work-tree=<some path> --git-dir=<some path>
git --work-tree=/home/magesfc/tmp/bbb --git-dir=/home/magesfc/tmp/aaa.git status On branch master
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage)
$ git clone ssh://gerrit.example.com:29418/git/aosp/kernel/msm-4.9 --reference /home/mirror/msm-4.9.git Cloning into 'msm-4.9'... fatal: reference repository '/home/mirror/msm-4.9.git' is not a local repository. 如果这个本地镜像仓库路径不存在,会报错的。
加上这个选项 clone的时候不会把源码检出到工作目录。默认是会检出的。有时候会提示个warning,就是检出源码失败。 warning: remote HEAD refers to nonexistent ref, unable to checkout. 一般这种情况是服务器端HEAD引用的那个分支是个不存在的分支,下载到本地检出代码的时候就报错了。
$ git clone /home/mirror/kernel/msm-4.9.git --no-checkout Cloning into 'msm-4.9'... done. $ ls msm-4.9
配合-v,--verbose选项 $ git branch -rv origin/HEAD -> origin/master origin/maint 34acdd2 Fix ManifestParseError when first child node is comment origin/master da40341 manifest: Support a default upstream value origin/stable eceeb1b Support broken symlinks when cleaning obsolete paths
git branch -a, –all 显示远端本地分支列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
当前分支会标记个星号 $ git br -a * (HEAD detached at cf7c083) # 这个表明当前是一个匿名分支。 master new remotes/origin/HEAD -> origin/master remotes/origin/maint remotes/origin/master remotes/origin/stable
同样的配合-v选项一起的效果 $ git br -av * (HEAD detached at cf7c083) cf7c083 Download latest patch when no patch is specified master da40341 manifest: Support a default upstream value new da40341 manifest: Support a default upstream value remotes/origin/HEAD -> origin/master remotes/origin/maint 34acdd2 Fix ManifestParseError when first child node is comment remotes/origin/master da40341 manifest: Support a default upstream value remotes/origin/stable eceeb1b Support broken symlinks when cleaning obsolete paths
$ git br -v * (HEAD detached at cf7c083) cf7c083 Download latest patch when no patch is specified master da40341 manifest: Support a default upstream value new da40341 manifest: Support a default upstream value
使用-vv的效果 $ git br -vv # * (HEAD detached at cf7c083) cf7c083 Download latest patch when no patch is specified master da40341 [origin/master] manifest: Support a default upstream value new da40341 [origin/master] manifest: Support a default upstream value
使用--abbrev=15选项来指定显示的commit id的位数,默认是显示7位。 (abbrev是缩写的意思) $ git br -v --abbrev=15 * (HEAD detached at cf7c083) cf7c0834cfc24c5 Download latest patch when no patch is specified master da40341a3e6e2e4 manifest: Support a default upstream value new da40341a3e6e2e4 manifest: Support a default upstream value
使用--no-abbrev 显示完整的40位的commit id $ git br -v --no-abbrev * (HEAD detached at cf7c083) cf7c0834cfc24c5c9584695c657c6baf97d0fbb3 Download latest patch when no patch is specified master da40341a3e6e2e45877426aaefb97b3f0735a776 manifest: Support a default upstream value new da40341a3e6e2e45877426aaefb97b3f0735a776 manifest: Support a default upstream value
$ git br --merged #列出已经合并到当前分支的其他分支列表 <commit>参数没有默认是HEAD * (HEAD detached at cf7c083) test
$ git br --no-merged master new
The options --contains, --no-contains, --merged and --no-merged serve four related but different purposes: · --contains <commit> is used to find all branches which will need special attention if <commit> were to be rebased or amended, since those branches contain the specified <commit>. · --no-contains <commit> is the inverse of that, i.e. branches that don’t contain the specified <commit>. · --merged is used to find all branches which can be safely deleted, since those branches are fully contained by HEAD. · --no-merged is used to find branches which are candidates for merging into HEAD, since those branches are not fully contained by HEAD.
$ git br --set-upstream-to=origin/master Branch test1 set up to track remote branch master from origin. $ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b [origin/master: behind 87] Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test3 f46902a forall: Clarify expansion of REPO_ environment values with -c
$ git br --set-upstream-to=origin/master test3 Branch test3 set up to track remote branch master from origin. $ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b [origin/master: behind 87] Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test3 f46902a [origin/master: behind 18] forall: Clarify expansion of REPO_ environment values with -c $
$ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b [origin/master: behind 87] Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test3 f46902a [origin/master: behind 18] forall: Clarify expansion of REPO_ environment values with -c $ git br --unset-upstream dev fatal: Branch 'dev' has no upstream information
$ git br --unset-upstream test1
$ git br --unset-upstream test2 fatal: Branch 'test2' has no upstream information
$ git br --unset-upstream test3
$ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test3 f46902a forall: Clarify expansion of REPO_ environment values with -c
$ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test3 f46902a forall: Clarify expansion of REPO_ environment values with -c
$ git br -m test3 test33
$ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment test 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment * test1 eceeb1b Support broken symlinks when cleaning obsolete paths test2 f46902a forall: Clarify expansion of REPO_ environment values with -c test33 f46902a forall: Clarify expansion of REPO_ environment values with -c
当前我们在test1分支上,我们修改了color.py文件 $ git st On branch test1 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: color.py
no changes added to commit (use "git add" and/or "git commit -a")
然后我们试着切换分支到dev分支上,但是发现报错了,因为color.py 文件的改动和 分支 dev上的有冲突了。 $ git checkout dev error: Your local changes to the following files would be overwritten by checkout: color.py Please commit your changes or stash them before you switch branches. Aborting
这种情况下我们可以使用 -f选项,强制切换到dev分支,不过我们的改动会丢失。
我们在切换到另外一个分支test2,这个分支可以顺利切换 $ git checkout test2 M color.py Switched to branch 'test2'
$ git st On branch test2 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: color.py
no changes added to commit (use "git add" and/or "git commit -a") $
我创建了新的分支,同时设置追踪一个远端分支origin/maint $ git checkout -b track-maint --track origin/maint Branch track-maint set up to track remote branch maint from origin. Switched to a new branch 'track-maint'
$ git brvv dev 34acdd2 Fix ManifestParseError when first child node is comment * track-maint 34acdd2 [origin/maint] Fix ManifestParseError when first child node is comment
$ git co -b track-master origin/master Previous HEAD position was cf31fe9... Initial Contribution Branch track-master set up to track remote branch master from origin. Switched to a new branch 'track-master' $ git brvv * track-master da40341 [origin/master] manifest: Support a default upstream value 通过这个方式我们创建了一个追踪分支
当前我们在new分支上 $ git brvv * new 02dbb6d Fix StopIteration exception during repo {sync,status} track-master da40341 [origin/master] manifest: Support a default upstream value
通过使用git checkout我们切换到 track-master 分支上。这个是不带--detach选项的行为。 $ git co track-master Switched to branch 'track-master' Your branch is up-to-date with 'origin/master'. $ git brvv new 02dbb6d Fix StopIteration exception during repo {sync,status} * track-master da40341 [origin/master] manifest: Support a default upstream value
下面我们带上--detach选项来切换到new分支上试试 $ git co --detach new HEAD is now at 02dbb6d... Fix StopIteration exception during repo {sync,status}
$ git brvv * (HEAD detached at refs/heads/new) 02dbb6d Fix StopIteration exception during repo {sync,status} new 02dbb6d Fix StopIteration exception during repo {sync,status} track-master da40341 [origin/master] manifest: Support a default upstream value 我们可以看到并没有切换到new分支上,而是以这个new分支为基准切换到了一个匿名分支上.
如果后面的参数是commitID,tag这个时候切换的是匿名分支,这个时候--detach选项可以省略. $ git checkout da40341 Previous HEAD position was 02dbb6d... Fix StopIteration exception during repo {sync,status} HEAD is now at da40341... manifest: Support a default upstream value $ git brvv * (HEAD detached at da40341) da40341 manifest: Support a default upstream value new 02dbb6d Fix StopIteration exception during repo {sync,status} track-master da40341 [origin/master] manifest: Support a default upstream value $
我们这个时候修改了color.py文件,但是我们想撤销修改,就可以使用git checkout了 HEAD detached at da40341 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: color.py modified: command.py
no changes added to commit (use "git add" and/or "git commit -a") $ git co HEAD -- color.py $ git st HEAD detached at da40341 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: command.py
no changes added to commit (use "git add" and/or "git commit -a")
我们也可以指定个commit来做作为参数 $ git co a32c92c -- command.py $ git st
HEAD detached at da40341 nothing to commit, working tree clean
当前分支状态 $ git brvv * new 02dbb6d Fix StopIteration exception during repo {sync,status} track-master da40341 [origin/master] manifest: Support a default upstream value
我们基于v1.0这个点创建一个孤立分支orphan-branch $ git co --orphan orphan-branch v1.0 Switched to a new branch 'orphan-branch' 这个时候我们看到所有的代码都是绿色的,表明都是在暂存区的。 $ git st On branch orphan-branch
No commits yet
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: .gitignore new file: COPYING new file: Makefile new file: codereview/__init__.py new file: codereview/need_retry_pb2.py new file: codereview/proto_client.py new file: codereview/review_pb2.py new file: codereview/upload_bundle_pb2.py new file: color.py new file: command.py new file: editor.py new file: error.py new file: froofle/__init__.py new file: froofle/protobuf/__init__.py new file: froofle/protobuf/descriptor.py new file: froofle/protobuf/descriptor_pb2.py new file: froofle/protobuf/internal/__init__.py new file: froofle/protobuf/internal/decoder.py new file: froofle/protobuf/internal/encoder.py new file: froofle/protobuf/internal/input_stream.py new file: froofle/protobuf/internal/message_listener.py new file: froofle/protobuf/internal/output_stream.py new file: froofle/protobuf/internal/type_checkers.py new file: froofle/protobuf/internal/wire_format.py new file: froofle/protobuf/message.py new file: froofle/protobuf/reflection.py new file: froofle/protobuf/service.py new file: froofle/protobuf/service_reflection.py new file: froofle/protobuf/text_format.py new file: gerrit_upload.py new file: git_command.py new file: git_config.py new file: import_ext.py new file: import_tar.py new file: import_zip.py new file: main.py new file: manifest.py new file: pager.py new file: project.py new file: remote.py new file: repo new file: subcmds/__init__.py new file: subcmds/compute_snapshot_check.py new file: subcmds/diff.py new file: subcmds/forall.py new file: subcmds/help.py new file: subcmds/init.py new file: subcmds/prune.py new file: subcmds/stage.py new file: subcmds/start.py new file: subcmds/status.py new file: subcmds/sync.py new file: subcmds/upload.py 这个之后我们需要git commit 一下。 $ git ci -s -m 'init my orphan branch base on tag v1.0' [orphan-branch (root-commit) 139e9c1] init my orphan branch base on tag v1.0 53 files changed, 11781 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 Makefile create mode 100644 codereview/__init__.py create mode 100644 codereview/need_retry_pb2.py create mode 100755 codereview/proto_client.py create mode 100644 codereview/review_pb2.py create mode 100644 codereview/upload_bundle_pb2.py create mode 100644 color.py create mode 100644 command.py create mode 100644 editor.py create mode 100644 error.py create mode 100644 froofle/__init__.py create mode 100644 froofle/protobuf/__init__.py create mode 100644 froofle/protobuf/descriptor.py create mode 100644 froofle/protobuf/descriptor_pb2.py create mode 100644 froofle/protobuf/internal/__init__.py create mode 100644 froofle/protobuf/internal/decoder.py create mode 100644 froofle/protobuf/internal/encoder.py create mode 100644 froofle/protobuf/internal/input_stream.py create mode 100644 froofle/protobuf/internal/message_listener.py create mode 100644 froofle/protobuf/internal/output_stream.py create mode 100644 froofle/protobuf/internal/type_checkers.py create mode 100644 froofle/protobuf/internal/wire_format.py create mode 100644 froofle/protobuf/message.py create mode 100644 froofle/protobuf/reflection.py create mode 100644 froofle/protobuf/service.py create mode 100644 froofle/protobuf/service_reflection.py create mode 100644 froofle/protobuf/text_format.py create mode 100755 gerrit_upload.py create mode 100644 git_command.py create mode 100644 git_config.py create mode 100644 import_ext.py create mode 100644 import_tar.py create mode 100644 import_zip.py create mode 100755 main.py create mode 100644 manifest.py create mode 100755 pager.py create mode 100644 project.py create mode 100644 remote.py create mode 100755 repo create mode 100644 subcmds/__init__.py create mode 100644 subcmds/compute_snapshot_check.py create mode 100644 subcmds/diff.py create mode 100644 subcmds/forall.py create mode 100644 subcmds/help.py create mode 100644 subcmds/init.py create mode 100644 subcmds/prune.py create mode 100644 subcmds/stage.py create mode 100644 subcmds/start.py create mode 100644 subcmds/status.py create mode 100644 subcmds/sync.py create mode 100644 subcmds/upload.py
$ git st On branch orphan-branch nothing to commit, working tree clean
git commit之后我们的孤立分支orphan-branch就新建好了 $ git brvv new 02dbb6d Fix StopIteration exception during repo {sync,status} * orphan-branch 139e9c1 init my orphan branch base on tag v1.0 track-master da40341 [origin/master] manifest: Support a default upstream value
$ git co track-master error: Your local changes to the following files would be overwritten by checkout: color.py Please commit your changes or stash them before you switch branches. Aborting 这个时候git checkout命令是拒绝你的切分支请求的
$ git co track-master --merge M color.py Switched to branch 'track-master' Your branch is up-to-date with 'origin/master'.
$ git st On branch track-master Your branch is up-to-date with 'origin/master'.
Unmerged paths: (use "git reset HEAD <file>..." to unstage) (use "git add <file>..." to mark resolution)
both modified: color.py
no changes added to commit (use "git add" and/or "git commit -a")
HEAD一般的都是指向一个有名字的分支的(例如master),同时每个分支指向一个特定的commit。 HEAD (refers to branch 'master') | v a---b---c branch 'master' (refers to commit 'c') ^ | tag 'v2.0' (refers to commit 'b')
HEAD (refers to commit 'f') | v e---f / a---b---c---d branch 'master' (refers to commit 'd') ^ | tag 'v2.0' (refers to commit 'b') 这个时候如果我们切换到master分支上会怎么样??? $ git checkout master
HEAD (refers to branch 'master') e---f | / v a---b---c---d branch 'master' (refers to commit 'd') ^ | tag 'v2.0' (refers to commit 'b') 这个时候我们要特别注意,提交f没有被任何分支引用了。最终这个提交f会被git 垃圾回收机制给清理掉,到那之后就再也找不回f提交了。 也就是执行了git gc就会删除f提交,e提交等。除非我们在次之前建个引用指向这个提交f。 $ git checkout -b foo (1) $ git branch foo (2) $ git tag foo (3) (1)creates a new branch foo, which refers to commit f, and then updates HEAD to refer to branch foo. In other words, we’ll no longer be in detached HEAD state after this command.
(2)similarly creates a new branch foo, which refers to commit f, but leaves HEAD detached.
(3)creates a new tag foo, which refers to commit f, leaving HEAD detached.
$ git checkout master (1) switch branch $ git checkout master~2 Makefile (2) take a file out of another commit $ rm -f hello.c $ git checkout hello.c (3) restore hello.c from the index
$ git checkout -- '*.c' check out all C source files out of the index 这个将会包所有的.c文件都检出了,文件hello.c 也会,这个不是看working tree下面是否有个这个hello.c 文件,而是看暂存区是否有个hello.c文件。
# 例如: format:"The author of %h was %an, %ar%nThe title was >>%s<<%n",其中使用%n来换行。
可以用的占位符有: · %H: commit hash 提交对象(commit)的完整哈希字串 · %h: abbreviated commit hash 提交对象的简短哈希字串 · %T: tree hash 树对象(tree)的完整哈希字串 · %t: abbreviated tree hash 树对象的简短哈希字串 · %P: parent hashes 父对象(parent)的完整哈希字串 · %p: abbreviated parent hashes 父对象的简短哈希字串 · %an: author name 作者(author)的名字 · %aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %ae: author email 作者的电子邮件地址 · %aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %ad: author date (format respects --date= option) 作者修订日期(可以用 -date= 选项定制格式) · %aD: author date, RFC2822 style · %ar: author date, relative 作者修订日期,按多久以前的方式显示 · %at: author date, UNIX timestamp · %ai: author date, ISO 8601-like format · %aI: author date, strict ISO 8601 format · %cn: committer name 提交者(committer)的名字 · %cN: committer name (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %ce: committer email 提交者的电子邮件地址 · %cE: committer email (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %cd: committer date (format respects --date= option) 提交日期(可以用 -date= 选项定制格式) · %cD: committer date, RFC2822 style · %cr: committer date, relative 提交日期,按多久以前的方式显示 · %ct: committer date, UNIX timestamp · %ci: committer date, ISO 8601-like format · %cI: committer date, strict ISO 8601 format · %d: ref names, like the --decorate option of git-log(1) · %D: ref names without the " (", ")" wrapping. · %e: encoding · %s: subject 提交说明 · %f: sanitized subject line, suitable for a filename · %b: body · %B: raw body (unwrapped subject and body) · %N: commit notes · %GG: raw verification message from GPG for a signed commit · %G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature · %GS: show the name of the signer for a signed commit · %GK: show the key used to sign a signed commit · %gD: reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}). · %gd: shortened reflog selector; same as %gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master). · %gn: reflog identity name · %gN: reflog identity name (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %ge: reflog identity email · %gE: reflog identity email (respecting .mailmap, see git-shortlog(1) or git-blame(1)) · %gs: reflog subject · %Cred: switch color to red · %Cgreen: switch color to green · %Cblue: switch color to blue · %Creset: reset color · %C(...): color specification, as described under Values in the "CONFIGURATION FILE" section of git-config(1). By default, colors are shown only when enabled for log output (by color.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal). %C(auto,...) is accepted as a historical synonym for the default (e.g., %C(auto,red)). Specifying %C(always,...) will show the colors even when color is not otherwise enabled (though consider just using `--color=always to enable color for the whole output, including this format and anything else git might color). auto alone (i.e. %C(auto)) will turn on auto coloring on the next placeholders until the color is switched again. · %m: left (<), right (>) or boundary (-) mark · %n: newline · %%: a raw % · %x00: print a byte from a hex code · %w([<w>[,<i1>[,<i2>]]]): switch line wrapping, like the -w option of git-shortlog(1). · %<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >= 2. · %<|(<N>): make the next placeholder take at least until Nth columns, padding spaces on the right if necessary · %>(<N>), %>|(<N>): similar to %<(<N>), %<|(<N>) respectively, but padding spaces on the left · %>>(<N>), %>>|(<N>): similar to %>(<N>), %>|(<N>) respectively, except that if the next placeholder takes more spaces than given and there are spaces on its left, use those spaces · %><(<N>), %><|(<N>): similar to % <(<N>), %<|(<N>) respectively, but padding both sides (i.e. the text is centered) · %(trailers): display the trailers of the body as interpreted by git-interpret-trailers(1) 需要注意的是有些占位符需要和某些选择配合使用的。
$ git log --pretty=format:"%h %s" --graph * 2d3acf9 ignore errors from SIGCHLD on trap * 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit |\ | * 420eac9 Added a method for getting the current branch. * | 30e367c timeout code and tests * | 5a09431 add timeout protection to grit * | e1193f8 support for heads with slashes in them |/ * d6016bc require time for xmlschema * 11d191e Merge branch 'defunkt' into local
这个命令和git stash有相似的使用场景。当工作被打断了,有不想取储存,就可以使用这个新建个干净的工作目录。 You are in the middle of a refactoring session and your boss comes in and demands that you fix something immediately. You might typically use git-stash(1) to store your changes away temporarily, however, your working tree is in such a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) that you don’t want to risk disturbing any of it. Instead, you create a temporary linked working tree to make the emergency fix, remove it when done, and then resume your earlier refactoring session.
manifest: Support a default upstream value It's convenient to set upstream for all projects in a manifest instead of repeating the same value for each project. Change-Id: I946b1de4efb01b351c332dfad108fa7d4f443cba diff --git a/docs/manifest-format.txt b/docs/manifest-format.txt index 56bdf14..0c957dd 100644 --- a/docs/manifest-format.txt +++ b/docs/manifest-format.txt @@ -44,6 +44,7 @@ following DTD: <!ATTLIST default remote IDREF #IMPLIED> <!ATTLIST default revision CDATA #IMPLIED> <!ATTLIST default dest-branch CDATA #IMPLIED> + <!ATTLIST default upstream CDATA #IMPLIED> <!ATTLIST default sync-j CDATA #IMPLIED> <!ATTLIST default sync-c CDATA #IMPLIED> <!ATTLIST default sync-s CDATA #IMPLIED> @@ -164,6 +165,11 @@ Project elements not setting their own `dest-branch` will inherit this value. If this value is not set, projects will use `revision` by default instead. +Attribute `upstream`: Name of the Git ref in which a sha1 +can be found. Used when syncing a revision locked manifest in +-c mode to avoid having to sync the entire ref space. Project elements +not setting their own `upstream` will inherit this value. + Attribute `sync-j`: Number of parallel jobs to use when synching. Attribute `sync-c`: Set to true to only sync the given Git diff --git a/manifest_xml.py b/manifest_xml.py index 60d6116..d0211ea 100644 --- a/manifest_xml.py +++ b/manifest_xml.py @@ -59,6 +59,7 @@ class _Default(object): revisionExpr = None destBranchExpr = None + upstreamExpr = None remote = None sync_j = 1 sync_c = False @@ -230,6 +231,9 @@ class XmlManifest(object): if d.destBranchExpr: have_default = True e.setAttribute('dest-branch', d.destBranchExpr) + if d.upstreamExpr: + have_default = True + e.setAttribute('upstream', d.upstreamExpr) if d.sync_j > 1: have_default = True e.setAttribute('sync-j', '%d' % d.sync_j) @@ -295,7 +299,8 @@ class XmlManifest(object): revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr if not revision or revision != p.revisionExpr: e.setAttribute('revision', p.revisionExpr) - if p.upstream and p.upstream != p.revisionExpr: + if (p.upstream and (p.upstream != p.revisionExpr or + p.upstream != d.upstreamExpr)): e.setAttribute('upstream', p.upstream) if p.dest_branch and p.dest_branch != d.destBranchExpr: @@ -694,6 +699,7 @@ class XmlManifest(object): d.revisionExpr = None d.destBranchExpr = node.getAttribute('dest-branch') or None + d.upstreamExpr = node.getAttribute('upstream') or None sync_j = node.getAttribute('sync-j') if sync_j == '' or sync_j is None: @@ -830,7 +836,7 @@ class XmlManifest(object): dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr - upstream = node.getAttribute('upstream') + upstream = node.getAttribute('upstream') or self._default.upstreamExpr groups = '' if node.hasAttribute('groups'):
git show -s, –no-patch 不显示提交更改的内容 默认是显示更改内容的,可以通过-p, -u, –patch选项来改变这个选项的行为。
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58 $
$ git show --name-only commit cf7c0834cfc24c5c9584695c657c6baf97d0fbb3 (HEAD) Author: Akshay Verma <akshayverma948@gmail.com> Date: Thu Mar 15 21:56:30 2018 +0530
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58
project.py subcmds/download.py
$ git show --name-status commit cf7c0834cfc24c5c9584695c657c6baf97d0fbb3 (HEAD) Author: Akshay Verma <akshayverma948@gmail.com> Date: Thu Mar 15 21:56:30 2018 +0530
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58
M project.py M subcmds/download.py
git show –abbrev-commit,–no-abbrev-commit 是显示40位commitid还是显示简短的commitid。
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58 $ git show --abbrev-commit -s --abbrev=15 commit cf7c0834cfc24c5 (HEAD) Author: Akshay Verma <akshayverma948@gmail.com> Date: Thu Mar 15 21:56:30 2018 +0530
Download latest patch when no patch is specified When someone does "repo download -c <project> <change>" without specifying a patch number, by default patch 1 is downloaded. An alternative is to look for the latest patch and download the same when no explicit patch is given. This commit does the same by identifying the latest patch using "git ls-remote". Change-Id: Ia5fa7364415f53a3d9436df4643e38f3c90ded58
使用了--no-abbrev-commit选项就会显示完整的40位的commit id了。
git show –oneline 显示为一行,和git log的一样效果。如果同时使用了-s那就是和git log –oneline -1 等价效果了。
$ git show --oneline cf7c083 (HEAD) Download latest patch when no patch is specified diff --git a/project.py b/project.py old mode 100644 new mode 100755 index e4682e6..5297a5c --- a/project.py +++ b/project.py @@ -2270,6 +2270,16 @@ class Project(object): if self._allrefs: raise GitError('%s cherry-pick %s ' % (self.name, rev)) + def _LsRemote(self): + cmd = ['ls-remote'] + p = GitCommand(self, cmd, capture_stdout=True) + if p.Wait() == 0: + if hasattr(p.stdout, 'decode'): + return p.stdout.decode('utf-8') + else: + return p.stdout + return None + def _Revert(self, rev): cmd = ['revert'] cmd.append('--no-edit') diff --git a/subcmds/download.py b/subcmds/download.py old mode 100644 new mode 100755 index e1010aa..384af78 --- a/subcmds/download.py +++ b/subcmds/download.py @@ -62,6 +62,14 @@ If no project is specified try to use current directory as a project. ps_id = int(m.group(2)) else: ps_id = 1 + regex = r'refs/changes/%2.2d/%d/(\d+)' % (chg_id % 100, chg_id) + output = project._LsRemote() + if output: + rcomp = re.compile(regex, re.I) + for line in output.splitlines(): + match = rcomp.search(line) + if match: + ps_id = max(int(match.group(1)), ps_id) to_get.append((project, chg_id, ps_id)) else: project = self.GetProjects([a])[0]
如果同时使用了 -s选项 $ git show -s --oneline cf7c083 (HEAD) Download latest patch when no patch is specified 这个时候和git log 的效果一样了 $ git log --oneline -1 cf7c083 (HEAD) Download latest patch when no patch is specified
$ git ll [magesfc@10.0.63.43 ] 18-06-19 13:57 /home/magesfc/tmp/myconfig * ef9faab - update b file — SCM life (HEAD -> test) - (2 days ago) * 5211c8d - update a file — SCM life - (2 days ago) * cdd80fa - touch e — SCM life - (2 days ago) * 34260fc - touch d — SCM life - (2 days ago) * 96ed6df - touch c — SCM life - (2 days ago) * f3480c7 - touch b — SCM life - (2 days ago) * a2b661e - touch a — SCM life - (2 days ago) * f7277c4 - init empty git — Minghui Ma - (12 days ago)
下面我们试试这个-n选项的效果 $ git ll * ef9faab - update b file — SCM life (HEAD -> test) - (2 days ago) * 5211c8d - update a file — SCM life - (2 days ago) * cdd80fa - touch e — SCM life - (2 days ago) * 34260fc - touch d — SCM life - (2 days ago) * 96ed6df - touch c — SCM life - (2 days ago) * f3480c7 - touch b — SCM life - (2 days ago) * a2b661e - touch a — SCM life - (2 days ago) * f7277c4 - init empty git — Minghui Ma - (12 days ago) $ $ git revert -n HEAD~5..HEAD~2 到这里git revert执行成功了
通过git log看到并没有产生revert的那个提交 $ git ll * ef9faab - update b file — SCM life (HEAD -> test) - (2 days ago) * 5211c8d - update a file — SCM life - (2 days ago) * cdd80fa - touch e — SCM life - (2 days ago) * 34260fc - touch d — SCM life - (2 days ago) * 96ed6df - touch c — SCM life - (2 days ago) * f3480c7 - touch b — SCM life - (2 days ago) * a2b661e - touch a — SCM life - (2 days ago) * f7277c4 - init empty git — Minghui Ma - (12 days ago)
通过git status我们查看工作区和暂存区的状态,发现3个删除状态的文件,这个3个文件就是我们命令参数中的3个提交加上的文件,这里是revert操作那就是要删除这3个文件了 $ git st On branch test You are currently reverting commit 96ed6df. (all conflicts fixed: run "git revert --continue") (use "git revert --abort" to cancel the revert operation)
Changes to be committed: (use "git reset HEAD <file>..." to unstage)
$ cd tmp/myconfig $ git ll * d322685 - Merge branch 'test' into empty — SCM life (HEAD -> empty) - (2 days ago) |\ | * ef9faab - update b file — SCM life (test) - (2 days ago) | * 5211c8d - update a file — SCM life - (2 days ago) | * cdd80fa - touch e — SCM life - (2 days ago) | * 34260fc - touch d — SCM life - (2 days ago) | * 96ed6df - touch c — SCM life - (2 days ago) | * f3480c7 - touch b — SCM life - (2 days ago) | * a2b661e - touch a — SCM life - (2 days ago) * | d826f8a - update file a — SCM life - (2 days ago) * | 01b8625 - touch a — SCM life - (2 days ago) |/ * f7277c4 - init empty git — Minghui Ma - (12 days ago) $ $ $ git revert d322685 error: commit d3226856737d20b4945ef6ff3454f093fe851052 is a merge but no -m option was given. fatal: revert failed 这个时候不使用-m选项会报错的,这个提交有2个父提交,我们要通过-m来指定哪个父提交。
$ git revert -m 3 d322685 error: commit d3226856737d20b4945ef6ff3454f093fe851052 does not have parent 3 fatal: revert failed
$ git revert -m 2 d322685 [empty 56049dc] Revert "Merge branch 'test' into empty" 1 file changed, 1 insertion(+), 8 deletions(-) $ git ll * 56049dc - Revert "Merge branch 'test' into empty" — SCM life (HEAD -> empty) - (5 seconds ago) * d322685 - Merge branch 'test' into empty — SCM life - (2 days ago) |\ | * ef9faab - update b file — SCM life (test) - (2 days ago) | * 5211c8d - update a file — SCM life - (2 days ago) | * cdd80fa - touch e — SCM life - (2 days ago) | * 34260fc - touch d — SCM life - (2 days ago) | * 96ed6df - touch c — SCM life - (2 days ago) | * f3480c7 - touch b — SCM life - (2 days ago) | * a2b661e - touch a — SCM life - (2 days ago) * | d826f8a - update file a — SCM life - (2 days ago) * | 01b8625 - touch a — SCM life - (2 days ago) |/ * f7277c4 - init empty git — Minghui Ma - (12 days ago) $ 如果是-m 1这个时候revert的 “a2b661e - touch a” 到 “ef9faab - update b file” 这几个提交的改动 $ git revert -m 1 d322685 [empty 03da93b] Revert "Merge branch 'test' into empty" 4 files changed, 1 deletion(-) delete mode 100644 b delete mode 100644 c delete mode 100644 d delete mode 100644 e $ git ll * 03da93b - Revert "Merge branch 'test' into empty" — SCM life (HEAD -> empty) - (4 seconds ago) * d322685 - Merge branch 'test' into empty — SCM life - (2 days ago) |\ | * ef9faab - update b file — SCM life (test) - (2 days ago) | * 5211c8d - update a file — SCM life - (2 days ago) | * cdd80fa - touch e — SCM life - (2 days ago) | * 34260fc - touch d — SCM life - (2 days ago) | * 96ed6df - touch c — SCM life - (2 days ago) | * f3480c7 - touch b — SCM life - (2 days ago) | * a2b661e - touch a — SCM life - (2 days ago) * | d826f8a - update file a — SCM life - (2 days ago) * | 01b8625 - touch a — SCM life - (2 days ago) |/ * f7277c4 - init empty git
我们重置回原来 $ git reset --hard d322685 HEAD is now at d322685 Merge branch 'test' into empty $ git ll * d322685 - Merge branch 'test' into empty — SCM life (HEAD -> empty) - (2 days ago) |\ | * ef9faab - update b file — SCM life (test) - (2 days ago) | * 5211c8d - update a file — SCM life - (2 days ago) | * cdd80fa - touch e — SCM life - (2 days ago) | * 34260fc - touch d — SCM life - (2 days ago) | * 96ed6df - touch c — SCM life - (2 days ago) | * f3480c7 - touch b — SCM life - (2 days ago) | * a2b661e - touch a — SCM life - (2 days ago) * | d826f8a - update file a — SCM life - (2 days ago) * | 01b8625 - touch a — SCM life - (2 days ago) |/ * f7277c4 - init empty git
此时看看b文件的内容 $ cat b bbb
我们可以看到这一行是在 ef9faabedab94293708588d93e4b6e8a6a697a3a 这个提交上加上的 $ git show ef9faab commit ef9faabedab94293708588d93e4b6e8a6a697a3a (test) Author: SCM life <scm.life@scmlife.com> Date: Sun Jun 17 12:35:10 2018 +0800
update b file Change-Id: Ic28a9e9434fe4cbc1d95ec4436289d9008689c31 Signed-off-by: SCM life <scm.life@scmlife.com>
1.Changes between the tips of the topic and the master branches. 2.Same as above. 3.Changes that occurred on the master branch since when the topic branch was started off it. 第一个和第二个一样,都是比较topic 分支到 master分支在那个最后一个提交处的代码的差异。 第三个就是比较两个分支共同起始点那个提交开始,到master结束的那个提交 的差异。 我们给个图片就清楚可以看到了:
What’s the difference between HEAD^ and HEAD~ in git
1 2 3 4
<rev>^, e.g. HEAD^, v1.5.1^0 A suffix ^ to a revision parameter means the first parent of that commit object. ^<n> means the <n>th parent (i.e. <rev>^ is equivalent to <rev>^1). As a special rule, <rev>^0 means the commit itself and is used when <rev> is the object name of a tag object that refers to a commit object.
1 2 3 4 5
<rev>~<n>, e.g. master~3 A suffix ~<n> to a revision parameter means the commit object that is the <n>th generation ancestor of the named commit object, following only the first parents. I.e. <rev>~3 is equivalent to <rev>^^^ which is equivalent to <rev>^1^1^1. See below for an illustration of the usage of this form.
类似这样的一个符号的 ~ 和 ^ 都是表示父提交, 两个符号 ~~ 和 ^^ 都是表示的爷爷提交。
他们的不同是体现在后面跟上了数字。 ~2 表示的是上面2层。这个是在层级上的。2 means up two levels in the hierarchy。 ^2 表示的第二个父提交。一个^ 表示第一个父提交,两个^表示第二个父提交。^2 means the second parent where a commit has more than one parent (i.e. because it’s a merge) 这2个符号也可以组合起来使用的。HEAD2^3 means HEAD’s grandparent commit’s third parent commit