git 初步使用

以前简单用过git ui,到ubuntu里面用git命令行,发现对git一窍不通,从新弄。css

 

      git 配置文件

  1. git的配置文件位置
    针对全部用户:/etc/gitconfig
    针对当前用户: ~/.gitconfigpython

  2. 查看配置的方法nginx

    git config --list
  3. 修改配置的方法git

    git config --global user.name "wangyubin" (修改的是~/.gitconfig) git config --system user.name "wangyubin" (修改的是/etc/gitconfig)

 

git 基本使用

 

    1. clone现有仓库github

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

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

      git status
    4. 初始化一个仓库swift

      git init

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

    5. 追加文件ruby

      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]是远程仓库的名字)

      3. 2 向远程分支推送数据

        git push [remote_repo] [remote_branch]

      4. 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号

相关文章
相关标签/搜索