GIT版本控制工具使用

基本命令

  • git init,初始化,表示即将对当前文件夹进行版本控制。
  • git status,查看Git当前状态,如:那些文件被修改过、那些文件还未提交到版本库等。
  • git add 文件名,将指定文件添加到版本库的暂存状态。
  • git commit -m '提交信息',将暂存区的文件提交到版本库的分支。
  • git log,查看提交记录,即:历史版本记录
  • git config --local user.name 'xxx' 设定用户名和邮箱    
  • git config --local user.email '[email protected]'

回滚

 1 [root@vultr test]# git log                               #显示版本日志
 2 commit 8ea124605072a2e464b9844fb8d689a0d8a60c6f           
 3 Author: root <[email protected]>
 4 Date:   Sun Jul 15 14:08:02 2018 +0000
 5 
 6     有一个版本
 7 
 8 commit ffd29f7565dd7f783aac711cd55b40be1b96fd65
 9 Author: root <[email protected]>
10 Date:   Sun Jul 15 14:04:05 2018 +0000
11 
12     第一次提交
13 [root@vultr test]# git log
14 commit 8ea124605072a2e464b9844fb8d689a0d8a60c6f
15 Author: root <[email protected]>
16 Date:   Sun Jul 15 14:08:02 2018 +0000
17 
18     有一个版本
19 
20 commit ffd29f7565dd7f783aac711cd55b40be1b96fd65
21 Author: root <[email protected]>
22 Date:   Sun Jul 15 14:04:05 2018 +0000
23 
24     第一次提交
25 [root@vultr test]# ^C
26 [root@vultr test]# git reset --hard ffd29f7565dd7f783aac711cd55b40be1b96fd65      #回滚到目标版本id
27 HEAD is now at ffd29f7 第一次提交
28 [root@vultr test]# git reflog                                #回滚回去的方法
29 ffd29f7 HEAD@{0}: ffd29f7565dd7f783aac711cd55b40be1b96fd65: updating HEAD
30 8ea1246 HEAD@{1}: commit: 有一个版本
31 [root@vultr test]# git reset --hard 8ea1246
32 HEAD is now at 8ea1246 有一个版本

分支控制版本

方案一:stash

stash用于将工作区发生变化的所有文件获取临时存储在“某个地方”,将工作区还原当前版本未操作前的状态;stash还可以将临时存储在“某个地方”的文件再次拿回到工作区。

