• git 常用命令


     

    上传代码前需要pull(拉下来)之后再上传

    输入git init     将目录变成git可管理的仓库

    输入git remote add origin ssh项目链接     将本地仓库与远程仓库关联

    输入git add .      将本地仓库中所有变化提交到暂存区

    输入git status     用于查看是否有文件未提交,提示 nothing to commit,work tree clean 表示文件已全部提交

    输入git commit -m “20190128”     将文件提交到仓库 “”中是解释

    输入git push origin master 将代码推送到gitlab端 (-u 是第一次提交使用 ,其他时间buyong)

    切换分支:

    $ git checkout -bdevelopNew origin/developNew
    
    
    
    #已经切换到v0.9rc1分支了
    $ git branch
      master
    * v0.9rc1
    
    #切换回master分支
    $ git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.

     git init 把当前的目录变成可以管理的git仓库,生成隐藏.git文件。
      git add XX 把xx文件添加到暂存区去。
      git commit –m “XX” 提交文件 –m 后面的是注释。
      git status 查看仓库状态
      git diff XX 查看XX文件修改了那些内容
      git log 查看历史记录
      git reset --hard HEAD^ 或者 git reset --hard HEAD~ 回退到上一个版本
      (如果想回退到100个版本,使用git reset –hard HEAD~100 )
      cat XX 查看XX文件内容
      git reflog 查看历史记录的版本号id
      git checkout -- XX 把XX文件在工作区的修改全部撤销。
      git rm XX 删除XX文件
      git remote add origin http://git.fengqun.ltd/liucong/pizza.git 关联一个远程库 (git地址)

           git clone https://github.com/tugenhua0707/testgit 从远程库中克隆

      git push –u(第一次要用-u 以后不需要) origin master 把当前master分支推送到远程库
     

      git branch name 创建分支
      git checkout –b dev 创建dev分支 并切换到dev分支上
      git branch 查看当前分支

      git branch  -a查看远程分支

        git checkout master 切换回master分支

      git merge dev 在当前的分支上合并dev分支

      git branch -D BranchName 删除本地分支

      其中-D也可以是--delete:

      git branch --delete BranchName

      git branch -r -D origin/BranchName  删除本地的远程分支

           git push origin -d BranchName 远程删除git服务器上的分支

      其中-d也可以是--delete:

      git push origin --delete BranchName

           git push origin v2.0_dev //推送本地的v2.0_dev分支到远程
      git remote 查看远程库的信息
      git remote –v 查看远程库的详细信息
      git pull origin master 获取远程文件 指定分支
      git diff XX 查看XX文件修改了那些内容
      git log 查看历史记录
      vim .gitignore 添加忽略文件
      git check-ignore 检查忽略文件命令是否正确
      :wq 保存退出VI命令  :q!不保存退出
      git log -p -2 查看最近两次详细日志
      git reset --hard 代码库覆盖本地
      git pull
      回退版本:
      git reset --hard 139dcfaa558e3276b30b6b2e5cbbb9c00bbdca96 
      推送到远程服务器
      git push -f -u origin master 
      迁移地址:
      git remote set-url origin http://git.fengqun.ltd/fengqun/pizza.git

      # 将b分支合并到当前分支

      git merge b

      git 用放弃本地修改用远程覆盖本地:

      git fetch --all

      git reset --hard origin/master

      git fetch 只是下载远程的库的内容,不做任何的合并git reset 把HEAD指向刚刚下载的最新的版本

      git update-index --assume-unchanged .classpath //忽略已跟踪文件的修改

    标签管理:

      列出现有标签:

      git tag

      创建含附注的标签用 -a (译注:取 annotated 的首字母)指定标签名字:

      git tag -a v1.1 -m 'version 1.1'

      推送所有本地新增的标签:

      git push origin --tags

      拉取使用tag分支:

      1、 git clone 整个仓库,然后 git checkout tag_name 取得 tag 对应的代码. 

    # git 可能会提示当前处于一个“detached HEAD” 状态,因为 tag 相当于是一个快照,是不能更改它的代码的,如果要在 tag 代码的基础上做修改,需要一个分支: 

      2、git checkout -b branch_name tag_name 
       tag会 创建一个分支,然后就和普通的 git 操作一样了

      删除tag标签:

      git tag -d v0.9 删除本地tag标签

      git push origin :refs/tags/v0.9 删除远程标签

      已经推送(push)过的文件,想在以后的提交时忽略此文件,即使本地已经修改过,而且不删除git远程库中相应文件 执行命令 git update-index --assume-unchanged Xml/config.xml 后面的 Xml/config.xml 是要忽略的文件的路径

      git更新远程分支列表

      git remote update origin --prune

      git remote update origin -p

    git小技巧:git blame && git show 查看某一行代码的修改历史

    先查看某行代码由谁写的,在哪个commit中提交的:

    git blame file_name

    其显示格式为: 
    commit ID | 代码提交作者 | 提交时间 | 代码位于文件中的行数 | 实际代码 

    f604879e (yingyinl              2014-09-23 23:39:55 -0700   35) typedef enum
    9be6b4bd (yingyinl              2014-01-01 21:22:50 -0800   36) {
    597886b5 (Shengjie Yu           2015-09-29 12:00:24 +0800   37)     Index_R_Hue = 0,    //Index_Range0_Hue
    9be6b4bd (yingyinl              2014-01-01 21:22:50 -0800   38)     Index_R_Sat,
    9be6b4bd (yingyinl              2014-01-01 21:22:50 -0800   39)     Index_R_brt,
    9be6b4bd (yingyinl              2014-01-01 21:22:50 -0800   40)     Index_R_Offset,
    9be6b4bd (yingyinl              2014-01-01 21:22:50 -0800   41)     Index_R_Gain,
    f604879e (yingyinl              2014-09-23 23:39:55 -0700   42) 

    就可以知道commit ID了,然后使用命令:git show commitID来看

    1、git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令:

     git fetch orgin master //将远程仓库的master分支下载到本地当前branch中
    
     git log -p master  ..origin/master //比较本地的master分支和origin/master分支的差别
    
     git merge origin/master //进行合并

    也可以用以下指令:

    git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支
    
    git diff tmp //將當前分支和tmp進行對比
    
    git merge tmp //合并tmp分支到当前分支

    2. git pull:相当于是从远程获取最新版本并merge到本地

    git pull origin master

    git pull 相当于从远程获取最新版本并merge到本地

    在实际使用中,git fetch更安全一些

    1.1 创建git数据仓库

    [root@gitlab ~]# mkdir git_data
    
    [root@gitlab ~]# ll
    
    total 4
    
    -rw-------. 1 root root 1347 Mar 12 11:09 anaconda-ks.cfg
    
    drwxr-xr-x  2 root root    6 Mar 20 20:40 git_data

    1.2 初始化git目录

    [root@gitlab git_data]# git init
    
    Initialized empty Git repository in /root/git_data/.git/

    1.3 查看git当前工作状态

    [root@gitlab git_data]# git status
    
    # On branch master
    
    #
    
    # Initial commit
    
    #
    
    nothing to commit (create/copy files and use "git add" to track)

    1.4 创建数据-提交数据

    [root@gitlab git_data]# touch README
    
    [root@gitlab git_data]# ll
    
    total 0
    
    -rw-r--r-- 1 root root 0 Mar 20 20:47 README
    
    [root@gitlab git_data]# git status
    
    # On branch master
    
    #
    
    # Initial commit
    
    #
    
    # Untracked files:
    
    #   (use "git add <file>..." to include in what will be committed)
    
    #
    
    #   README
    
    nothing added to commit but untracked files present (use "git add" to track)

    1.5 把文件上传的暂存区

    [root@gitlab git_data]# git add  README
    
    [root@gitlab git_data]# git status
    
    # On branch master
    
    #
    
    # Initial commit
    
    #
    
    # Changes to be committed:
    
    #   (use "git rm --cached <file>..." to unstage)
    
    #
    
    #   new file:   README

    1.6 把暂存区的文件提交的版本库,-m是对文件的说明信息

    [root@gitlab git_data]# git commit -m 'jiangboyang'
    
    [master (root-commit) bb1dc0b] jiangboyang
    
     1 file changed, 0 insertions(+), 0 deletions(-)
    
     create mode 100644 README

    1.7 如果修改了已经上传到仓库的文件后,可以一步上传到仓库

    说明:这里的一步上传到仓库是指已经存在仓库中的文件,修改后,支持这样的操作

    [root@gitlab git_data]# echo daya >>test
    
    [root@gitlab git_data]# 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:   test
    
    #
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    [root@gitlab git_data]# git commit -a -m "xiugaiwenjian"
    
    [master 3bd1ed4] xiugaiwenjian
    
     1 file changed, 1 insertion(+)
    
    [root@gitlab git_data]# git status
    
    # On branch master
    
    nothing to commit, working directory clean

    1.8 删除暂存区的数据

    [root@gitlab git_data]# git reset HEAD test2
    
    [root@gitlab git_data]# git rm --cached test
    
    rm 'test'
    
    [root@gitlab git_data]# git rm -f test2        删除暂存区文件并且删除源文件
    
    rm 'test2'
    
    [root@gitlab git_data]# ll
    
    total 4
    
    -rw-r--r-- 1 root root 5 Mar 20 20:16 test

    1.9 重命名暂存区文件

    [root@gitlab git_data]# git mv test test.txt
    
    [root@gitlab git_data]# ll
    
    total 0
    
    -rw-r--r-- 1 root root 0 Mar 20 20:47 README
    
    -rw-r--r-- 1 root root 0 Mar 20 21:01 test.txt
    
    [root@gitlab git_data]# git status
    
    # On branch master
    
    # Changes to be committed:
    
    #   (use "git reset HEAD <file>..." to unstage)
    
    #
    
    #   new file:   test.txt

    1.10 查看历史记录

    查看历史提交记录

    [root@gitlab git_data]# git log
    
    commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422
    
    Author: dy <15555513217@163.com>
    
    Date:   Tue Mar 20 20:51:34 2018 +0800

    jiangboyang

    1.11 查看最近几条提交记录

    [root@gitlab git_data]# git log -3
    
    commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422
    
    Author: dy <15555513217@163.com>
    
    Date:   Tue Mar 20 20:51:34 2018 +0800

    jiangboyang

    git log   #→查看提交历史记录
    git log -2   #→查看最近几条记录
    git log -p -1  #→-p显示每次提交的内容差异,例如仅查看最近一次差异
    git log --stat -2 #→--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息
    git log --pretty=oneline  #→--pretty根据不同的格式展示提交的历史信息
    git log --pretty=fuller -2 #→以更详细的模式输出提交的历史记录
    git log --pretty=fomat:"%h %cn"  #→查看当前所有提交记录的简短SHA-1哈希字串与提交者的姓名,其他格式见备注。
    #→还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有:

    格式

    说明

    %s

    提交说明。

    %cd

    提交日期。

    %an

    作者的名字。

    %cn

    提交者的姓名。

    %ce

    提交者的电子邮件。

    %H

    提交对象的完整SHA-1哈希字串。

    %h

    提交对象的简短SHA-1哈希字串。

    %T

    树对象的完整SHA-1哈希字串。

    %t

    树对象的简短SHA-1哈希字串。

    %P

    父对象的完整SHA-1哈希字串。

    %p

    父对象的简短SHA-1哈希字串。

    %ad

    作者的修订时间。

    1.12 还原未来数据

    什么是未来数据?就是还原到历史数据了,但是你后悔了,想撤销更改,但是git log找不到这个版本了

    git reflog    ---查看未来历史的更新点

    1.13 还原历史数据

    [root@gitlab git_data]# git log                     利用git log查看版本号
    
    commit 3bd1ed4424eb5e66cfc5ce855228a3547e5bef47
    
    Author: dy <15555513217@163.com>
    
    Date:   Tue Mar 20 20:16:46 2018 +0800
    
     
    
        xiugaiwenjian
    
    commit 90a7e9ce4469fda8712c0a9cdb39693bd34b92c5
    
    Author: dy <15555513217@163.com>
    
    Date:   Tue Mar 20 20:10:09 2018 +0800
    
     
    
        ceshiwenjian_2018.3.21
    
    [root@gitlab git_data]# ll
    
    total 4
    
    -rw-r--r-- 1 root root 5 Mar 20 20:16 test
    
    [root@gitlab git_data]# git reset --hard 90a7e9ce 
    
    HEAD is now at 90a7e9c ceshiwenjian_2018.3.21
    
    [root@gitlab git_data]# cat test
    
    [root@gitlab git_data]# git reset --hard 3bd1ed4424e
    
    HEAD is now at 3bd1ed4 xiugaiwenjian
    
    [root@gitlab git_data]# cat test
    
    daya

    1.14 标签使用

    [root@gitlab git_data]# git tag v200217        给当前提交内容打一个标签,每次提交都可以打tag
    
    [root@gitlab git_data]# git tag                查看当前所有标签
    
    v200217
    
    [root@gitlab git_data]# git show v200217       查看当前标签详细信息
    
    commit 5f499e63e4db0ce040e0527467a085fe644519b1
    
    Author: dy <15555513217@163.com>
    
    Date:   Wed Mar 21 13:19:56 2018 +0800
    
     
    
        xiugai
    
     
    
    diff --git a/jiang.txt b/jiang.txt
    
    index ba9c1ad..c5e58fc 100644
    
    --- a/jiang.txt
    
    +++ b/jiang.txt
    
    @@ -1 +1,2 @@
    
     nihao
    
    +888
    
    [root@gitlab git_data]# git reset --hard 8c44f2f   
    
    HEAD is now at 8c44f2f 777
    
    [root@gitlab git_data]# cat jiang.txt
    
    nihao
    
    [root@gitlab git_data]# git reset --hard v200217    
    
    HEAD is now at 5f499e6 xiugai
    
    [root@gitlab git_data]# cat jiang.txt
    
    nihao
    
    888

    1.15 对比数据

    diff命令可以对比当前文件与仓库已保存文件的区别,从而知道对文件做了哪些修改

    [root@gitlab git_data]# cat jiang.txt
    
    nihao
    
    888
    
    [root@gitlab git_data]# echo 666 >>jiang.txt
    
    [root@gitlab git_data]# git diff jiang.txt
    
    diff --git a/jiang.txt b/jiang.txt
    
    index c5e58fc..a232fb5 100644
    
    --- a/jiang.txt
    
    +++ b/jiang.txt
    
    @@ -1,2 +1,3 @@
    
     nihao
    
     888
    
    +666
    
    [root@gitlab git_data]#

    第2章 分支结构

    [root@gitlab git_data]# git branch                      查看当前系统所有分支
    
    * master
    
    [root@gitlab git_data]# git branch linux                创建分支
    
    [root@gitlab git_data]# git branch
    
      linux
    
    * master
    
    [root@gitlab git_data]# git checkout linux              切换分支
    
    Switched to branch 'linux'
    
    [root@gitlab git_data]# git branch
    
    * linux
    
      master
    
    [root@gitlab git_data]# git branch -D linux             删除分支
    
    Deleted branch linux (was 5f499e6).

    2.1 代码合并:

    [root@gitlab git_data]# git merge linux
    
    Updating 90a7e9c..84a289b
    
    Fast-forward
    
     test | 1 +
    
     1 file changed, 1 insertion(+)

    2.1.1 合并分支时的冲突问题:

    [root@gitlab git_data]# git branch
    
      linux
    
    * master
    
    [root@gitlab git_data]# cat test
    
    linux branch
    
    [root@gitlab git_data]# echo nihao >>test
    
    [root@gitlab git_data]# git commit -a -m "nihao"
    
    [root@gitlab git_data]# git checkout linux
    
    Switched to branch 'linux'
    
    [root@gitlab git_data]# echo buhao >>test    
    
    [root@gitlab git_data]# git commit -a -m "buhao"
    
    [linux c21fd3c] buhao
    
     1 file changed, 1 insertion(+)
    
    [root@gitlab git_data]# git status
    
    # On branch linux
    
    nothing to commit, working directory clean
    
    [root@gitlab git_data]# git branch
    
    * linux
    
      master
    
    [root@gitlab git_data]# git checkout master
    
    Switched to branch 'master'
    
    [root@gitlab git_data]# git merge linux
    
    Auto-merging test
    
    CONFLICT (content): Merge conflict in test
    
    Automatic merge failed; fix conflicts and then commit the result.
    
    [root@gitlab git_data]# vim test                      手动解决冲突,只保留想要保留的数据然后保存
    
    [root@gitlab git_data]# git commit  -a -m "hebingshibai"
    
    [master e5092ee] hebingshibai
    
    [root@gitlab git_data]# cat test
    
    linux branch
    
    nihao

    第3章 windows客户端使用

    windows 上git软件网站 https://git-for-windows.github.io

        软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git-2.15.1.2-64-bit.exe  软件安装默认即可。

    第4章 搭建gitlab私有仓库

    4.1 安装gitlab,软件比较大,注意内容空间

    yum -y localinstall gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
    gitlab-ctl reconfigure  #→初始化,就执行一次
    gitlab-ctl status

    4.2 在浏览器中输入ip地址进行访问,我这里ip是10.0.0.63

    image.png

    image.png

    image.png

    1.1 定义项目名称

    image.png

    image.png

    1.1 创建完成后会提示没有ssh密钥:

    在服务端生成密钥对,复制公钥内容粘贴到网页上即可

    [root@gitlab ~]# ssh-keygen
    
    Generating public/private rsa key pair.
    
    Enter file in which to save the key (/root/.ssh/id_rsa):
    
    Created directory '/root/.ssh'.
    
    Enter passphrase (empty for no passphrase):
    
    Enter same passphrase again:
    
    Your identification has been saved in /root/.ssh/id_rsa.
    
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    
    The key fingerprint is:
    
    84:76:c5:d0:b0:7a:28:b0:e2:0e:12:7c:d7:cf:4d:a4 root@gitlab
    
    The key's randomart image is:
    
    +--[ RSA 2048]----+
    
    |        o=.      |
    
    |       . oo      |
    
    |  .   o +  .     |
    
    |.  o ..=  o      |
    
    |o.....o.SE .     |
    
    |.o. .. .o o      |
    
    |o.       o .     |
    
    |+                |
    
    | .               |
    
    +-----------------+
    
    [root@gitlab ~]# cd /root/.ssh/
    
    [root@gitlab .ssh]# cat id_rsa.pub
    
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAGOae1O+UBTUPJNIIgOTdgB0KXT26HhZgh5JFRgau6BifEI34goNMYxNQS5pHiSO6GdHbk+wSi5ZB3Xl9nWYL29zbtSC7TDWEoPlz/FCbk4LXylFF+20MXt0hu+NsBS8xkMk0uyIt4ELEfZ8KO/Ki2zT6aFUJrqmkqxnn9hQyoiOPZv0ewQEYHfgUnXlGkA21arIOL3fMuaLoGcuyeiTEbL2H60nG8N3kC3B/4EcUs18P9rqAKv2A2tMsHoQyzfTRNSHHf1bWnc28oZ4KcQrdIfOQkLQCXMF6Vb9HWmJ01xCdwMiTbcGTQnkudr8bmeJitNnlqIqoZ2sCYsHf52gR root@gitlab

    image.png

    1.1 上传文件到gitlab私有仓库:

    [root@gitlab ~]# cd 43team
    
    [root@gitlab 43team]# touch README.md
    
    [root@gitlab 43team]# git add README.md
    
    [root@gitlab 43team]# git commit -m "add README"
    
    [master (root-commit) 9429222] add README
    
     1 file changed, 0 insertions(+), 0 deletions(-)
    
     create mode 100644 README.md
    
    [root@gitlab 43team]# git push -u origin master
    
    Counting objects: 3, done.
    
    Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done.
    
    Total 3 (delta 0), reused 0 (delta 0)
    
    To git@10.0.0.63:root/43team.git
    
     * [new branch]      master -> master
    
    Branch master set up to track remote branch master from origin.
  • 相关阅读:
    Java package和import
    2文本
    dotnet学习制表技巧
    视频教程:小型登陆系统(五)
    读书札记:ASP.NET网站管理工具遇到错误。请返回上一页并重试
    视频教程:小型登陆系统(三)
    视频教程:小型登陆系统(四)
    视频教程:小型登陆系统(二)
    视频教程:小型登陆系统(一)
    视频教程:小型登陆系统(完)
  • 原文地址:https://www.cnblogs.com/pipiyan/p/10474965.html
Copyright © 2020-2023  润新知