Git版本控制_&_分支管理

文章目录

Git基本操作

1. git 两大特点

  • 版本控制:可以解决多人同时开发的代码问题,也可以解决找回历史代码的问题

  • 分布式: Git是分布式的版本控制系统,同一个Git仓库,可以分不到不同的机器上。

    • 首先找一台电脑充当服务器的角色,7*24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑,
      并且把各自的提交 推送(commit/push) 到服务器仓库里,也可以从服务器仓库中 拉取(pull) 别人的提交。

    • 可以自己搭建这台“服务器”,也可以使用GitHub。
      在这里插入图片描述

2. 安装与配置

  • Ubuntu 系统为例:
sudo apt-get install git
  • 安装成功之后 用git 查看相关指令说明,git version 查看版本号
[fuli@16:20:58]~/PycharmProjects/git_action$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

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.

[fuli@16:19:34]~/PycharmProjects/git_action$ git version
git version 2.15.1 (Apple Git-101)

3. 创建一个版本库

  • 新建一个目录 或者 cd到需要用git管理的目录下, 执行 git init,会生成一个.git 隐藏目录,帮助管理该目录(directory) 下的文件
[fuli@16:23:00]~/PycharmProjects/git_action$ git init
Initialized empty Git repository in /Users/fuli/PycharmProjects/git_action/.git/

4.版本创建与回退

4.1 使用

  1. git_action 目录下创建一个文件 code.txt, 编辑内容如下:
[fuli@16:23:07]~/PycharmProjects/git_action$ vim code.txt
[fuli@16:31:54]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
  1. 使用下面两条命令可以创建一个版本
git add code.txt

git commit -m "版本注释, for instance: 版本1.123"

操作如下:

[fuli@16:47:33]~/PycharmProjects/git_action$ git log
fatal: your current branch 'master' does not have any commits yet
[fuli@16:47:39]~/PycharmProjects/git_action$ git add code.txt 
[fuli@16:47:46]~/PycharmProjects/git_action$ git commit -m "版本0.01"
[master (root-commit) ba26141] 版本0.01
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 code.txt
  1. 使用 git log 可以查看版本记录
[fuli@16:48:22]~/PycharmProjects/git_action$ git log
commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@16:52:14]~/PycharmProjects/git_action$ 
  1. 继续编辑 code.txt, 增加一行, 然后创建一个新版本,再查看日志信息。
[fuli@16:52:14]~/PycharmProjects/git_action$ vim code.txt 
[fuli@16:57:15]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.
[fuli@16:57:18]~/PycharmProjects/git_action$ git add code.txt 
[fuli@16:58:17]~/PycharmProjects/git_action$ git commit -m "版本0.02"
[master bbf036c] 版本0.02
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
[fuli@16:58:35]~/PycharmProjects/git_action$ git log
commit bbf036c780245dffa56052047d6b0d1bbae939d0 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
  • git在管理不同的版本时,只记录当前版本相较于上一个版本发生了哪些变化,不会拷贝之前版本没有修改的内容。 上两步的操作如图所示:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o4TogVQD-1590760213645)(./img/WechatIMG94.png)]

  1. 版本回退:若想回到之前的某一个版本,可以使用git reset --hard HEAD^
    • 其中 HEAD 表示当前最新的版本, HEAD^表示当前版本的前一个版本, HEAD^^ 表示当前版本的前前一个版本(往前回退2次).

    • 也可以使用 HEAD~1 表示当前版本的前一个版本,HEAD~100 表示当前版本的前100个版本。