[root@vultr test]# touch view.py                   #新建一个文件
[root@vultr test]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    view.py
nothing added to commit but untracked files present (use "git add" to track)
[root@vultr test]# git add .                           #git检测到变化
[root@vultr test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   view.py
#
[root@vultr test]# git stash                        #保存到暂存区
Saved working directory and index state WIP on master: 8ea1246 有一个版本
HEAD is now at 8ea1246 有一个版本
[root@vultr test]# git status
# On branch master
nothing to commit (working directory clean)
[root@vultr test]# vim a.py                        #修改一个bug
YouCompleteMe unavailable: requires Vim 7.4.1578+.
Press ENTER or type command to continue
[root@vultr test]# git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   a.py
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@vultr test]# git add . 
[root@vultr test]# git commit -m '修复bug'
[master aa0bfdb] 修复bug
 Committer: root <[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:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <[email protected]>'

 1 files changed, 2 insertions(+), 0 deletions(-)
[root@vultr test]# git stash pop         #重新合并到一起继续编辑
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   view.py
#
Dropped refs/stash@{0} (011817eab1ddd2da8d4abe8393c0f9862567c2f1)

特别的:执行 git stash pop 命令时,可能会遇到冲突,因为在紧急修复bug的代码和通过stash存储在“某个地方”的代码会有重合部分,所以执行 git stash pop 时候就会出现冲突,有冲突解决冲突即可。

 1 a. 原来内容:
 2     from django.shortcuts import render,HttpResponse
 3 
 4     def index(request):
 5         return render(request,'index.html')
 6 
 7     def africa(request):
 8         return HttpResponse('非洲专区')
 9 
10     
11 b. 开发到一半直播功能:
12     from django.shortcuts import render,HttpResponse
13 
14     def index(request):
15         return render(request,'index.html')
16 
17     def africa(request):
18         return HttpResponse('非洲专区')
19 
20 
21     def live(request):
22         print('开发到一半')
23         return HttpResponse('....')
24 
25 
26 c. 执行git stash,回到当前版本未修改状态:
27     from django.shortcuts import render,HttpResponse
28 
29     def index(request):
30         return render(request,'index.html')
31 
32     def africa(request):
33         return HttpResponse('非洲专区')
34 
35 d. 修复Bug并提交:
36     from django.shortcuts import render,HttpResponse
37 
38     def index(request):
39         return render(request,'index.html')
40 
41     def africa(request):
42         return HttpResponse('非洲xxxxx专区')
43 
44 
45 e. 继续开发直播功能 git stash pop,此时会出现冲突:
46     MacBook-Pro-4:pondo wupeiqi$ git stash pop
47     Auto-merging app01/views.py
48     CONFLICT (content): Merge conflict in app01/views.py
49 
50     表示app01/views.py存在冲突需要解决,此时文件内容为:
51 
52     from django.shortcuts import render,HttpResponse
53 
54         def index(request):
55             return render(request,'index.html')
56 
57         def africa(request):
58         <<<<<<< Updated upstream:               # 修复Bug时更改的内容
59             return HttpResponse('非洲xxxx区')  
60         =======                                  # 修复Bug前正在开发新功能时的内容
61             return HttpResponse('非洲专区')
62 
63         def live(request):
64             print('刚开发到一半')
65             return HttpResponse('直播功能')
66         >>>>>>> Stashed changes
67 
68 
69     需要自行解决冲突,然后继续开发,如:
70 
71     from django.shortcuts import render,HttpResponse
72 
73         def index(request):
74             return render(request,'index.html')
75 
76         def africa(request):
77         
78             return HttpResponse('非洲xxxx区')  
79         
80         def live(request):
81             print('刚开发到一半')
82             return HttpResponse('直播功能')
83         
84 
85 git stash pop 出现冲突

stash相关常用命令:

  • git stash             将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
  • git stash list        查看“某个地方”存储的所有记录
  • git stash clear     清空“某个地方”
  • git stash pop       将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
  • git stash apply     编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突) 
  • git stash drop      编号,删除指定编号的记录

方案二:branch

分支学习:branch称为分支,默认仅有一个名为master的分支。一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支。

  一般流程示例(上图)

学习参考上图,小P也可以按照着这样的流程进行开发,如果遇到上文开发到一般需要临时修复Bug的情况,可以按照下图的流程进行:

 1 MacBook-Pro-4:pondo wupeiqi$ git branch                     # 当前在master分支
 2 * master
 3  
 4  
 5 MacBook-Pro-4:pondo wupeiqi$ git branch dev                 # 创建dev分支用于开发新功能
 6  
 7 MacBook-Pro-4:pondo wupeiqi$ git checkout dev               # 切换到dev分支
 8 Switched to branch 'dev'
 9  
10 MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 开发新功能到一半,需要紧急修复Bug
11  
12 MacBook-Pro-4:pondo wupeiqi$ git add .
13  
14 MacBook-Pro-4:pondo wupeiqi$ git commit -m '新功能开发一半'
15 [dev b3ac2cb] 新功能开发一半
16  1 file changed, 2 insertions(+)
17  
18  
19  
20  
21 MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换回master分支
22 Switched to branch 'master'
23  
24 MacBook-Pro-4:pondo wupeiqi$ git branch bug                 # 创建bug分支
25  
26 MacBook-Pro-4:pondo wupeiqi$ git checkout bug               # 切换到bug分支
27 Switched to branch 'bug'
28  
29 MacBook-Pro-4:pondo wupeiqi$ vim pondo/settings.py          # 修改bug
30  
31 MacBook-Pro-4:pondo wupeiqi$ git add .                      # 提交bug
32  
33 MacBook-Pro-4:pondo wupeiqi$ git commit -m '紧急修复bug'      # 提交bug
34 [bug f42f386] 紧急修复bug
35  1 file changed, 1 insertion(+), 1 deletion(-)
36  
37  
38 MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换会master
39 Switched to branch 'master'
40  
41 MacBook-Pro-4:pondo wupeiqi$ git merge bug                  # 将bug分支内容合并到master分支,表示bug修复完毕,可以上线
42 Updating 0972f4b..f42f386
43 Fast-forward
44  pondo/settings.py | 2 +-
45  1 file changed, 1 insertion(+), 1 deletion(-)
46  
47  
48  
49  
50 MacBook-Pro-4:pondo wupeiqi$ git checkout dev               # 切换到dev分支,继续开发新功能
51 Switched to branch 'dev'
52  
53 MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 继续开发其他一半功能
54  
55 MacBook-Pro-4:pondo wupeiqi$ git add .                      # 提交新功能
56  
57 MacBook-Pro-4:pondo wupeiqi$ git commit -m '继续开发完成'      # 提交功能
58 [dev c0bfb27] 继续开发完成
59  1 file changed, 1 insertion(+)
60  
61 MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换回master分支
62 Switched to branch 'master'
63  
64 MacBook-Pro-4:pondo wupeiqi$ git merge dev                  # 将dev分支合并到master分支
65 Merge made by the 'recursive' strategy.
66  app01/views.py | 3 +++
67  1 file changed, 3 insertions(+)

