文章目录
  1. 1. 引言
  2. 2. HTTPS 方式登录
    1. 2.1. config
  3. 3. SSH 方式登录
  4. 4. 基本操作
  5. 5. 复杂操作
  6. 6. 关于分支
  7. 7. 关于标签
  8. 8. 更改提交的操作
  9. 9. 推送到远程仓库
  10. 10. 从远程仓库获取
  11. 11. 补充
  12. 12. 别名
  13. 13. FQA

引言


版本管理就是管理更新的历史记录,它为我们提供了一些在软件开发过程中必不可少的功能,例如记录一款软件添加或更改源代码的过程,回滚到特定的阶段,恢复误删除的文件等。

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals

服务器托管:GitHub、GitLab、Bitbucket
客户端软件:GitHub、SourceTree

使用 git help 查看命令手册,使用 git help commond 可以查看其他命令使用。

HTTPS 方式登录


0、在终端中输入下面命令,会在 ~/.gitconfig 中有所体现。

1
2
$ git config --global user.name "userName"
$ git config --global user.email "userEmail"

1、编辑 vim ~/.gitconfig ,类似显示内容为:

1
2
3
[user]
name = userName
email = userEmail

2、显示配置内容

1
$ git config --list

config


使用 config 命令就是更改 ~/.gitconfig 里面的内容
配置用户名和邮箱,请参考上面信息
将配置应用到整个系统中 git config --global
局部:.git/config

SSH 方式登录


请参考 SSH 登录配置文章

基本操作


0、在磁盘上创建一个文件夹(devhitao),用 git init 命令将此文件夹初始化为 Git 版本库;

1
$ git init

1、devhitao 文件是一个空的 Git 仓库, 用 git add 命令将之前创建的 devhitao.txt 文件纳入到 Git 版本仓库中;

1
$ git add devhitao.txt // 如需添加多个文件,用英文逗号分割, 也可以使用 "." ,  它代表所有文件

2、使用 git commit 命令来做一次本地保存, 并在 -m 字符后面添加提交说明;

1
$ git commit -m 'devhitao first commit'

3、远程仓库
3.1、查看远程repo

1
2
3
$ git remote -v
or
$ cat .git/config

3.2、添加远程仓库

1
$ git remote add origin 'ssh://url.git 或 https://url.git'

3.3、移除远程repo

1
$ git remote rm origin

3.4、重置本地远程的URL(注:上面步骤1和2与本步实现的效果相同)

1
$ git remote set-url origin git/https.git

4、推送本地项目到 github

1
$ git push -u origin master

5、压缩本地资源

1
$ git gc --auto

基本用法表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status

grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

参考资料


0、Changing a remote’s URL

复杂操作


0、在本地初始化仓库,会在该目录下生成 .git 目录,里面存储着管理当前目录内容所需的仓库数据,初始化完的目录,我们称之为工作树。所有的操作都在工作树中进行,然后记录到仓库中,以此来管理文件的历史快照,也是就是历史版本。

1
2
3
$ mkdir execrise
$ cd execrise
$ git init

1、查看仓库的状态, Untracked files 没有提交的文件(不是 Git 仓库的管理对象)。

1
$ git status

2、向暂存区中添加文件,暂存区是提交之前的一个临时区域。 git add . 是将本目录下所有的文件,都提交到暂存区。

1
$ git add README.md

3、将暂存区中的文件实际保存到仓库的历史记录中,通过这些记录,我们可以在工作树种复用文件。(注:-m "提交的信息" 可以不写,可能回跳到编辑器,我们可以填写更多的信息)

1
$ git commit -m "提交的信息"

4、查看提交日志-提交内容差异:

1
2
3
4
5
6
$ git log //查看以往仓库中提交的日志
$ git log --pretty=short //只显示提交信息的第一行
$ git log 文件名or目录名 //只显示指定目录、文件的日志
$ git log -p //显示文件的改动
$ git log -p 文件名 //只查看该文件日志提交前后的差别
$ git log --graph //以图表形式查看分支,适合在分支合并后查看。

5、查看更改前后的差别(注:HEAD 是指向当前分支中最新一次提交的指针)

1
2
3
$ git diff //查看工作树与暂存区的差别
$ git diff HEAD //在 commit 之前,查看本次提交与上次提交之间的差别
$

关于分支


0、显示分支,“*” 代表我们当前所在的分支;

1
$ git branch

1、创建、切换分支(A)

1
2
3
4
$ git checkout -b A
//上面一条命令与下面两条命令实现的效果相同
$ git branch A
$ git checkout A

2、在相应的分支下(A),对代码进行修改,然后添加到暂存区,最后提交。

1
2
3
$ echo "# AA" >>README.md
$ git add README.md
$ git commit -m "Add AA"

3、在切换到 master 分支,我们并没有修改 master 下的README.md文件。可见我们可以在多个分支下同时开发多个功能。

1
2
$ git checkout master //切换到master分支
$ git checkout - //切换回上一分支

4、合并分支,master通常作为主干分支,通俗的讲就是根节点(终点),根节点只有一个,但是我们可以使用 Tag 标签进行标记,管理多个版本的发布。

1
2
$ git checkout master //切换到master分支
$ git merge --no-ff A //参数--no-ff,在历史记录中明确记录本次分支的合并,

5、当我们合并分支时,我们会遇到合并冲突,其解决办法很简单,用文本编辑器打开冲突文件,与相关人员进行商量,如何进行修改,修改完后,提交解决后的结果 git add (文件名)git commit -m "修改的内容"
6、删除本地分支

1
$ git branch -d new_branch // 大写 D 表示强制删除

7、回退历史,使用 git log 查出历史记录,