# 回退到上一个版本
[fuli@16:58:45]~/PycharmProjects/git_action$ git reset --hard HEAD^
HEAD is now at ba26141 版本0.01
[fuli@17:35:02]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
[fuli@17:35:09]~/PycharmProjects/git_action$ git log
commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
  • 回退之后,git并没有删除之前的信息,而是将头指针移动到了对应的位置。上面操作之后的示意图如下:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c7T4aaRS-1590760213647)(./img/WechatIMG97.png)]
  1. 如果再想返回到最新版本,需要用到版本号信息。
    • 即 git也可以用版本号直接指定HEAD指向的版本信息,格式为 git reset --hard 版本号

    • 这里所说的版本号是 在每次 commit 之后,系统会返回版本号信息,如下

      • commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
    • 可以用 git reflog 查看之前的操作记录,该操作记录会显示每次 commit 的版本号信息。

  • 例如:重新返回到 版本0.02
# 执行 git reflog 之后,返回的每一行最前面的值 即为版本号的 前7位,可以只输入这7为 返回到其对应的版本。
[fuli@17:45:30]~/PycharmProjects/git_action$ git reflog
ba26141 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
bbf036c HEAD@{1}: commit: 版本0.02
ba26141 (HEAD -> master) HEAD@{2}: commit (initial): 版本0.01
[fuli@17:47:25]~/PycharmProjects/git_action$ git reset --hard bbf036c
HEAD is now at bbf036c 版本0.02
[fuli@17:47:46]~/PycharmProjects/git_action$ git log
commit bbf036c780245dffa56052047d6b0d1bbae939d0 (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@17:47:52]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

4.2 工作区和版本库

工作区 ( Working Directory )

  • pc中的目录,比如当前的目录 git_action 就是一个工作区。

版本库 ( Repository )

  • 工作区有一个隐藏目录 .git,这个隐藏目录就是 git 的版本库。

    • git 版本库里存了很多东西,其中最重要的就是称为 stage 或者叫 (index) 的暂存区,还有git自动创建的第一个分支 master ,以及指向 master 的一个指针 HEAD.

    • 在创建 git 版本库时, git 自动创建了唯一一个 master 分区。 故, git commit 默认是往 master 分支上提交修改。

    • 可以理解为:需要提交的文件修改通通放到暂存区( git add 文件名 , 可以多次 add ),然后一次性提交暂存区的所有修改(一次 git commit -m "注释")。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4LaoJ8RM-1590760213649)(./img/WechatIMG93.png)]

  • 前面提到:把文件往 git 版本库中添加分两步执行。
      1. git add 文件名 把文件添加进去,实际上是把文件修改添加到了暂存区
      1. git commit -m "注释" 提交更改,实际上是把暂存区的所有内容提交到当前分支。

    1. 下面 再创建一个 code2.txt 并编辑内容为 添加一行,
    1. 然后再次编辑 code.txt 内容,在其中添加一行
[fuli@20:11:41]~/PycharmProjects/git_action$ vim code2.txt
[fuli@20:13:38]~/PycharmProjects/git_action$ cat code2.txt 
This is code2's first line...
[fuli@20:13:43]~/PycharmProjects/git_action$ vim code.txt 
[fuli@20:22:28]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

    1. 使用 git status 查看当前工作树的状态。
[fuli@20:22:34]~/PycharmProjects/git_action$ git status
On branch master
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:   code.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	code2.txt

no changes added to commit (use "git add" and/or "git commit -a")

其中,Changes not staged for commit 表示 尚未暂存椅背提交的变更,即 code.txt 修改的内容还未 git add

Untracked files 表示 未跟踪的文件,也即新建的 code2.txt 还未 git add

no changes added to commit 表示修改还未加入提交。


接着执行下面操作, 将修改添加到暂存区

[fuli@20:29:42]~/PycharmProjects/git_action$ git add code.txt code2.txt 
[fuli@20:29:54]~/PycharmProjects/git_action$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   code.txt
	new file:   code2.txt

    1. 最后执行 git commit -m "注释"一次性把所有的修改提交到分支,创建一个版本
[fuli@20:29:59]~/PycharmProjects/git_action$ git commit -m "版本3"
[master aa4e0e8] 版本3
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 2 files changed, 3 insertions(+)
 create mode 100644 code2.txt