注意:git merge 时也可能会出现冲突,解决冲突的方式上述stash相同,即:找到冲突文件,手动修改冲突并提交,此处不再敖述。

branch相关常用命令:

  • git branch 分支名称             创建分支
  • git checkout 分支名称          切换分支
  • git branch -m 分支名称        创建并切换到指定分支
  • git branch                          查看所有分支
  • git branch -d 分支名称         删除分支
  • git merge 分支名称              将指定分支合并到当前分支

github代码管理

在家里,开发完毕部分功能将代码推送到GitHub。

 1 MacBook-Pro-4:pondo wupeiqi$ git remote add origin https://github.com/WuPeiqi/pondo.git   # 为地址起一个别名origin
 2 MacBook-Pro-4:pondo wupeiqi$ git push origin master              # 将本地master分支内容以及版本信息推送到GitHub
 3 Username for 'https://github.com':                               # 输入GitHub用户名
 4 Password for 'https://[email protected]':                       # 输入GitHub密码
 5 Counting objects: 2, done.
 6 Delta compression using up to 4 threads.
 7 Compressing objects: 100% (2/2), done.
 8 Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done.
 9 Total 2 (delta 1), reused 0 (delta 0)
10 remote: Resolving deltas: 100% (1/1), completed with 1 local object.
11 To https://github.com/WuPeiqi/pondo.git
12    634aac4..274f1e4  master -> master
13 MacBook-Pro-4:pondo wupeiqi$ git push origin dev              # 将本地dev分支内容以及版本信息推送到GitHub
14 Counting objects: 3, done.
15 Delta compression using up to 4 threads.
16 Compressing objects: 100% (2/2), done.
17 Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
18 Total 3 (delta 1), reused 0 (delta 0)
19 remote: Resolving deltas: 100% (1/1), completed with 1 local object.
20 To https://github.com/WuPeiqi/pondo.git
21    274f1e4..50e2169  dev -> dev

在公司,新电脑第一次使用,需要将代码从GitHub中获取并继续开发

 1 MacBook-Pro-4:github wupeiqi$ git clone https://github.com/WuPeiqi/pondo.git    # 将项目从GitHub中获取
 2 Cloning into 'pondo'...
 3 remote: Counting objects: 31, done.
 4 remote: Compressing objects: 100% (26/26), done.
 5 remote: Total 31 (delta 2), reused 30 (delta 1), pack-reused 0
 6 Unpacking objects: 100% (31/31), done.
 7 MacBook-Pro-4:github wupeiqi$ cd pondo/
 8 MacBook-Pro-4:pondo wupeiqi$ git Branch                                          # 默认获取到得只有master分支
 9 * master
