git的基本操作和技巧使用

GIT 的常规操作

常规操作也是我自己平时常用的几个命令, 学自于 pro git 这本书中

git 配置

必须配置:

//xxxx 是登录git对外访问的用户名
git config --global user.name "xxxx"
//用户的邮箱
git config --global  user.email "邮箱地址"

作用:

​ 跟SSH和hhtp的登录没有关系。作用就是为了提交到服务器的时候,服务器可以记录到是谁提交的。

git 配置等级:

​ 当本地有多个项目时,可以根据git的配置级别,设置不同的用户和邮箱。git配置有三个级别:system(系统级)、global(用户级别)、local(版本级别)。这三个是逐层覆盖的。先去查找system、接着是global、最后是local。可以将常用的一个设置为system、将不常用的设置为local即可。只要这样设置和github中的设置一样就可以解决上述问题。

System配置

 git config --system user.name "name" git config --system user.email myuser@email.com

global配置

 git config --global user.name "name" git config --global user.email myuser@email.com

local配置

 git config --local user.name "name" git config --local user.email myuser@email.com

查看配置的方法

git config --list

git移除全局配置:

  1. 除全局配置账户
git config --global --unset user.name
  1. 查看全局用户名
git config --global user.name
  1. 移除全局配置邮箱
git config --global --unset user.email
  1. 查看全局邮箱
git config --global user.email
  1. 移除全局密码
git config --global --unset user.password
  1. 查看全局密码
git config --global user.password

git 基本使用

  1. clone现有仓库

    git clone URL  (URL支持git,ssh,http,https等各种协议)
    
  2. git中文件的各个状态

    • unstaged - git仓库中没有此文件的相关记录
    • modified - git仓库中有这个文件的记录,并且此文件当前有改动
    • staged - 追加,删除或修改的文件被暂时保存,这些追加,删除和修改并没有提交到git仓库
    • commited - 追加或修改的文件被提交到本地git仓库(git仓库中大部分都是这种文件,所以git status不显示这些文件)
  3. 查看git仓库中各文件状态

    git status
    
  4. 初始化一个仓库

    git init
    

    在当前文件夹下生成.git目录,完成初始化,此时此文件夹下的所有文件处于unstaged状态

  5. 追加文件

    git add test.c
    

    test.c的文件变为staged状态,其他文件还是unstaged状态

    5.1 追加文件的结果1 - 恢复为原先状态(变为unstaged)

    git rm --cache test.c
    

    5.2 追加文件的结果2 - 提交到git仓库(变为commited)

    git commit -m "my message"
    
  6. 修改文件

    echo "aaa"  >> test.c
    

    test.c的状态变为modified

    6.1 修改文件的结果1

    git add test.c  (暂时保存修改的内容,即变为staged)
    

    下面有2条路可以选择:
    6.1.1 取消刚才的暂时保存

    git reset test.c  (状态变回modified)
    

    6.2.2 将暂存的修改提交到git仓库

    git commit -m "my message"
    

    6.2 修改文件的结果2

    git checkout test.c  (将test.c恢复为git仓库中的最新版本,即变为commited状态,test.c的内容和5.2节一样)
    
  7. 删除文件
    7.1 从git仓库和磁盘上删除文件

    git rm test.c  (当前目录中删除了test.c,在git仓库中暂时删除了test.c,相当于staged状态)
    

    7.1.1 从git仓库中删除test.c

    git commit -m "my message"  (git仓库以后不再维护test.c)
    

    7.1.2 删错了,恢复刚才的操作

    git reset HEAD test.c  (恢复到删除前的状态,当前目录中已删除的test.c也恢复了,test.c仍文commited状态)
    

    7.2 仅从git仓库中删除文件

    git rm --cache test.c (当前目录中没有删除了test.c,仅在git仓库中暂时删除了test.c,相当于staged状态)
    

    7.2.1 从git仓库中删除test.c

    git commit -m "my message"  (git仓库以后不再维护test.c,但是当前目录中仍然有test.c)
    

    7.2.2 删错了,恢复刚才的操作

    git reset HEAD test.c  (和7.1.2一样)
    

    7.3 误删除后的恢复
    如果删除了一个文件,并且commit之后发现删错了。也可以恢复,

    git log  (查看各次的提交信息)
    git checkout commit号  (恢复到未删除前的commint号,此时删除的文件也恢复到磁盘上了)
    git checkout master  (备份好删除的文件后,再回到最新状态)
    