[fuli@20:32:17]~/PycharmProjects/git_action$ git status
On branch master
nothing to commit, working tree clean
[fuli@20:32:23]~/PycharmProjects/git_action$ git log
commit aa4e0e8732d92d5b46c6a0db37e05442ed0d84da (HEAD -> master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 20:32:17 2020 +0800

    版本3

commit bbf036c780245dffa56052047d6b0d1bbae939d0
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JCMOns8x-1590760213650)(./img/WechatIMG95.png)]


4.3 管理修改

  • git 管理文件:只会提交暂存区的修改来创建一个新的版本

  • 一个例子:

    • code.txt 中新增一行

    • 执行 git add <file>,将修改添加到暂存区

    • 再在code.txt 中新增一行. (这次的修改并没有 git add)

    • 执行 git commit -m "版本4", 创建一个新版本。

    • 使用 git status 查看工作树(the working tree)的状态,发现第二次修改由于没有被添加到暂存区,导致新创建的版本中没有该修改记录。

[fuli@22:53:51]~/PycharmProjects/git_action$ ls
code.txt  code2.txt
[fuli@22:53:53]~/PycharmProjects/git_action$ vim code.txt 
[fuli@22:54:26]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...
[fuli@22:54:32]~/PycharmProjects/git_action$ git add code.txt 
[fuli@22:54:42]~/PycharmProjects/git_action$ vim code.txt 
[fuli@22:55:20]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...

the new line...
[fuli@22:55:24]~/PycharmProjects/git_action$ git commit -m "版本4"
[master 7e9087b] 版本4
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
[fuli@22:55:41]~/PycharmProjects/git_action$ git status
On branch master
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:   code.txt

no changes added to commit (use "git add" and/or "git commit -a")

4.4 撤销修改

  • 继续 4.3 节的操作

  • 如果要撤销的修改 还未添加到暂存区

    • 可以使用 git checkout -- <file_name> 来丢弃工作区的改动。

    • 执行该操作会撤销 该文件未添加到暂存区的修改

  • 如果要撤销的修改 已经添加到暂存区

    • 限制性 git reset HEAD <file_name> 取消暂存

    • 再使用 git checkout -- <file_name> 来丢弃工作区的改动。

    • 执行该操作会撤销 该文件未添加到暂存区的修改

  • 如果要撤销的修改 已经到创建了版本记录, 即commit到了对应的分支

    • 使用之前提到的 git reset --hard <版本号>, 回退到之前版本
[fuli@23:09:10]~/PycharmProjects/git_action$ git status
On branch master
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:   code.txt
no changes added to commit (use "git add" and/or "git commit -a")
[fuli@23:09:14]~/PycharmProjects/git_action$ git add code.txt 
[fuli@23:09:24]~/PycharmProjects/git_action$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	modified:   code.txt
[fuli@23:09:27]~/PycharmProjects/git_action$ git reset HEAD code.txt 
Unstaged changes after reset:
M	code.txt
[fuli@23:09:41]~/PycharmProjects/git_action$ git status
On branch master
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:   code.txt
no changes added to commit (use "git add" and/or "git commit -a")
[fuli@23:09:43]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
the new line...
[fuli@23:10:11]~/PycharmProjects/git_action$ git checkout -- code.txt 
[fuli@23:10:23]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
[fuli@23:10:27]~/PycharmProjects/git_action$ git status
On branch master
nothing to commit, working tree clean

4.5 对比文件的不同

  • 用法 git diff [<options>] [<commit> [<commit>]] [--] [<path>...]

  • 对比工作区 和 版本中文件的不同

    • git diff HEAD -- <file_name>

    • 例: 继续编辑code.txt ,添加一行,

    • 对比工作区中 code.txtHEAD版本中 code.txt 的不同,执行 git diff HEAD -- code.txt

  • 对比 两个版本中 文件的不同

    • git diff HEAD HEAD^ -- <file_name>

    • 执行 git diff HEAD HEAD^ -- code.txt

