zoukankan      html  css  js  c++  java
  • git用法总结

    1.git撤销操作

    1.1git追加提交操作

    有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令尝试重新提交:

    $ git commit --amend

    例如,你提交后发现忘记了暂存某些需要的修改,可以像下面这样操作:

    $ git commit -m 'initial commit'
    $ git add forgotten_file
    $ git commit --amend

    最终你只会有一个提交 - 第二次提交将代替第一次提交的结果。

    1.2git取消暂存文件

     在 “Changes to be committed” 文字正下方,提示使用 git reset HEAD <file>... 来取消暂存。 所以,我们可以这样来取消暂存 CONTRIBUTING.md 文件:

       git reset HEAD CONTRIBUTING.md

     Unstaged changes after reset:

      M	CONTRIBUTING.md
    最终你只会有一个提交 - 第二次提交将代替第一次提交的结果。1.3取消对文件的修改

    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:   CONTRIBUTING.md

    1.3git取消对文件的修改

    git checkout -- CONTRIBUTING.md
    git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        renamed:    README.md -> README

    可以看到那些修改已经被撤消了。

    需要知道 git checkout -- [file] 是一个危险的命令,这很重要。 你对那个文件做的任何修改都会消失 - 你只是拷贝了另一个文件来覆盖它。 除非你确实清楚不想要那个文件了,否则不要使用这个命令。

    2.远程仓库的使用

    2.1查看远程仓库

    $ git remote
    origin

    你也可以指定选项 -v,会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL。

    $ git remote -v
    origin	https://github.com/schacon/ticgit (fetch)
    origin	https://github.com/schacon/ticgit (push)

    如果想要查看某一个远程仓库的更多信息,可以使用 git remote show [remote-name] 命令。 如果想以一个特定的缩写名运行这个命令,例如 origin,会得到像下面类似的信息:

    $ git remote show origin
    * remote origin
      Fetch URL: https://github.com/schacon/ticgit
      Push  URL: https://github.com/schacon/ticgit
      HEAD branch: master
      Remote branches:
        master                               tracked
        dev-branch                           tracked
      Local branch configured for 'git pull':
        master merges with remote master
      Local ref configured for 'git push':
        master pushes to master (up to date)

    2.2添加远程仓库

     $ git remote

    origin
    $ git remote add pb https://github.com/paulboone/ticgit
    $ git remote -v
    origin	https://github.com/schacon/ticgit (fetch)
    origin	https://github.com/schacon/ticgit (push)
    pb	https://github.com/paulboone/ticgit (fetch)
    pb	https://github.com/paulboone/ticgit (push) 

    2.3从远程仓库抓取和拉取

    就如刚才所见,从远程仓库中获得数据,可以执行:

    $ git fetch [remote-name]

    这个命令会访问远程仓库,从中拉取所有你还没有的数据。 执行完成后,你将会拥有那个远程仓库中所有分支的引用,可以随时合并或查看。

    如果你使用 clone 命令克隆了一个仓库,命令会自动将其添加为远程仓库并默认以 “origin” 为简写。 所以,git fetch origin 会抓取克隆(或上一次抓取)后新推送的所有工作。 必须注意 git fetch 命令会将数据拉取到你的本地仓库 - 它并不会自动合并或修改你当前的工作。 当准备好时你必须手动将其合并入你的工作

    如果你有一个分支设置为跟踪一个远程分支(阅读下一节与 Git 分支 了解更多信息),可以使用 git pull命令来自动的抓取然后合并远程分支到当前分支。 这对你来说可能是一个更简单或更舒服的工作流程;默认情况下,git clone 命令会自动设置本地 master 分支跟踪克隆的远程仓库的 master 分支(或不管是什么名字的默认分支)。 运行 git pull 通常会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支。

    2.4推送远程仓库

    当你想分享你的项目时,必须将其推送到上游。 这个命令很简单:git push [remote-name] [branch-name]。 当你想要将 master 分支推送到 origin 服务器时(再次说明,克隆时通常会自动帮你设置好那两个名字),那么运行这个命令就可以将你所做的备份到服务器:

    $ git push origin master

    只有当你有所克隆服务器的写入权限,并且之前没有人推送过时,这条命令才能生效。 当你和其他人在同一时间克隆,他们先推送到上游然后你再推送到上游,你的推送就会毫无疑问地被拒绝。 你必须先将他们的工作拉取下来并将其合并进你的工作后才能推送。 阅读 Git 分支 了解如何推送到远程仓库服务器的详细信息

     2.5 远程仓库的移除与重命名

    $ git remote rename pb paul
    $ git remote
    origin
    paul

    $ git remote rm paul
    $ git remote
    origin

    3git标签

    3.1 查看标签

    像其他版本控制系统(VCS)一样,Git 可以给历史中的某一个提交打上标签,以示重要。 比较有代表性的是人们会使用这个功能来标记发布结点(v1.0 等等)。 在本节中,你将会学习如何列出已有的标签、如何创建新标签、以及不同类型的标签分别是什么。

    $ git tag
    v0.1
    v1.3
    $ git tag -l 'v1.8.5*'
    v1.8.5
    v1.8.5-rc0
    v1.8.5-rc1
    v1.8.5-rc2
    v1.8.5-rc3
    v1.8.5.1
    v1.8.5.2
    v1.8.5.3
    v1.8.5.4
    v1.8.5.5

    3.2创建标签

    3.2.1 创建 附注标签

    $ git tag -a v1.4 -m 'my version 1.4'
    $ git tag
    v0.1
    v1.3
    v1.4

    3.2.2 创建轻量标签

    $ git tag v1.4-lw
    $ git tag
    v0.1
    v1.3
    v1.4
    v1.4-lw
    v1.5

     

    3.2.2 后期打标签

    $ git tag -a v1.2 9fceb02

     

     3.3共享标签

    git push origin v1.5   推送某一个标签

    git push origin --tags 推送所有标签

    3.4检出标签 

    $ git checkout -b version2 v2.0.0   可以使用 git checkout -b [branchname] [tagname] 在特定的标签上创建一个新分支:
    Switched to a new branch 'version2'

     4git 别名管理

    $ git config --global alias.co checkout
    $ git config --global alias.br branch
    $ git config --global alias.ci commit
    $ git config --global alias.st status

    在创建你认为应该存在的命令时这个技术会很有用。 例如,为了解决取消暂存文件的易用性问题,可以向 Git 中添加你自己的取消暂存别名:

    $ git config --global alias.unstage 'reset HEAD --'

    这会使下面的两个命令等价:

    $ git unstage fileA
    $ git reset HEAD -- fileA

    这样看起来更清楚一些。 通常也会添加一个 last 命令,像这样:

    $ git config --global alias.last 'log -1 HEAD'

     5.分支管理

    5.1创建分支

    $ git branch testing

    5.2切换分支

    $ git checkout testing

    5.3创建并切换分支

    $ git checkout -b iss53
    Switched to a new branch "iss53"

    它是下面两条命令的简写:

    $ git branch iss53
    $ git checkout iss53 

    5.3删除分支

    $ git branch -d hotfix
    Deleted branch hotfix (3a0874c).
    
    

    5.4 分支查看

    
    

    git branch 命令不只是可以创建与删除分支。 如果不加任何参数运行它,会得到当前所有分支的一个列表:

    
    
    $ git branch
      iss53
    * master
      testing
    $ git branch -v
      iss53   93b412c fix javascript issue
    * master  7a98805 Merge branch 'iss53'
      testing 782fd34 add scott to the author list in the readmes

    --merged 与 --no-merged 这两个有用的选项可以过滤这个列表中已经合并或尚未合并到当前分支的分支。 如果要查看哪些分支已经合并到当前分支,可以运行 git branch --merged

    $ git branch --merged
      iss53
    * master

    因为之前已经合并了 iss53 分支,所以现在看到它在列表中。 在这个列表中分支名字前没有 * 号的分支通常可以使用 git branch -d 删除掉;你已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。

    查看所有包含未合并工作的分支,可以运行 git branch --no-merged

    $ git branch --no-merged
      testing

    这里显示了其他分支。 因为它包含了还未合并的工作,尝试使用 git branch -d 命令删除它时会失败:

    $ git branch -d testing
    error: The branch 'testing' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D testing'
     

    6.git log

    6.1 git log用法一

    $ git log --oneline --decorate --graph --all
    * c2b9e (HEAD, master) made other changes
    | * 87ab2 (testing) made a change
    |/
    * f30ab add feature #32 - ability to add new formats to the
    * 34ac2 fixed bug #1328 - stack overflow under certain conditions
    * 98ca9 initial commit of my project

     7.暂存文件

    7.1 暂存文件

    运行 git stash 或 git stash save: 如果需要添加注释则必须要加save 

    $ git stash

    Saved working directory and index state 
      "WIP on master: 049d078 added the index file"
    HEAD is now at 049d078 added the index file
    (To restore them type "git stash apply")

     --keep-index 选项。 它告诉 Git 不要储藏任何你通过 git add 命令已暂存的东西。

    $ git status -s
    M  index.html
     M lib/simplegit.rb
    
    $ git stash --keep-index
    Saved working directory and index state WIP on master: 1b65b17 added the index file
    HEAD is now at 1b65b17 added the index file
    
    $ git status -s
    M  index.html

    git stash 只会储藏已经在索引中的文件。 如果指定 --include-untracked 或 -u 标记,Git 也会储藏任何创建的未跟踪文件。 

    $ git status -s
    M  index.html
     M lib/simplegit.rb
    ?? new-file.txt
    
    $ git stash -u
    Saved working directory and index state WIP on master: 1b65b17 added the index file
    HEAD is now at 1b65b17 added the index file
    
    $ git status -s
    $
     

     7.2查看暂存列表

    用 git stash list

    $ git stash list
    stash@{0}: WIP on master: 049d078 added the index file
    stash@{1}: WIP on master: c264051 Revert "added file_size"
    stash@{2}: WIP on master: 21d80a5 added number to log

    7.2恢复暂存文件

     

    git stash apply stash@{2}。 如果不指定一个储藏,Git 认为指定的是最近的储藏:

    $ git stash apply
    # On branch master
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #
    #      modified:   index.html
    #      modified:   lib/simplegit.rb
    #

    文件的改动被重新应用了,但是之前暂存的文件却没有重新暂存。 想要那样的话,必须使用 --index 选项来运行 git stash apply 命令,来尝试重新应用暂存的修改。 如果已经那样做了,那么你将回到原来的位置:

    $ git stash apply --index
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #      modified:   index.html
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #
    #      modified:   lib/simplegit.rb
    #

    7.3从暂存创建分支

    $ git stash branch testchanges
    Switched to a new branch "testchanges"
    # On branch testchanges
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #      modified:   index.html
    #
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #
    #      modified:   lib/simplegit.rb
    #
    Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)

    8.清理未跟踪的文件

    对于工作目录中一些工作或文件,你想做的也许不是储藏而是移除。 git clean 命令会帮你做这些事。

    有一些通用的原因比如说为了移除由合并或外部工具生成的东西,或是为了运行一个干净的构建而移除之前构建的残留。

    你需要谨慎地使用这个命令,因为它被设计为从工作目录中移除未被追踪的文件。 如果你改变主意了,你也不一定能找回来那些文件的内容。 一个更安全的选项是运行 git stash --all 来移除每一样东西并存放在栈中。

    你可以使用`git clean`命令去除冗余文件或者清理工作目录。 使用`git clean -f -d`命令来移除工作目录中所有未追踪的文件以及空的子目录。 -f 意味着 强制 或 “确定移除”。

    如果只是想要看看它会做什么,可以使用 -n 选项来运行命令,这意味着 “做一次演习然后告诉你 将要 移除什么”

    $ git clean -d -n
    Would remove test.o
    Would remove tmp/

    默认情况下,git clean 命令只会移除没有忽略的未跟踪文件。 任何与 .gitiignore 或其他忽略文件中的模式匹配的文件都不会被移除。 如果你也想要移除那些文件,例如为了做一次完全干净的构建而移除所有由构建生成的 .o 文件,可以给 clean 命令增加一个 -x 选项。

    $ git status -s
     M lib/simplegit.rb
    ?? build.TMP
    ?? tmp/
    
    $ git clean -n -d
    Would remove build.TMP
    Would remove tmp/
    
    $ git clean -n -d -x
    Would remove build.TMP
    Would remove test.o
    Would remove tmp/

    如果不知道 git clean 命令将会做什么,在将 -n 改为 -f 来真正做之前总是先用 -n 来运行它做双重检查。 另一个小心处理过程的方式是使用 -i 或 “interactive” 标记来运行它。

    这将会以交互模式运行 clean 命令。

    $ git clean -x -i
    Would remove the following items:
      build.TMP  test.o
    *** Commands ***
        1: clean                2: filter by pattern    3: select by numbers    4: ask each             5: quit
        6: help
    What now>
     

     

     
  • 相关阅读:
    Charles网络工具
    查找最长子串的长度(不重复字符)
    KMP算法
    java并发编程实战:第十四章----构建自定义的同步工具
    java并发编程实战:第十二章---并发程序的测试
    java并发编程实战:第二章----线程安全性
    java并发编程实战:第三章----对象的共享
    java并发编程实战:第四章----对象的组合
    java并发编程实战:第五章----基础构建模块
    java并发编程实战:第六章----任务执行
  • 原文地址:https://www.cnblogs.com/z-test/p/10234475.html
Copyright © 2011-2022 走看看