git 远程仓库

  1. 查看远程仓库
    1.1 简单查看-所有仓库

    git remote (只能查看远程仓库的名字)
    

    1.2 查看更多内容-所有仓库

    git remote -v (远程仓库的名字及git地址)
    

    1.3 查看单个仓库的信息

    git remote show [remote-name]
    
  2. 新建远程仓库

    git remote add [shortname] [url]
    ex. git remote add mc git://www.host.com/gitdir/mycode.git
    
  3. 修改远程仓库

    git remote rename [oldnanme] [newname]
    
  4. 删除远程仓库

    git remote rm [remote-name]
    
  5. 远程仓库的数据
    5.1 获取数据

    git fetch [remote-name] (获取仓库的所有更新,但是不自动合并当前分支)
    git pull (获取仓库的所有更新, 并且自动合并到当前分支)
    

    5.2 上传数据

    git push [remote-name] [branch-name]
    ex. git push origin master
    

git 标签

  1. 列出标签
    1.1 查看所有tag

    git tag
    

    1.2 查看某个tag

    git show [tag-name]
    
  2. 新建标签
    2.1 轻量级tag

    git tag [tag-name]
    

    2.2 带标注的tag

    git tag -a [tag-name] -m "tag message"
    

    2.3 后期追加tag

    git log --pretty=oneline (查看所有的commit号)
    git tag -a [tag-name] [commit号前几位即可]
    
  3. 删除标签

    git tag -d [tag-name]
    
  4. 提交标签到远程仓库

    git push [remote-name] --tags
    ex. git push origin --tags
    

git 分支

  1. 查看和切换分支

    git branch (查看所有的分支及当前处于哪个分支)
    git branch -v (查看所有的分支的详细信息)
    git branch --merged (查看已经合并的分支)
    git branch --no-merged (查看还没合并的分支)
    git checkout [branch-name] (切换到某个分支)
    
  2. 新建分支

    git branch [branch-name]  (新建一个分支)
    git branch -b [branch-name] (新建一个分支并切换到这个分支上)
    
  3. 合并分支

    git merge [branch-name]
    ex. 将分支btest合并到主分支master
    git checkout master
    git merge btest
    

    merge时有冲突的文件会列出来,需要手动合并

    将冲突手动解决后,再次用 git status来查看是否还有 unmerged的文件。
    如果没有冲突的文件,就可以 git commit 来提交这次合并了。

  4. 删除分支

    git branch -d [branch-name]
    或者 git branch -D [branch-name] (强制删除某个还未合并的分支)
    
  5. 远程分支相关
    5.1 新建远程分支

    1. git checkout [local_branch] (首先进入想要上传的分支)
    2. git remote add [remote_repo] [remote_branch]
      (这里的[remote_branch]是远程分支的名字,一般和[local_branch]同名,
      [remote_repo]是远程仓库的名字)

    5.2 向远程分支推送数据

    git push [remote_repo] [remote_branch]
    

    5.3 删除远程分支

    git push [remote_repo] :[remote_branch] (注意远程分支前有个":")
    
  6. 合并分支的另一个方法:衍和

衍和可以简化master上的提交记录,使得代码可以方便的回退,
但是在公共仓库上用衍和有一定的风险。
衍和我基本用不上,这里就不赘述了。

服务器创建 git 仓库, 并将其作为远程仓库

其实 git 是分布式的 SCM. 并不存在谁是服务器, 谁是客户端的问题, 这里所说的服务器上的git仓库, 指的是多人合作开发时, 共用的, 作为最终发布版本的 git 仓库.
这个 git 仓库就相当于你在 github 上建的仓库, 会将你在各个电脑上做的代码等提交到上面进行统一管理.

  1. 服务端 (远程 git 仓库)

    • 生成用于git服务的账户 (一般就用git)

      groupadd gpxxx
      useradd -m -g gpxxx gitxxx
      
    • 初始化服务端的git 仓库

      cd ~/
      mkdir git-repo
      cd git-repo
      mkdir test.git
      cd test.git
      git --bare init
      
  2. 客户端 (本地 git 仓库)

    • 新建本地git 仓库

      cd ~/gitlocal
      mkdir test
      cd test
      git init
      
    • 初始化本地仓库

      touch README
      git add README
      git commit -m 'first commit for init'
      
    • 设置git用户信息

      git config --global user.name "wangyubin"
      git config --global user.email "xxx@xxx.com"
      
    • 关联远程仓库

      git remote add origin gituser@<server address>:~/test.git/
      
    • 将本地仓库提交到远程

      git push origin master
      