[fuli@23:18:25]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...
[fuli@23:18:29]~/PycharmProjects/git_action$ vim code.txt 
[fuli@23:19:08]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17

This is the second line.

This is the third line....

This is the fourth line...

neeeeeew line...................
[fuli@23:19:17]~/PycharmProjects/git_action$ git diff HEAD -- code.txt 
diff --git a/code.txt b/code.txt
index 2e58d97..17c0853 100644
--- a/code.txt
+++ b/code.txt
@@ -5,3 +5,5 @@ This is the second line.
 This is the third line....
 
 This is the fourth line...
+
+neeeeeew line...................

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IptPtDnd-1590760213651)(./img/WechatIMG98.png)]

  • 对比工作区 和 版本中文件的不同

4.6 删除文件

  • 无论是创建文件、编辑文件 或者是 删除文件,都是对工作区的改动。

    • 比如删除文件 code2.txt,如果想撤销,可以用撤销工作区的改动命令 git checkout -- <file_name>

    • 如果就是要 删除文件 code2.txt, 执行 rm code2.txt 物理删除,然后执行git add code2.txt 或者 git rm code2.txt将修改保存到暂存区,再git commit -m "版本注释" 创建新的版本。

  • 命令 git rm <file_name> 用于删除一个文件。 如果一个文件已经被提交到版本库,那么永远不用担心误删。但是 仍要小心,因为只能恢复文件到最新版本,这样会丢失删除文件之后所有的版本修改。

  • 如果文件没有被提交到版本库,误删就找不回来了,除非用一些扫描磁盘的工具,但也不一定找得回。

Git 分支管理

1. 概念

  • 分支在实际中有什么用呢?

    • 如果只有一个master 分支,假设我们准备开发一个新功能,但是需要两周才能完成,第一周写了50%的代码,若立刻提交,由于代码未写完,不完整的代码库会导致别人无法干活。

    • 如果等代码全部写完再一次提交,又存在丢失每天工作进度的巨大风险。

    • 现在有了分支,就不用怕了。 可以创建一个属于自己的分支,别人看不到,还继续再原来的分支上正常工作,而我们再自己的分支上干活,想提交到自己的分支就提交,直到开发完毕后,再一次性合并到原来的分支上,这样既安全,又不影响别人工作。

2. 创建与合并分支

  • 默认只有一个 master 分支,HEAD 指向 分支,分支指向分支上最新的一次版本。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gUC4CE1Q-1590760377838)(./img/WechatIMG100.png)]


  • 案例

      1. git branch 查看当前所有的分支,以及(HEAD)当前在哪个分支下工作。
      1. 执行 git checkout -b dev,创建一个dev 分支,并切换到其上进行工作。 这两步完成后的 工作树如下

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-etwyw3LH-1590760377839)(./img/WechatIMG102.jpeg)]

      1. 在code.txt 新增一行,然后 在 dev 分支下 git addgit commit 创建版本. 该步完成后 工作树如下
    • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pVXFR63z-1590760377842)(./img/WechatIMG103.png)]

      1. 执行 git checkout master, 切换回 master 分支
      1. master分支下 执行 git merge dev 合并 dev 分支。此时是Fast-forward (快速合并) 方式的合并。
      1. 删除dev 分支,执行 git branch -d dev
[fuli@23:41:35]~/PycharmProjects/git_action$ git branch
* master
[fuli@18:12:12]~/PycharmProjects/git_action$ git checkout -b dev
Switched to a new branch 'dev'
[fuli@18:12:21]~/PycharmProjects/git_action$ git branch
* dev
  master
[fuli@18:12:30]~/PycharmProjects/git_action$ git log --pretty=oneline
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (HEAD -> dev, master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:44:48]~/PycharmProjects/git_action$ ls
code.txt  code2.txt
[fuli@18:47:08]~/PycharmProjects/git_action$ vim code.txt 
[fuli@18:48:00]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...

