(二)git基础

获取帮助

  • git help
    获取git命令的简要描述
  • man git
    获取git的详细描述
  • 获取单个命令的详细描述
    git help cmd,git help config
  • 获取单个命令的简要描述
    git cmd -h或者git cmd --help
    例如:git add -h 或 git add --help

git环境配置

1. 配置文件
1、/etc/gitconfig 文件: 包含系统上每一个用户及他们仓库的通用配置。 如果使用带有 --system 选项的 git config 时,它会从此文件读写配置变量。
2、~/.gitconfig 或 ~/.config/git/config 文件:只针对当前用户。 可以传递 --global 选项让 Git 读写此文件。
3、当前使用仓库的 Git 目录中的 config 文件(就是 .git/config):针对该仓库。

每一个级别覆盖上一级别的配置,所以 .git/config 的配置变量会覆盖 /etc/gitconfig 中的配置变量。在 Windows 系统中,Git 会查找 $HOME 目录下(一般情况下是 C:\Users\$USER)的 .gitconfig 文件。 Git 同样也会寻找 /etc/gitconfig 文件,但只限于 MSys 的根目录下,即安装 Git 时所选的目标位置。Git 同样也会寻找 /etc/gitconfig 文件,但只限于 MSys 的根目录下,即安装 Git 时所选的目标位置。

2.常用配置

用户信息

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

文本编辑器

$ git config --global core.editor emacs
3.查看配置

检查配置信息

$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto

查看某一项配置

// git config <key>
$ git config user.name
John Doe

git基础

1. 获取一个仓库

创建一个仓库
在要建立为仓库的目录下执行

git init

注意: 当服务器端仓库初始创建为空仓库时,本地仓库更新添加文件,在初始化仓库时需加上–bare,否则,本地仓库push文件时,会出如下错误

$ git commit -m "adfa"
[master (root-commit) 4a52dab] adfa
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
binn@ubuntu:~/error_test$ git push origin master 
git@192.168.132.24's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 198 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@192.168.132.24:/home/git_server/error
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@192.168.132.24:/home/git_server/error'

根据提示也可以解决该问题,但是不是最佳方案。

克隆一个仓库

git clone url

例如:

git clone https://github.com/libgit2/libgit2

克隆一个和远端一样的仓库,或

git clone https://github.com/libgit2/libgit2 mylibgit

克隆一个仅仓库名不同,其他相同的仓库

2. 跟踪文件

文件的状态变化周期
在这里插入图片描述
查看状态

git status

状态简要展示

git status -s

添加跟踪文件

git add 目录或文件

忽略文件
在某个目录下添加.gitignore文件,文件内容格式规则:

  • 所有空行或者以“#”开头的行都会被 Git 忽略。
  • 可以使用标准的 glob 模式匹配。
  • 匹配模式可以以“/”开头防止递归。
  • 匹配模式可以以“/”结尾指定目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号“!”取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。
.gitignore 文件的例子:

# 不跟踪以.a结束的文件
*.a

# 上一个规则的例外,除了lib.a,其他都不跟踪
!lib.a

# 仅忽略当前目录下TODO文件夹,其他文件夹的子文件夹不受影响
/TODO

# 忽略所有build目录
build/

# 忽略doc目录下的txt文件,不影响doc子目录下的txt文件,如doc/server/1.txt
doc/*.txt

# 忽略所有doc目录下的pdf文件,包括doc目录下子目录的pdf文件
doc/**/*.pdf

查看尚未暂存的文件及其修改

# 工作区文件和索引区文件的对比
git diff

查看将要commit(索引区)的文件和本地仓库文件的区别

git diff --staged
// 等同git diff --cached

提交更新到本地仓库

git commit -m mesg
// 例如:git commit -m "Story 182: Fix benchmarks for speed"

一般提交,需要把文件add到索引区(也即缓存区),然后再提交。跳过使用暂存区域,直接提交到本地仓库

git commit -a mesg
// 例如:git commit -a -m 'added new benchmarks'

移除文件

git rm files
// 等同
rm files
git add files
git commit -m mesg

若文件已修改且提交到缓存区,增加选项-f

git rm -f files

从仓库中删除,不删除工作区文件

git rm --cached files

移动文件

git mv src  dest
// 等同
mv src dest
git rm src
git add dest
3.查看修改纪录

查看历史提交记录

git log

查看最近2次提交及差异

$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
 spec = Gem::Specification.new do |s|
     s.platform  =   Gem::Platform::RUBY
     s.name      =   "simplegit"
-    s.version   =   "0.1.0"
+    s.version   =   "0.1.1"
     s.author    =   "Scott Chacon"
     s.email     =   "schacon@gee-mail.com"
     s.summary   =   "A simple gem for using Git in Ruby code."

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae..47c6340 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit
     end

 end
-
-if $0 == __FILE__
-  git = SimpleGit.new
-  puts git.show
-end
\ No newline at end of file

查看提交并简要显示修改统计

$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

 Rakefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

修改纪录格式化输出

// 详细了解$ git log --pretty=的使用,例如
// 单行输出
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit

修改记录按照限定输出

部分选项如下,详细请查看git help log
选项								说明
-(n)    							仅显示最近的 n 条提交
--since, --after					仅显示指定时间之后的提交。
--until, --before					仅显示指定时间之前的提交。
--author							仅显示指定作者相关的提交。
--committer							仅显示指定提交者相关的提交。
--grep								仅显示含指定关键字的提交
-S									仅显示添加或移除了某个关键字的提交
4.撤销操作