git 使用中遇到的一些问题

git pull 时, 远程文件与本地文件有冲突

如果远程的仓库被其他人更新了, 并且更新的内容与我自己本地编辑的内容有冲突. 这时执行 git pull 可能有如下message:

Auto-merging path/to/conflict-file
CONFLICT (content): Merge conflict in path/to/conflict-file
Automatic merge failed; fix conflicts and then commit the result.

用文本编辑器 vim 或者 emacs 之类的来编辑冲突的文件 path/to/conflict-file, 冲突的地方有类似如下的显示

<<<<<<< HEAD
    App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
    file = request.FILES.get('file-xxx')
    App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

=======
    file = request.FILES.get('xxxx')
>>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5

===== 之上的 <<<<<<< HEAD 是本地的内容
===== 之下的 >>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5 是远程的内容(这个commit号每次都会不同)
根据实际情况, 删除多余的内容(包括===== >>>>> <<<<<< 之类的), 修改冲突的地方, 如果以本地的代码为准的话, 会得到如下结果:

App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
file = request.FILES.get('file-xxx')
App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

然后 git commit -am '提交的信息' 就解决了冲突.
最后, 也可以将本地的修改同步到远程 git 仓库: git push

git pull 时, 本地还有未commit 的文件

从远程仓库更新时, 假使本地还有没commit的文件A, 远程仓库的A文件却被修改了. 此时进行 git pull 时有如下信息:

6a707cc..f93575d  master     -> origin/master
Updating 6a707cc..f93575d
error: Your local changes to the following files would be overwritten by merge:
    apps/myapp/utils.py
Please, commit your changes or stash them before you can merge.
Aborting

此时, 如果不想将本地文件commit(可能只是临时的修改), 但是又像将远程的仓库更新下来, 可以这样:

$ git stash    # 先将自己的改变保存起来
Saved working directory and index state WIP on master: 6a707cc ...
HEAD is now at 6a707cc ...
$ git pull     # 从远程仓库更新
Updating 6a707cc..f93575d
... ...
$ git stash pop   # 将自己的修改合并到更新后的代码中

最后一步如果有冲突, 再参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.

git 分支合并时的冲突

正在开发的分支和主分支的编辑了同一个文件时, 在主分支上进行 merge 的时候可能会产生冲突.
以下构造一个冲突的示例:

$ git branch test  # 创建一个分支 test, 但是没有进入test分支, 此时还在 master 分支上.
$ vim xxxx         # 编辑 master 分支上的一个已有的文件
$ git commit -am 'xxx message'  # 提交 master 分支的修改
$ git checkout test    # 切换到 test 分支
$ vim xxxx             # 编辑之前在 master 上编辑的文件, 可以编辑同一个地方, 造成冲突
$ git commit -am 'xxx message'  # 提交 test 分支的修改
$ git checkout master           # 切换到 master 分支
$ git merge test                # 将 test 分支合并到 master 分支, 由于上面编辑了同一文件, 这里会产生冲突
Auto-merging xxxx
CONFLICT (content): Merge conflict in xxxx
Automatic merge failed; fix conflicts and then commit the result.

最后, 参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.

通过 git 提取补丁

提取的补丁的方法有多种:

$ git format-patch -1     # 提取本次 commit 和上次 commit 之间的不同, 并生成patch文件
$ git format-patch -2     # 提取本次 commit 和 上上次 commit 之间的不同, 并生成patch文件
$ git format-patch commit号1 commit号2  # 提取2次commit号之间的不同, 并生成patch文件 (commit号可以通过 git log 来查看)
$ git format-patch tag1 tag2            # 提取2次tag之间的不同, 并生成patch文件 (tag可以通过 git tag 来查看)

通过 git 提取指定版本的源码

这个功能在部署的时候比较有用.

$ git archive --format=tar --prefix="tagxx/" tagxx > ../tagxx.tar  # 获取 tagxx 的源码, 加了 --prefix 的作用是在最终的 tagxx.tar 中加了一层文件夹 tagxx

上面的 tagxx 也可以是 commit号

声明:
本文为随笔,仅供日常学习总结。来源为自己总结和部分网络资料。如有错误或侵权可以告知作者,以便及时处理。

posted @ 2020-12-02 15:29  左瑾  阅读(97)  评论(0)    收藏  举报