[fuli@18:48:04]~/PycharmProjects/git_action$ git add code.txt 
[fuli@18:48:13]~/PycharmProjects/git_action$ git commit -m "dev分支提交"
[dev 2e2afc6] dev分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+), 3 deletions(-)
[fuli@18:48:29]~/PycharmProjects/git_action$ git log
commit 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> dev)
Author: Fuli <[email protected]>
Date:   Sun Apr 19 18:48:29 2020 +0800

    dev分支提交

commit 7e9087bcb1a8c8a0acdb4e1a418239160db0584b (master)
Author: Fuli <[email protected]>
Date:   Fri Apr 17 22:55:41 2020 +0800

    版本4

commit aa4e0e8732d92d5b46c6a0db37e05442ed0d84da
Author: Fuli <[email protected]>
Date:   Fri Apr 17 20:32:17 2020 +0800

    版本3

commit bbf036c780245dffa56052047d6b0d1bbae939d0
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:58:35 2020 +0800

    版本0.02

commit ba26141703c2a4f78d055a47ffd1dc6df39de0e9
Author: Fuli <[email protected]>
Date:   Fri Apr 17 16:48:22 2020 +0800

    版本0.01
[fuli@18:48:37]~/PycharmProjects/git_action$ git log --pretty=oneline
2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> dev) dev分支提交
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:50:19]~/PycharmProjects/git_action$ git checkout master
Switched to branch 'master'
[fuli@18:53:46]~/PycharmProjects/git_action$ git log --pretty=oneline
7e9087bcb1a8c8a0acdb4e1a418239160db0584b (HEAD -> master) 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@18:53:56]~/PycharmProjects/git_action$ git branch
  dev
* master
[fuli@18:54:08]~/PycharmProjects/git_action$ git merge dev
Updating 7e9087b..2e2afc6
Fast-forward
 code.txt | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
[fuli@18:56:30]~/PycharmProjects/git_action$ git log --pretty=oneline
2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (HEAD -> master, dev) dev分支提交
7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01

[fuli@18:58:49]~/PycharmProjects/git_action$ git branch -d dev
Deleted branch dev (was 2e2afc6).
[fuli@18:59:32]~/PycharmProjects/git_action$ git branch
* master
小结
查看分支 git branch
创建分支 git branch <name>
切换分支 git checkout <name>
创建+切换分支 git checkout -b <name>
合并某分支到当前分支 git merge <name>
删除分支 git branch -d <name>

3. 解决冲突

  • masterdev2(或者任意两个欲合并的分支)起冲突,需要手动修改 冲突对应的文件, 并进行一次新的 git addgit commit 创建新的版本,即可使两个分支合并,并解决冲突。

  • git status 也可以提示 冲突对应的文件

  • 解决 Conflict之后就可以删除 dev2分支了

[fuli@19:20:03]~/PycharmProjects/git_action$ git branch
* master
[fuli@19:20:15]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...

[fuli@19:20:34]~/PycharmProjects/git_action$ git branch dev2
[fuli@19:20:50]~/PycharmProjects/git_action$ git checkout dev2
Switched to branch 'dev2'
[fuli@19:20:59]~/PycharmProjects/git_action$ git branch
* dev2
  master
[fuli@19:21:07]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:22:17]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
this is dev2 branch add: dev2.....
[fuli@19:22:58]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:23:05]~/PycharmProjects/git_action$ git commit -m "dev2分支提交"
[dev2 3bc40fe] dev2分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[fuli@19:23:19]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
* 3bc40fe5ffd2f542bab9f6988d58570bee271454 (HEAD -> dev2) dev2分支提交
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 (master) dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:23:34]~/PycharmProjects/git_action$ git checkout master
Switched to branch 'master'
[fuli@19:23:50]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:25:21]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
master branch add: master......
[fuli@19:25:25]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:25:33]~/PycharmProjects/git_action$ git commit -m "master分支提交"
[master a5027ad] master分支提交
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 1 deletion(-)
[fuli@19:25:47]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
* a5027ad7feb05c133c237f0098d1645a8d2a27a9 (HEAD -> master) master分支提交
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:25:53]~/PycharmProjects/git_action$ git merge dev2
Auto-merging code.txt
CONFLICT (content): Merge conflict in code.txt
Automatic merge failed; fix conflicts and then commit the result.
[fuli@19:26:05]~/PycharmProjects/git_action$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   code.txt

