Git—— 1.安装

Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。(和linux同作者)Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。
Git 不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等。Git 是分布式的,这是 Git 和 SVN,CVS 等,最核心的区别。Git 把内容按元数据方式存储,而 SVN 是按文件:所有的资源控制系统都是把文件的元信息隐藏在一个类似 .svn、.cvs 等的文件夹里。Git 分支和 SVN 的分支不同。Git 没有一个全局的版本号,目前为止这是跟 SVN 相比 Git 缺少的最大的一个特征。Git 的内容完整性要优于 SVN:Git 的内容存储使用的是 SHA-1 哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏。

1.安装

yum安装
yum install git
源码安装

2.工作区、暂存区和版本库

工作区:当前目录。
暂存区:stage, 或index。存放在当前目录下的.git/index中。
版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
在这里插入图片描述

3.git config

[root@node2 temp]# git config 
Display all 282 possibilities? (y or n)
add.ignoreErrors                core.excludesfile               gitcvs.logfile                  notes.rewrite.rebase 
advice.commitBeforeMerge        core.fileMode                   gitcvs.usecrlfattr              notes.rewriteRef 
advice.detachedHead             core.fsyncobjectfiles           gui.blamehistoryctx             pack.compression 
advice.implicitIdentity         core.gitProxy                   gui.commitmsgwidth              pack.deltaCacheLimit 
advice.pushNonFastForward       core.ignorecase                 gui.copyblamethreshold          pack.deltaCacheSize 
advice.resolveConflict          core.ignoreCygwinFSTricks       gui.diffcontext                 pack.depth 
advice.statusHints              core.ignoreStat                 gui.encoding                    pack.indexVersion 
alias.                          core.logAllRefUpdates           gui.fastcopyblame               pack.packSizeLimit 
am.keepcr                       core.loosecompression           gui.matchtrackingbranch         pack.threads 
apply.ignorewhitespace          core.notesRef                   gui.newbranchtemplate           pack.window 
apply.whitespace                core.packedGitLimit             gui.pruneduringfetch            pack.windowMemory 
branch.                         core.packedGitWindowSize        gui.spellingdictionary          pager.
branch.autosetupmerge           core.pager                      guitool.                        pretty.
branch.autosetuprebase          core.preferSymlinkRefs          gui.trustmtime                  pull.octopus 
browser.                        core.preloadindex               help.autocorrect                pull.twohead 
clean.requireForce              core.quotepath                  help.browser                    push.default 
color.branch                    core.repositoryFormatVersion    help.format                     rebase.autosquash 
color.branch.current            core.safecrlf                   http.lowSpeedLimit              rebase.stat 
color.branch.local              core.sharedRepository           http.lowSpeedTime               receive.autogc 
color.branch.plain              core.sparseCheckout             http.maxRequests                receive.denyCurrentBranch 
color.branch.remote             core.symlinks                   http.minSessions                receive.denyDeleteCurrent 
color.decorate.branch           core.trustctime                 http.noEPSV                     receive.denyDeletes 
color.decorate.HEAD             core.warnAmbiguousRefs          http.postBuffer                 receive.denyNonFastForwards 
color.decorate.remoteBranch     core.whitespace                 http.proxy                      receive.fsckObjects 
color.decorate.stash            core.worktree                   http.sslCAInfo                  receive.unpackLimit 
color.decorate.tag              diff.algorithm                  http.sslCAPath                  receive.updateserverinfo 
color.diff                      diff.autorefreshindex           http.sslCert                    remote.
color.diff.commit               diff.external                   http.sslCertPasswordProtected   remote.pushdefault 
color.diff.frag                 diff.ignoreSubmodules           http.sslKey                     remotes.
color.diff.func                 diff.mnemonicprefix             http.sslVerify                  repack.usedeltabaseoffset 
color.diff.meta                 diff.noprefix                   http.useragent                  rerere.autoupdate 
color.diff.new                  diff.renameLimit                i18n.commitEncoding             rerere.enabled 
color.diff.old                  diff.renames                    i18n.logOutputEncoding          sendemail.
color.diff.plain                diff.statGraphWidth             imap.authMethod                 sendemail.aliasesfile 
color.diff.whitespace           diff.submodule                  imap.folder                     sendemail.aliasfiletype 
color.grep                      diff.suppressBlankEmpty         imap.host                       sendemail.bcc 
color.grep.context              diff.tool                       imap.pass                       sendemail.cc 
color.grep.filename             difftool.                       imap.port                       sendemail.cccmd 
color.grep.function             difftool.prompt                 imap.preformattedHTML           sendemail.chainreplyto 
color.grep.linenumber           diff.wordRegex                  imap.sslverify                  sendemail.confirm 
color.grep.match                fetch.recurseSubmodules         imap.tunnel                     sendemail.envelopesender 
color.grep.selected             fetch.unpackLimit               imap.user                       sendemail.from 
color.grep.separator            format.attach                   init.templatedir                sendemail.identity 
color.interactive               format.cc                       instaweb.browser                sendemail.multiedit 
color.interactive.error         format.headers                  instaweb.httpd                  sendemail.signedoffbycc 
color.interactive.header        format.numbered                 instaweb.local                  sendemail.smtpdomain 
color.interactive.help          format.pretty                   instaweb.modulepath             sendemail.smtpencryption 
color.interactive.prompt        format.signature                instaweb.port                   sendemail.smtppass 
color.pager                     format.signoff                  interactive.singlekey           sendemail.smtpserver 
color.showbranch                format.subjectprefix            log.date                        sendemail.smtpserveroption 
color.status                    format.suffix                   log.decorate                    sendemail.smtpserverport 
color.status.added              format.thread                   log.showroot                    sendemail.smtpuser 
color.status.changed            format.to                       mailmap.file                    sendemail.suppresscc 
color.status.header             gc.                             man.                            sendemail.suppressfrom 
color.status.nobranch           gc.aggressiveWindow             man.viewer                      sendemail.thread 
color.status.untracked          gc.auto                         merge.                          sendemail.to 
color.status.updated            gc.autopacklimit                merge.conflictstyle             sendemail.validate 
color.ui                        gc.packrefs                     merge.log                       showbranch.default 
commit.status                   gc.pruneexpire                  merge.renameLimit               status.relativePaths 
commit.template                 gc.reflogexpire                 merge.renormalize               status.showUntrackedFiles 
core.abbrev                     gc.reflogexpireunreachable      merge.stat                      status.submodulesummary 
core.askpass                    gc.rerereresolved               merge.tool                      submodule.
core.attributesfile             gc.rerereunresolved             mergetool.                      tar.umask 
core.autocrlf                   gitcvs.allbinary                mergetool.keepBackup            transfer.unpackLimit 
core.bare                       gitcvs.commitmsgannotation      mergetool.keepTemporaries       url.
core.bigFileThreshold           gitcvs.dbdriver                 mergetool.prompt                user.email 
core.compression                gitcvs.dbname                   merge.verbosity                 user.name 
core.createObject               gitcvs.dbpass                   notes.displayRef                user.signingkey 
core.deltaBaseCacheLimit        gitcvs.dbTableNamePrefix        notes.rewrite.                  web.browser 
core.editor                     gitcvs.dbuser                   notes.rewrite.amend             
core.eol                        gitcvs.enabled                  notes.rewriteMode  