10 MacBook-Pro-4:pondo wupeiqi$ git branch dev origin/dev                           # 创建dev分支且和远程dev分支同步
11 Branch dev set up to track remote branch dev from origin.
12 MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                    # 切换到dev分支
13 Switched to branch 'dev'
14  
15 MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py                                  # 继续开发新功能
16  
17 MacBook-Pro-4:pondo wupeiqi$ git add .                                           # 添加文件到版本库的暂存状态
18 MacBook-Pro-4:pondo wupeiqi$ git commit -m '公司开发功能1'                         # 提交新功能到版本库的分支
19 [dev 9281447] 公司开发功能1
20  1 file changed, 1 insertion(+), 1 deletion(-)
21 MacBook-Pro-4:pondo wupeiqi$ git push origin dev                                 # 提交dev分支内容到远程GitHub托管仓库的dev分支
22 Username for 'https://github.com': wupeiqi
23 Password for 'https://[email protected]':
24 Counting objects: 4, done.
25 Delta compression using up to 4 threads.
26 Compressing objects: 100% (4/4), done.
27 Writing objects: 100% (4/4), 427 bytes | 0 bytes/s, done.
28 Total 4 (delta 2), reused 0 (delta 0)
29 remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
30 To https://github.com/WuPeiqi/pondo.git
31    50e2169..9281447  dev -> dev

在家里,由于白天在公司已经开发一部分功能并提交到GitHub,家里电脑的代码还是昨晚的版本,所以需要从GitHub拉去最新代码,然后继续开发

 在公司,由于昨天晚上在家已经开发了一部分功能,在公司需要先把昨晚开发的功能从GitHub中拉取,并继续开发。

MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                   # 切换到dev分支
MacBook-Pro-4:pondo wupeiqi$ git fetch origin dev                               # 从GitHub仓库获取dev分支最新内容到版本库的分支
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/WuPeiqi/pondo
 * branch            dev        -> FETCH_HEAD
   150d891..65b6604  dev        -> origin/dev
MacBook-Pro-4:pondo wupeiqi$ git merge origin/dev                               # 将版本库的分支内容合并到工作区
Updating 150d891..65b6604
Fast-forward
 readme | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py                                 # 继续开发新功能
MacBook-Pro-4:pondo wupeiqi$ git add .                                          # 添加文件到版本库的暂存状态
MacBook-Pro-4:pondo wupeiqi$ git commit -m 'xxxxxxxxxxx'                        # 提交新功能到版本库的分支
View Code

长此以往,将Git和GitHub结合使用做到避免电脑损坏造成数据丢失以及多地开发的问题,上文执行过程中执行 【git pull origin 分支】命令等同于【git fetch origin 分支】+ 【git merge origin/分支】,并且在执行过程中可能会出现冲突,原因是由于本地代码和获取的最新代码有重合部分,那么就需要自己手动解决冲突然后再继续开发。

其他补充

1. 配置文件

Git的配置文件有三个:

  • 系统配置: /private/etc/gitconfig
  • 用户配置: ~/.gitconfig
  • 项目配置:.git/config

2. 用户凭证

由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两种解决方法:

    • 秘钥
      首先创建一对秘钥  ssh-keygen -t rsa,然后将 id_rsa.pub (公钥)内容拷贝到github中,日后操作无需再输入用户名和密码。
      注意:这种方式需要使用GIt中 [email protected]:WuPeiqi/xxxxx.git 格式地址。
    • 密码
      Https访问git时,避免每次操作需要输入用户名和密码,可以在配置文件中添加如下配置项:
          [credential]
          helper = store/cache/第三方

      store:
              表示将用户名和密码保存在硬盘上
              第一次输入过用户名和密码之后,用户名和密码就会保存在当前用户根目录的 .git-credentials 文件中,内容格式为:https://用户名:密码@github.com

              自动添加配置命令:git config credential.helper store
      cache: 
              表示将用户名和密码保存在缓存中
              第一次输入过用户名和密码之后,用户名和密码就会保存在缓存中,默认超时时间是 900 秒,缓存相关文件保存在当前用户根目录的 git-credential-cache 中 
              自动添加配置命令:
                     git config credential.helper cache
                     git config credential.helper 'cache --timeout=300'

      相关操作:
              清除缓存:git credential-cache exit 
              指定超时:
                  [credential]
                  helper = cache --timeout=300
      注意:
               这种方式需要使用GIt中 https://github.com/WuPeiqi/xxxx.git 格式地址。
               指定用户名和密码: https://用户名:密码@github.com/wupeiqi/xxx.git 

    • 参考博客https://www.cnblogs.com/wupeiqi/p/7295372.html#3872597

猜你喜欢

转载自www.cnblogs.com/blue-day/p/9315558.html