no changes added to commit (use "git add" and/or "git commit -a")
[fuli@19:26:12]~/PycharmProjects/git_action$ vim code.txt 
[fuli@19:26:47]~/PycharmProjects/git_action$ cat code.txt 
This is the first line. Time: 2020.04.17
This is the second line.
This is the third line....
This is the fourth line...
- dev branch add: new line...
master branch add: master......
this is dev2 branch add: dev2.....
[fuli@19:26:54]~/PycharmProjects/git_action$ git add code.txt 
[fuli@19:27:03]~/PycharmProjects/git_action$ git commit -m "解决冲突Conflict"
[master 455c320] 解决冲突Conflict
 Committer: Fuli <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

[fuli@19:27:30]~/PycharmProjects/git_action$ git log --pretty=oneline --graph
*   455c32056f8f3b7aa87ff087036adb4578aa1f10 (HEAD -> master) 解决冲突Conflict
|\  
| * 3bc40fe5ffd2f542bab9f6988d58570bee271454 (dev2) dev2分支提交
* | a5027ad7feb05c133c237f0098d1645a8d2a27a9 master分支提交
|/  
* 2e2afc61ac3edef1e18ecf24375e82aaf84a25a0 dev分支提交
* 7e9087bcb1a8c8a0acdb4e1a418239160db0584b 版本4
* aa4e0e8732d92d5b46c6a0db37e05442ed0d84da 版本3
* bbf036c780245dffa56052047d6b0d1bbae939d0 版本0.02
* ba26141703c2a4f78d055a47ffd1dc6df39de0e9 版本0.01
[fuli@19:27:40]~/PycharmProjects/git_action$ git branch -d dev2
Deleted branch dev2 (was 3bc40fe).

4. 分支管理策略

通常合并分支时,如果可能,git 会采用 Fast-forward 模式。
但是有时候 虽然没有冲突,但也不可以使用 快速合并,这时git(系统)会在合并之后做一次新的版本提交。
在这种模式下,删除分支会丢掉分支信息。

重点

强制禁用 Fast-forward 模式,gitmerge时会生成一个新的 commit,这样,从分支历史上就可以看出分支信息。
git merge --no-ff -m "禁用Fast-forward合并" dev . 因为本次合并要创建一个新的commit,所以加上-m 参数,把commit 描述写进去。

git stash 保存工作现场

git stash list 列出工作现场

git stash pop 恢复工作现场

Github 的使用


GitHub使用

1. 创建仓库 Repository

  • .gitignore 标记git不会跟踪的文件类型

2. 添加 ssh 账户

  • 若要使某台机器与GitHub上的Repository 交互,需要将某个pc端的 ssh公钥 添加到对应的Github账户上

第一步:创建SSH

打开终端(terminal)检测是否存在ssh:

$cd ~/.ssh

注:若提 -bash: cd: ~/.ssh: No such file or directory 那就说明.ssh文件夹不存在。

  1. 如果已存在,先将已有的ssh备份,或者将新建的ssh生成到另外的目录下。
  2. 如果不存在,通过默认的参数直接生成ssh。输入以下命令来创建ssh:
$ ssh-keygen -t rsa -C [email protected]

注:[email protected] 为你注册GitHub时的邮箱账号。
命令执行成功:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fuli/.ssh/id_rsa):    // .ssh默认路径,不输入则不修改 
Enter passphrase (empty for no passphrase):       // 密码长度至少为4  
Enter same passphrase again
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mS+RjTY9yt5uRz/spUP/lBIE0Go9Xv/o+ODEKbS31W4 xxxxx@xx.com
The key's randomart image is:
+---[RSA 2048]----+
|         .o.     |
|           ..    |
|          o  .   |
|         X o..   |
|        S * o..  |
|       o * =..oo.|
|        + +.*=.+=|
|       . o.=.=B=E|
|        .oo.++=++|
+----[SHA256]-----+