若提交漏了文件,可以使用如下命令

git add files
git commit --amend

如此操作,git log显示的提交为同一次

取消暂存文件,在git status显示中也有相应提示

git reset HEAD  files

撤销对文件的修改,在git status显示中也有相应提示

git checkout -- files
5.远程仓库的使用

克隆远程仓库

// 用于初始化本地仓库,并与远程仓库有联系
git clone https://github.com/schacon/ticgit

查看远程仓库

// 显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL
git remote -v

添加远程仓库

git remote add <shortname> <url>

例如:

$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)
pb	https://github.com/paulboone/ticgit (fetch)
pb	https://github.com/paulboone/ticgit (push)

origin、pb为远程服务器的仓库名

拉取远程仓库和本地仓库的区别部分

$ git fetch [remote-name]

这个命令会访问远程仓库,从中拉取所有你还没有的数据。 执行完成后,你将会拥有那个远程仓库中所有分支的引用,可以随时合并或查看。必须注意 git fetch 命令会将数据拉取到你的本地仓库 - 它并不会自动合并或修改你当前的工作。 当准备好时你必须手动将其合并入你的工作。 如果你有一个分支设置为跟踪一个远程分支,可以使用 git pull 命令来自动的抓取然后合并远程分支到当前分支

推送到远程仓库

git push [remote-name] [branch-name]

例如,把本地master分支推动到远程origin服务器

$ git push origin master

若远程服务器仓库存在相同分支,后面章节说明解决方案。

查看远程仓库

git remote show [remote-name] 

例如查看远程仓库origin信息

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           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 rm paul

重命名远程仓库

$ git remote rename pb paul
$ git remote
origin
paul

值得注意的是这同样也会修改你的远程分支名字。 那些过去引用 pb/master 的现在会引用 paul/master。

6.打标签

像其他版本控制系统(VCS)一样,Git 可以给历史中的某一个提交打上标签,以示重要。 比较有代表性的是人们会使用这个功能来标记发布结点(v1.0 等等)。 在本节中,你将会学习如何列出已有的标签、如何创建新标签、以及不同类型的标签分别是什么。

列出标签

$ git tag
v0.1
v1.3

使用特定模式查找标签

$ git tag -l 'v1.8.5*'
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
v1.8.5-rc2

创建标签
轻量标签: 一个不会改变的分支 - 它只是一个特定提交的引用。
附注标签: 存储在 Git 数据库中的一个完整对象。 它们是可以被校验的;其中包含打标签者的名字、电子邮件地址、日期时间;还有一个标签信息;并且可以使用 GNU Privacy Guard (GPG)签名与验证。 通常建议创建附注标签,这样你可以拥有以上所有信息
建议: 如果你只是想用一个临时的标签,或者因为某些原因不想要保存那些信息,轻量标签也是可用的。

附注标签
创建标签

// 在运行 tag 命令时指定 -a 选项
// -m 选项指定了一条将会存储在标签中的信息。若未指定,会进入编辑器
$ git tag -a v1.4 -m 'my version 1.4'
$ git tag
v0.1
v1.3
v1.4

查看标签信息

$ git show v1.4
tag v1.4
Tagger: Ben Straub <ben@straub.cc>
Date:   Sat May 3 20:19:12 2014 -0700

my version 1.4

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

轻量标签
创建标签

// 不需要使用-a -m选项
$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw

查看标签方式同附注标签

后期补打标签

git tag [选项] tag commitID
// 例如
$ git tag -a v1.2 9fceb02

共享标签

// 默认情况下,git push 命令并不会传送标签到远程仓库服务器上。 
// 在创建完标签后你必须显式地推送标签到共享服务器上。 
// 这个过程就像共享远程分支一样 - 你可以运行 git push origin [tagname]
$ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
 * [new tag]         v1.5 -> v1.5

删除标签

// 使用命令 git tag -d <tagname>
$ git tag -d v1.4-lw
// 同时更新到远端服务器,必须使用 git push <remote> :refs/tags/<tagname>
$ git push origin :refs/tags/v1.4-lw
To /git@github.com:schacon/simplegit.git
 - [deleted]         v1.4-lw

检出标签

// git checkout tag
$ git checkout 2.0.0

副作用: 检出标签的版本处于“分离头指针”状态,不能做修改,若修改提交,不属于任何分支。若需要修改,需要创建新的分支,例如:

$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'

此时version2版本和v2.0.0版本不同。

7.git别名

Git 并不会在你输入部分命令时自动推断出你想要的命令。 如果不想每次都输入完整的 Git 命令,可以通过 git config 文件来轻松地为每一个命令设置一个别名。 例如:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

例如:

$ git config --global alias.unstage 'reset HEAD --'
// 以下两个命令等同
$ git unstage fileA
$ git reset HEAD -- fileA

查看最新一次的提交

$ git config --global alias.last 'log -1 HEAD'
// 使用如下
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <dreamer3@example.com>
Date:   Tue Aug 26 19:48:51 2008 +0800

    test for current head

    Signed-off-by: Scott Chacon <schacon@example.com>

Git 只是简单地将别名替换为对应的命令。 然而,你可能想要执行外部命令,而不是一个 Git 子命令。 如果是那样的话,可以在命令前面加入 ! 符号。 如果你自己要写一些与 Git 仓库协作的工具的话,那会很有用。 我们现在演示将 git visual 定义为 gitk 的别名:

$ git config --global alias.visual '!gitk'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值