1
2
$ git checkout f903b087a // 取 9 位即可
$ git checkout -b new_branch f903b087a // 检索出的放在另一个分支之中

8、删除远程分支:

1
git push origin --delete <remote_branch_name>

在运行上面命令时,可能会遇到 error: unable to delete ‘origin/fixbug’: remote ref does not exist 情况;造成这种情况的原因是服务器上该远程分支不存在。

如果你想同步本地远程分支和服务上远程分支。可以通过下面命令对本地远程分支进行:

1
git fetch --prune

关于标签


0、列出已有的标签

1
2
$ git tag
$ git tag -l 'v1.0' //用特定模式查找标签

1、创建标签,标签分为:轻量标签和附注标签

  0、创建附注标签

1
  $ git tag -a V1.0 -m "create V1.0" // -a 即 --annotate 命令

  通过 git show (V1.0) 可以获得标签信息与对应的提交信息
  
  1、创建轻量标签

1
$ git tag V1.2

2、后期打标签

  0、通过 git log --pretty=oneline 获得提交的历史校验和
  1、通过 git tag -a V1.3 校验和(可以是部分校验和)

3、共享标签:创建完标签之后,我们必须显示的将标签推送到响应的服务器上,向共享分支一样 git push origin [tagname]

1
2
$ git push origin V1.0 //推送一个标签
$ git push origin --tags //会把所有不在服务器上标签都会推送上去

4、检出标签

在 Git 中,你并不能真的检出一个标签,因为它们并不像分支一样来回移动。如果你想要工作目录与仓库中特定的标签版本完全相同,可以使用 git checkout -b [branchname] [tagname],在特定的标签上创建一个新分支,但是如果在这之后又进行一次提交,version2branch 分支会因为向前移动,那么version2branch 就会与 V2.0 标签稍微有些改变,要小心哦!

1
$ git checkout -b version2.0branch V2.0 //在特定的标签上创建一个新分支

5、修剪本地标签

1
git fetch --prune-tags

更改提交的操作


0、回溯到相应的历史版本,需要提供目标时间点的哈希值。

1
$ git reset --hard (哈希值)

1、使用 git reflog 可以查看当前仓库的操作日志,而 git log 命令只能查看以当前状态为终点的历史日志。

1
$ git reset --hard (哈希值) //该哈希值由 reflog 命令获得,每个版本对应一个哈希值

2、修改提交信息

1
$ git commit --amend //会启动编辑器,在其中修改需要提交的信息即可

3、压缩历史

  0、建立一个分支,修改内容后进行提交。(小的变更,不必先执行 git addgit commit,我们用 git commit -am "提交信息" 进行一次提交)

  1、修改提交内容中包含的小错误,修改后再使用 git commit -am 修改信息",提交到暂存区。

  2、更改历史:使用 git rebase -i HEAD~2,将修改和上次提交的历史合为一处,进行完美提交。在随之打开的编辑器中,将第二行的 pick 改为 fixup ,关闭编辑器,我们可以使用 git log --graph 命令,查看提交的历史。

  3、最后合并到 master 分支。

推送到远程仓库


0、在GitHub上建立远程仓库,名字要与本地推送的仓库名字一样。

1、添加远程仓库,origin 是标识符

1
$ git remote add origin "SSH/HTTPS***.git"

2、推送至远程仓库

  0、推送至 master 分支

1
  $ git push -u origin master

  1、推送到 master 以外的分支,建议切换到要推送的分支(A)。

1
  $ git push -u origin A

从远程仓库获取


0、从远程服务器 clone 将代码克隆到本地,使用 git branch -a 可以查看本地仓库与远程仓库的分支信息。

1
$ git clone https://....git

1、获取远程的 A 分支到本地仓库。

1
$ git checkout -b A origin/A

2、在本地对 A 分支进行修改后,进行推送。

1
$ git push

3、从远程仓库获取最新的信息到本地

1
2
$ git pull  //获得整个最新消息
$ git pull origin A //获取最新的A分支

补充


open .git //打开文件
touch //添加文件
git diff //查看未添加(add)
git diff –staged //查看添加的(add)
git mv old.h new.h //重命名
git rm oldNew.h // 删除文件
git reset的几个选项
–soft 不会影响工作区和暂存区
–hard 会影响
–mixed 会影响暂存区
git branch -m oldFile newFile //重命名
git log –oneline //在一行上显示提交日志
git branch -r #查看远程分支

别名


可以为 git 命令设置别名类似以下命令,可以使用 git ci 进行提交。

1
$ git config --global alias.ci 'commit -m' // 可以是命令 or 命令 + 参数

fatal: ‘origin/xxx’ is not a commit and a branch ‘xxx’ cannot be created from it

FQA


Q:当在本地拉去远程分支时,可能会遇到 fatal: ‘origin/xxx’ is not a commit and a branch ‘xxx’ cannot be created from it 错误,原因是未检测到远程分支变化,需要在本地同步一下仓库状态。

A:如果你使用 Xcode 进行分支管理,可以点击 Source Control -> Fetch and Refresh Status 中操作,Xcode 会帮助我们获取更改并更新当前工作副本中文件的状态。

也可以自己使用命令行方式进行更新:

1
2
3
git fetch --all #Fetch all remotes
or
git pull # 拉取最新数据并将数据合并到本地


参考资料


文章目录
  1. 1. 引言
  2. 2. HTTPS 方式登录
    1. 2.1. config
  3. 3. SSH 方式登录
  4. 4. 基本操作
  5. 5. 复杂操作
  6. 6. 关于分支
  7. 7. 关于标签
  8. 8. 更改提交的操作
  9. 9. 推送到远程仓库
  10. 10. 从远程仓库获取
  11. 11. 补充
  12. 12. 别名
  13. 13. FQA