SSH创建成功。

第二步:登陆GitHub 配置 SSH keys

登陆GitHub,选择Settings-->SSH Keys

Title:[email protected]

Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此。

注:

  1. 查找id_rsa.pub文件,打开Finder,shift+command+g 输入设置的.ssh的路径(默认路径:~/.ssh)。

  2. id_rsa.pub文件可用文本编辑打开。

最后:打开终端

$ ssh -T [email protected]

测试一下你的帐号跟github连上没有, 如果出现如下提示,表示你连已经连上了.

Hi your GitHub's username! You've successfully authenticated, but GitHub does not provide shell access.

接下来就可以上传你的代码了。

3. 克隆项目

  • git clone <one github repo address>

4. 推送分支

  • 克隆到本地的项目一般会建一个分支进行开发,若开发完成,需要将分支推送到GitHub,执行git push origin fuli_dev,其中 origin 代表远程的分支(这里即GitHub),fuli_dev 是本地分支。

  • 若是第一次推送,执行上述命令之后,git会在 GitHub对应的Repository 中新建一个 fuli_dev 分支。

5. 跟踪远程

  • 本地的分支跟踪远程的分支, git branch --set-upstream-to=origin/远程分支名称 本地分支名称

    • 例如:git branch --set-upstream-to=origin/fuli_dev fuli_dev

    • 如果本地的提交版本 和 远程的不一致的话,git status会有相应提示。

6. 拉取代码

  • git pull origin <远程分支名dev_mac> 将远程的分支 dev_mac 上的代码下载并 合并 到本地所在的分支。

    • 注意 这里是 下载 并 合并

小结

项目一般包括两个主要分支:MasterDev

Master : 保存发布的项目版本代码。 V1.0, V2.0 …

Dev : 保存开发过程中的代码,每一个组员开发完自己的代码之后,都需要将代码发布到远程的Dev分支上。


关于.gitignore更新后不生效的方法

由于是后期对.gitignore 添加的内容的缘故

昨天能用pycharm写代码,发现venv文件夹更新了,
但是不想commit这些环境配置文件,就在.gitignore里面加了venv/但是发现并不起作用,
于是就google了一下,发现得在terminal中输入已下几行代码

例如,后期添加了 venv/,若要使添加内容生效,需要先 清除venv/git缓存。
git rm -r --cached venv
git add .
git commit -m 'update .gitignore'

搞定,再commit的时候.gitignore文件已经生效。

Reference
Reference2

Mac系统GitHub上传项目

  • 说到GitHub相信大家都不陌生,这里就不再赘述了。作为开源代码库以及版本控制系统,使用好了会非常受益。
  • 经常维护自己的技术博客和GitHub,在你找工作时也是加分项哟。

一、准备工作:

  1. 注册GitHub账号。https://github.com

  2. 安装Git客户端。https://git-scm.com/downloads

二、准备工作做好以后,打开终端(terminal)可以查看Git版本

$ git version
git version 2.14.1

第一步:创建SSH

打开终端(terminal)检测是否存在ssh:

$cd ~/.ssh

注:若提 -bash: cd: ~/.ssh: No such file or directory 那就说明.ssh文件夹不存在。

  1. 如果已存在,先将已有的ssh备份,或者将新建的ssh生成到另外的目录下。
  2. 如果不存在,通过默认的参数直接生成ssh。输入以下命令来创建ssh:
$ ssh-keygen -t rsa -C [email protected]