使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:
仓库级别 local,优先级最高,用户级别 global,优先级次之,系统级别 system优先级最低。
git 仓库级别对应的配置文件是当前仓库下的.git/config。
git 用户级别对应的配置文件是用户宿主目录下的~/.gitconfig。
git系统级别对应的配置文件是git安装目录下的 /etc/gitconfig。

#查看仓库级别配置信息
[root@node2 temp]# git config --local -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.test.url=https://github.com/blueicex/test.git
remote.test.fetch=+refs/heads/*:refs/remotes/test/*
branch.test.remote=test
branch.test.merge=refs/heads/test
 #查看用户级别配置信息
[root@node2 temp]# git config --global -l 
user.name=blueicex
user.email=blueice1980@126.com
#查看系统级别配置信息
[root@node2 temp]# git config --system -l
fatal: unable to read config file '/etc/gitconfig': No such file or directory
#查看所有的配置信息,依次是、用户级别、仓库级别
[root@node2 temp]# git config -l
user.name=blueicex

git config -e 编辑配置文件
git config --local -e 编辑仓库级别配置文件
git config --global -e 编辑用户级别配置文件
git config --system -e 编辑系统级别配置文件
git config 添加配置项目
git config --global user.email “Name@***.com”
git config --global user.name “Name”
#增加配置项
–add
git config [–local|–global|–system] --add section.key value(默认是添加在local配置中)
#获取配置项
–get
git config [–local|–global|–system] --get section.key(默认是获取local配置中内容)
#删除配置项
–unset
git config [–local|–global|–system] --unset section.key

3.git init

git init在当前目录生成.git文件夹。

4.git add

同名文件只要添加一次就不必再重新添加了,自动识别各种状态。
#添加所有内容
git add -A
#添加新文件和编辑过的文件不包括删除的文件
git add .
#添加编辑或者删除的文件,不包括新添加的文件。
git add -u
#查看中被所有修改过或已删除文件但没有提交的文件,并通过其revert子命令可以查看 中所有未跟踪的文件,同时进入一个子命令系统。
git add -i
#使用指定目录作为Git仓库。在当前目录创建了repo1
git init repo1

5.git clone

[root@node2 temp]# git clone
You must specify a repository to clone.

usage: git clone [options] [--] <repo> [<dir>]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --progress            force progress reporting
    -n, --no-checkout     don't create a checkout
    --bare                create a bare repository
    --mirror              create a mirror repository (implies bare)
    -l, --local           to clone from a local repository
    --no-hardlinks        don't use local hardlinks, always copy
    -s, --shared          setup as shared repository
    --recursive           initialize submodules in the clone
    --recurse-submodules  initialize submodules in the clone
    --template <template-directory>
                          directory from which templates will be used
    --reference <repo>    reference repository
    -o, --origin <name>   use <name> instead of 'origin' to track upstream
    -b, --branch <branch>
                          checkout <branch> instead of the remote's HEAD
    -u, --upload-pack <path>
                          path to git-upload-pack on the remote
    --depth <depth>       create a shallow clone of that depth
    --single-branch       clone only one branch, HEAD or --branch
    --separate-git-dir <gitdir>
                          separate git dir from working tree
    -c, --config <key=value>
                          set config inside the new repository

git clone http[s]😕/example.com/path/to/repo.git
git clone http://git.oschina.net/yiibai/sample.git
git clone ssh://example.com/path/to/repo.git
git clone git://example.com/path/to/repo.git
git clone /opt/git/project.git
git clone file:///opt/git/project.git
git clone ftp[s]😕/example.com/path/to/repo.git
git clone rsync://example.com/path/to/repo.git
git clone [user@]example.com:path/to/repo.git
Git协议下载速度最快,SSH协议用于需要用户认证的场合。
git clone git://git.kernel.org/pub/scm/…/linux.git textdir
cd textdir
make # 执行代码或其它命令

6.git status

用于显示工作目录和暂存区的状态。git status不显示已经commit到项目历史中去的信息。看项目历史的信息要使用git log。
在每次执行 git commit之前先使用git status检查文件状态是一个很好的习惯,防止你不小心提交了不想提交的东西。
只列出所有已经被git管理的且被修改但没提交的文件。
git status -uno

[root@node2 test]# git status -h
usage: git status [options] [--] <pathspec>...

    -v, --verbose         be verbose
    -s, --short           show status concisely
    -b, --branch          show branch information
    --porcelain           machine-readable output
    --long                show status in long format (default)
    -z, --null            terminate entries with NUL
    -u, --untracked-files[=<mode>]
                          show untracked files, optional modes: all, normal, no. (Default: all)
    --ignored             show ignored files
    --ignore-submodules[=<when>]
                          ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)
    --column[=<style>]    list untracked files in columns

7.git diff

#比较当前文件和暂存区文件差异
git diff
#比较两次提交之间的差异
git diff
#在两个分支之间比较
git diff
#比较暂存区和版本库差异
git diff --staged
#比较暂存区和版本库差异
git diff --cached
#仅仅比较统计信息
git diff --stat
#查看已缓存的与未缓存的所有改动
git diff HEAD

[root@node2 test]# echo test1 >> test1
[root@node2 test]# echo test2 >> test2
[root@node2 test]# echo test3 >> test3
[root@node2 test]# git add -A
[root@node2 test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   test1
#	new file:   test2
#	new file:   test3
#
[root@node2 test]# echo test4 >> test4
[root@node2 test]# 
[root@node2 test]# git status -s
A  test1
A  test2
A  test3
?? test4
[root@node2 test]# git diff test1 test2
[root@node2 test]# git diff 

master 必须commit后才出现,只有有了master才能新建branch(分支)

[root@node2 test]# git branch a
fatal: Not a valid object name: 'master'.
[root@node2 test]# git branch 
[root@node2 test]# git branch master
fatal: Not a valid object name: 'master'.
[root@node2 test]# ls
test1  test2  test3  test4
[root@node2 test]# git commit test1 -m "test11"
[master (root-commit) 3bfdad0] test11
 1 file changed, 1 insertion(+)
 create mode 100644 test1
[root@node2 test]# git commit test2 -m "test21"
[master 762cefc] test21
 1 file changed, 1 insertion(+)
 create mode 100644 test2
[root@node2 test]# git branch 
* master
[root@node2 test]# git branch 2019
[root@node2 test]# git branch 2020
[root@node2 test]# git branch 
  2019
  2020
* master

7.git commit

将索引的当前内容与描述更改的用户和日志消息一起存储在新的提交中。
#普通提交,前提,必须git add后。
git commit -m “the commit message” #
#git add + git commit 或者git rm + git commit
git commit -a
#增补提交,新的提交覆盖旧的提交。
git commit --amend
提交发现错误,可以使用 git reset 命令恢复。
git status -s

8.git rm

从工作区和索引中删除文件。
如果只是简单地从工作目录中手工删除文件,运行 git status 时就会在 Changes not staged for commit 的提示。

#删除当前目录和暂存区中的文件
git rm
#删除暂存区域和工作区的文件,如果二者不同
git rm -f
#只删除暂存区域的文件
git rm --cached
#递归删除
进入某个目录中,会删除该目录下的所有文件和子目录。
git rm –r *

9.git reset

(A) 回滚添加操作
$ edit file1.c file2.c
$ git add file1.c file1.c
$ mailx
$ git reset
$ git pull git://info.example.com/ nitfol
(B)回滚最近一次提交
$ git commit -a -m “这是提交的备注信息”
$ git reset --soft HEAD^
$ edit code
$ git commit -a -c ORIG_HEAD
© 回滚最近几次提交,并把这几次提交放到指定分支中
回滚最近几次提交,并把这几次提交放到叫做topic/wip的分支上去。
$ git branch topic/wip
$ git reset --hard HEAD~3
$ git checkout topic/wip
?????????????????

9.git mv

用于移动或重命名文件,目录或符号链接。

[root@node2 test]# git mv -h
usage: git mv [options] <source>... <destination>

    -v, --verbose         be verbose
    -n, --dry-run         dry run
    -f, --force           force move/rename even if target exists
    -k                    skip move/rename errors

把一个文件:text.txt 移动到 textdir,可以执行以下操作 -
$ git mv text.txt textdir
运行上面的 git mv 其实就相当于运行了3条命令:
root@node2 test]# mv test.txt textdir/
root@node2 test]# git rm test.txt
root@node2 test]# git add textdir

10.git branch

[root@node2 test]# git branch -h
usage: git branch [options] [-r | -a] [--merged | --no-merged]
   or: git branch [options] [-l] [-f] <branchname> [<start-point>]
   or: git branch [options] [-r] (-d | -D) <branchname>...
   or: git branch [options] (-m | -M) [<oldbranch>] <newbranch>

Generic options
    -v, --verbose         show hash and subject, give twice for upstream branch
    -q, --quiet           suppress informational messages
    -t, --track           set up tracking mode (see git-pull(1))
    --set-upstream        change upstream info
    -u, --set-upstream-to <upstream>
                          change the upstream info
    --unset-upstream      Unset the upstream info
    --color[=<when>]      use colored output
    -r, --remotes         act on remote-tracking branches
    --contains <commit>   print only branches that contain the commit
    --abbrev[=<n>]        use <n> digits to display SHA-1s

Specific git-branch actions:
    -a, --all             list both remote-tracking and local branches
    -d, --delete          delete fully merged branch
    -D                    delete branch (even if not merged)
    -m, --move            move/rename a branch and its reflog
    -M                    move/rename a branch, even if target exists
    --list                list branch names
    -l, --create-reflog   create the branch's reflog
    --edit-description    edit the description for the branch
    -f, --force           force creation (when already exists)
    --no-merged <commit>  print only not merged branches
    --merged <commit>     print only merged branches
    --column[=<style>]    list branches in columns
  1. 查看当前有哪些分支
    $ git branch
    master
  • wchar_support
    Shell
    上面显示结果中,当前有两个分支:master 和 wchar_support,当前在 wchar_support 分支上,它前面有个星号(*)。
  1. 新建一个分支
    下面命令将创建一个分支:dev2 -
    $ git branch dev2
    Shell
  2. 切换到指定分支
    下面命令将切换到指定分支:dev2 -
    $ git checkout dev2
    $ # 再次查看分支
    $ git branch
  • dev2
    master
    wchar_support
    Shell
  1. 查看本地和远程分支
    $ git branch -a
  • dev2
    master
    wchar_support
    remotes/origin/HEAD -> origin/master
    remotes/origin/master
    remotes/origin/wchar_support
    Shell
  1. 将更改添加到新建分支上
    $ git status
    On branch dev2
    Untracked files:
    (use “git add …” to include in what will be committed)

     newfile.txt
    

nothing added to commit but untracked files present (use “git add” to track)

Administrator@MY-PC /D/worksp/sample (dev2)

$ git add newfile.txt

Administrator@MY-PC /D/worksp/sample (dev2)

$ git commit newfile.txt -m “commit a new file: newfile.txt”
[dev2 c5f8a25] commit a new file: newfile.txt
1 file changed, 2 insertions(+)
create mode 100644 newfile.txt

Administrator@MY-PC /D/worksp/sample (dev2)

$ git push origin dev2
Username for ‘http://git.oschina.net’: 769728683@qq.com
Password for ‘http://769728683@qq.com@git.oschina.net’:
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 965 bytes | 0 bytes/s, done.
Total 11 (delta 3), reused 0 (delta 0)
To http://git.oschina.net/yiibai/sample.git

  • [new branch] dev2 -> dev2

Administrator@MY-PC /D/worksp/sample (dev2)
$
Shell
6. 修改分支的名字
$ git branch

  • dev2
    master
    wchar_support

Administrator@MY-PC /D/worksp/sample (dev2)
$ git branch -m dev2 version.2

Administrator@MY-PC /D/worksp/sample (version.2)
$ git branch -r
origin/HEAD -> origin/master
origin/dev2
origin/master
origin/wchar_support

Administrator@MY-PC /D/worksp/sample (version.2)
$ git branch
master

  • version.2
    wchar_support
    Shell
  1. 删除远程分支
    删除一个名称为:dev2 的远客
    $ git branch
    master
  • version.2
    wchar_support

Administrator@MY-PC /D/worksp/sample (version.2)
$ git push origin --delete dev2
Username for ‘http://git.oschina.net’: 769728683@qq.com
Password for ‘http://769728683@qq.com@git.oschina.net’:
To http://git.oschina.net/yiibai/sample.git

  • [deleted] dev2
    Shell
  1. 合并某个分支到当前分支
    合并分支:version.2到当前分支(master),如下 -
    $ git branch
    master
  • version.2
    wchar_support

Administrator@MY-PC /D/worksp/sample (version.2)
$ git checkout master
Switched to branch ‘master’
Your branch is up-to-date with ‘origin/master’.

Administrator@MY-PC /D/worksp/sample (master)
$ git status
On branch master
Your branch is up-to-date with ‘origin/master’.

nothing to commit, working directory clean

Administrator@MY-PC /D/worksp/sample (master)
$ git merge version.2
Updating e7d1734…c5f8a25
Fast-forward
mydir/text.txt | 0
newfile.txt | 2 ++
src/string.py | 5 +++±
3 files changed, 6 insertions(+), 1 deletion(-)
create mode 100644 mydir/text.txt
create mode 100644 newfile.txt

11.git branch

 [root@node2 test]# git checkout -h
usage: git checkout [options] <branch>
   or: git checkout [options] [<branch>] -- <file>...

    -q, --quiet           suppress progress reporting
    -b <branch>           create and checkout a new branch
    -B <branch>           create/reset and checkout a branch
    -l                    create reflog for new branch
    --detach              detach the HEAD at named commit
    -t, --track           set upstream info for new branch
    --orphan <new branch>
                          new unparented branch
    -2, --ours            checkout our version for unmerged files
    -3, --theirs          checkout their version for unmerged files
    -f, --force           force checkout (throw away local modifications)
    -m, --merge           perform a 3-way merge with the new branch
    --overwrite-ignore    update ignored files (default)
    --conflict <style>    conflict style (merge or diff3)
    -p, --patch           select hunks interactively
    --ignore-skip-worktree-bits
                          do not limit pathspecs to sparse entries only

切换分支
$ git checkout master #(1)
从另一个提交中取出文件
$ git checkout master~2 Makefile #(2)
)从索引中恢复hello.
$ rm -f hello.c
$ git checkout hello.c #(3)
如果想要检出索引中的所有C源文件
$ git checkout – ‘*.c’
切换到该分支
$ git checkout – hello.c
示例-2
在错误的分支工作后,想切换到正确的分支,则使用:
$ git checkout mytopic
Shell
但是,您的“错误”分支和正确的“mytopic”分支可能会在在本地修改的文件中有所不同,在这种情况下,上述检出将会失败:文件数量不同
$ git checkout mytopic
error: You have local changes to ‘frotz’; not switching branches.
Shell
可以将-m标志赋给命令,这将尝试三路合并:
$ git checkout -m mytopic
Auto-merging frotz
Shell
在这种三路合并之后,本地的修改没有在索引文件中注册,所以git diff会显示从新分支的提示之后所做的更改。
示例-3
当使用-m选项切换分支时发生合并冲突时,会看到如下所示:
$ git checkout -m mytopic
Auto-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed
Shell
此时,git diff会显示上一个示例中干净合并的更改以及冲突文件中的更改。 编辑并解决冲突,并用常规方式用git add来标记它:
$ edit frotz # 编辑 frotz 文件中内容,然后重新添加,同名文件内容不同
$ git add frotz
Shell
其它示例
git checkout的主要功能就是迁出一个分支的特定版本。默认是迁出分支的HEAD版本一此用法示例:
$ git checkout master #//取出master版本的head。
$ git checkout tag_name #//在当前分支上 取出 tag_name 的版本
$ git checkout master file_name #//放弃当前对文件file_name的修改
$ git checkout commit_id file_name #//取文件file_name的 在commit_id是的版本。commit_id为 git commit 时的sha值。

$ git checkout -b dev/1.5.4 origin/dev/1.5.4

#从远程dev/1.5.4分支取得到本地分支/dev/1.5.4
$ git checkout – hello.rb
#这条命令把hello.rb从HEAD中签出.
$ git checkout .
#这条命令把 当前目录所有修改的文件 从HEAD中签出并且把它恢复成未修改时的样子.
#注意:在使用 git checkout 时,如果其对应的文件被修改过,那么该修改会被覆盖掉。

12.git merge

用于将两个或两个以上的开发历史加入(合并)一起。

示例-1
合并分支fixes和enhancements在当前分支的顶部,使它们合并:
$ git merge fixes enhancements
Shell
示例-2
合并obsolete分支到当前分支,使用ours合并策略:
$ git merge -s ours obsolete
Shell
示例-3
将分支maint合并到当前分支中,但不要自动进行新的提交:
$ git merge --no-commit maint
Shell
当您想要对合并进行进一步更改时,可以使用此选项,或者想要自己编写合并提交消息。应该不要滥用这个选项来潜入到合并提交中。小修补程序,如版本名称将是可以接受的。
示例-4
将分支dev合并到当前分支中,自动进行新的提交:
$ git merge dev//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/git/git_merge.html#article-start

13.git mergetool

用于运行合并冲突解决工具来解决合并冲突。

14.git log

用于显示提交日志信息

15.git tag

用于创建,列出,删除或验证使用GPG签名的标签对象。

[root@node2 soft]# git tag -h
usage: git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]
   or: git tag -d <tagname>...
   or: git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] 
		[<pattern>...]
   or: git tag -v <tagname>...

    -l, --list            list tag names
    -n[<n>]               print <n> lines of each tag message
    -d, --delete          delete tags
    -v, --verify          verify tags

Tag creation options
    -a, --annotate        annotated tag, needs a message
    -m, --message <message>
                          tag message
    -F, --file <file>     read message from file
    -s, --sign            annotated and GPG-signed tag
    --cleanup <mode>      how to strip spaces and #comments from message
    -u, --local-user <key id>
                          use another key to sign the tag
    -f, --force           replace the tag if exists
    --column[=<style>]    show tag list in columns

Tag listing options
    --contains <commit>   print only tags that contain the commit
    --points-at <object>  print only tags of the object

1.列显已有的标签
列出现有标签的命令非常简单,直接运行 git tag 即可:
$ $ git tag
v1.0
v1.2

$ git tag -l ‘v1.4.2.*’
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4
Shell
2.创建标签
标签有两种类型:轻量(lightweight)和注解(annotated)。
创建一个含附注类型的标签非常简单,用 -a (译注:取 annotated 的首字母)指定标签名字即可:
$ git tag -a v1.4 -m ‘my version 1.4’
$ git tag
v0.1
v1.3
v1.4
Shell
-m 选项则指定了对应的标签说明,Git 会将此说明一同保存在标签对象中。如果没有给出该选项,Git 会启动文本编辑软件供你输入标签说明。
可以使用 git show 命令查看相应标签的版本信息,并连同显示打标签时的提交对象。
$ git show v1.4
tag v1.4
Tagger: Scott Chacon schacon@gee-mail.com
Date: Mon Feb 9 14:45:11 2009 -0800

my version 1.4

commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7… a6b4c97…
Author: Scott Chacon schacon@gee-mail.com
Date: Sun Feb 8 19:02:46 2009 -0800

Merge branch 'experiment'

我们可以看到在提交对象信息上面,列出了此标签的提交者和提交时间,以及相应的标签说明。
3.签署标签
需要使用gpg创建私钥。
如果你有自己的私钥,还可以用 GPG 来签署标签,只需要把之前的选项 -a 改为 -s:
$ git tag -s v1.5 -m ‘my signed 1.5 tag’
You need a passphrase to unlock the secret key for
user: “Scott Chacon schacon@gee-mail.com
1024-bit DSA key, ID F721C45A, created 2009-02-09
Shell
现在再运行 git show 会看到对应的 GPG 签名也附在其内:
$ git show v1.5
tag v1.5
Tagger: Scott Chacon schacon@gee-mail.com
Date: Mon Feb 9 15:22:20 2009 -0800

my signed 1.5 tag
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEABECAAYFAkmQurIACgkQON3DxfchxFr5cACeIMN+ZxLKggJQf0QYiQBwgySN
Ki0An2JeAVUCAiJ7Ox6ZEtK+NvZAj82/
=WryJ
-----END PGP SIGNATURE-----
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7… a6b4c97…
Author: Scott Chacon schacon@gee-mail.com
Date: Sun Feb 8 19:02:46 2009 -0800

Merge branch 'experiment'

Shell
4.删除标签
很简单,比如想要名称删除名称为:v1.0的标签,可以执行以下操作:
$ git tag -d v1.0

5.轻量级标签
轻量级标签实际上就是已经创建标签的tag再创建一个标签。 -a,-s 或 -m 选项都不用,直接给出标签名字。
$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5
Shell
现在运行 git show 查看此标签信息,就只有相应的提交对象摘要:
$ git show v1.4-lw
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7… a6b4c97…
Author: Scott Chacon schacon@gee-mail.com
Date: Sun Feb 8 19:02:46 2009 -0800

Merge branch 'experiment'

6.验证标签
可以使用 git tag -v [tag-name] (译注:取 verify 的首字母)的方式验证已经签署的标签。此命令会调用 GPG 来验证签名,所以你需要有签署者的公钥,存放在 keyring 中,才能验证:
$ git tag -v v1.4.2.1
object 883653babd8ee7ea23e6a5c392bb739348b1eb61
type commit
tag v1.4.2.1
tagger Junio C Hamano junkio@cox.net 1158138501 -0700

GIT 1.4.2.1

Minor fixes since 1.4.2, including git-mv and git-http with alternates.
gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Good signature from “Junio C Hamano junkio@cox.net
gpg: aka “[jpeg image of size 1513]”
Primary key fingerprint: 3565 2A26 2040 E066 C9A7 4A7D C0C6 D9A4 F311 9B9A
Shell
若是没有签署者的公钥,会报告类似下面这样的错误:
gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Can’t check signature: public key not found
error: could not verify the tag ‘v1.4.2.1’
Shell
7.后期加注标签
甚至可以在后期对早先的某次提交加注标签。比如在下面展示的提交历史中:
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch ‘experiment’
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch ‘experiment’
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme
Shell
我们忘了在提交 “updated rakefile” 后为此项目打上版本号 v1.2,没关系,现在也能做。只要在打标签的时候跟上对应提交对象的校验和(或前几位字符)即可:
$ git tag -a v1.2 9fceb02
Shell
现在,可以看到我们已经补上了标签:
$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5

$ git show v1.2
tag v1.2
Tagger: Scott Chacon schacon@gee-mail.com
Date: Mon Feb 9 15:32:16 2009 -0800

version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon mchacon@gee-mail.com
Date: Sun Apr 27 20:43:35 2008 -0700

updated rakefile


Shell
8.分享标签
默认情况下,git push 并不会把标签传送到远端服务器上,只有通过显式命令才能分享标签到远端仓库。其命令格式如同推送分支,运行 git push origin [tagname] 即可:
$ git push origin v1.5
Counting objects: 50, done.
Compressing objects: 100% (38/38), done.
Writing objects: 100% (44/44), 4.56 KiB, done.
Total 44 (delta 18), reused 8 (delta 1)
To git@github.com:schacon/simplegit.git

  • [new tag] v1.5 -> v1.5
    Shell
    如果要一次推送所有本地新增的标签上去,可以使用 --tags 选项:
    $ git push origin --tags
    Counting objects: 50, done.
    Compressing objects: 100% (38/38), done.
    Writing objects: 100% (44/44), 4.56 KiB, done.
    Total 44 (delta 18), reused 8 (delta 1)
    To git@github.com:schacon/simplegit.git
  • [new tag] v0.1 -> v0.1
  • [new tag] v1.2 -> v1.2
  • [new tag] v1.4 -> v1.4
  • [new tag] v1.4-lw -> v1.4-lw
  • [new tag] v1.5 -> v1.5
    Shell
    现在,其他人克隆共享仓库或拉取数据同步后,也会看到这些标签。//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/git/git_tag.html#article-start

————Blueicex 2020/2/7 15:26 blueice1980@126.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值