注:[email protected] 为你注册GitHub时的邮箱账号。
命令执行成功:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/clyde/.ssh/id_rsa):    // .ssh默认路径,不输入则不修改 
Enter passphrase (empty for no passphrase):       // 密码长度至少为4  
Enter same passphrase again
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mS+RjTY9yt5uRz/spUP/lBIE0Go9Xv/o+ODEKbS31W4 xxxxx@xx.com
The key's randomart image is:
+---[RSA 2048]----+
|         .o.     |
|           ..    |
|          o  .   |
|         X o..   |
|        S * o..  |
|       o * =..oo.|
|        + +.*=.+=|
|       . o.=.=B=E|
|        .oo.++=++|
+----[SHA256]-----+

SSH创建成功。

第二步:登陆GitHub 配置 SSH keys

登陆GitHub,选择Settings-->SSH Keys

Title:[email protected]

Key:打开你生成的id_rsa.pub文件,将其中内容拷贝至此。

注:

  1. 查找id_rsa.pub文件,打开Finder,shift+command+g 输入设置的.ssh的路径(默认路径:~/.ssh)。

  2. id_rsa.pub文件可用文本编辑打开。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hT6AYTBv-1590760879616)(attachment:image.png)]

最后:打开终端

$ ssh -T [email protected]

测试一下你的帐号跟github连上没有, 如果出现如下提示,表示你连已经连上了.

Hi your GitHub's username! You've successfully authenticated, but GitHub does not provide shell access.

接下来就可以上传你的代码了。

第三步:在GitHub新建自己的Repository,并关联到本地

一、登录GitHub账号,新建Repository

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WcvuNxRl-1590760879619)(attachment:image.png)]

  • 点击Create repository 进入下面界面
    在这里插入图片描述

二、关联本地文件

打开终端(terminal),cd到所传文件夹中,然后执行

git init   //初始化本地仓库
git remote add origin [email protected]:xxxxxx/CCTestDemo.git  //连接远程仓库并建了一个名叫:origin的别名,当然可以为其他名字,
git pull origin master  //先从远程pull一次文件  以免提交报错
git add -A   //文件  (git add -A 为添加该文件夹所有文件)
git commit -m "你的注释"  //提交到本地仓库,并写一些注释
git push origin master   //将本地仓库合并到别名为origin地址的master分支

如果中间不出什么问题就大功告成了!!! 刷新GitHub页面就可以看到了。
注:

  1. 如果git remote add origin这一步出现origin exitss, 输入git remote rm origin,再次执行上面那条语句。

  2. 如果出现 failed to push some refs to git错误,可以通过如下命令进行代码合并 git pull --rebase origin master

  3. 切记上传文件时,一定要先commit到本地仓库,才能进行push提交,否则会显示Everything up-to-date(意思就是目前的远程仓库的内容跟本地仓库对比后,没有做修改,是最新的。

  4. 要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git 关联后,使用命令git push -u origin master 第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改。

Reference

Git 拉去Github某个分支

方法一:直接获取

  • 首先新建个文件夹,右键打开Git Bash

在这里插入图片描述

  • Git Bash中直接输入指令:git clone -b dev 代码仓库地址dev是分支名称)
    在这里插入图片描述
  • 拉取完毕

方法二:先关联远程仓库

  • 打开Git Bash
  • Git Bash 中输入 git init进行初始化
  • 与远程代码仓库建立连接:git remote add origin 代码仓库地址 –关联远程仓库

建立连接

  • 将远程分支拉到本地:git fetch origin devdev即分支名)
    拉取分支

  • 创建本地分支:git checkout -b LocalDev origin/dev (LocalDev 为本地分支名,dev为远程分支名)

创建分支

  • 根据分支的变化,感觉这条指令可能是创建并切换到该分支
  • 最后一步将远程分支拉取到本地:git pull origin devdev为远程分支名)
    拉取成功

方法三

  • 打开Git Bash

  • 输入 git clone 代码仓库地址

  • 进入文件夹中 命令:cd XXX(文件夹名)

  • 继续输入指令 git submodule init

  • 最后 git submodule update

  • 至此结束

Reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章