天天看點

Git基礎入門(七)Git撤銷操作和遠端倉庫管理

撤銷操作:

注意:Git的有些撤消操作是不可逆的。 這是在使用Git的過程中,會因為操作失誤而導緻之前的工作丢失的少有的幾個地方之一

取消暫存的檔案

git add a.py b.py 

git status 

    On branch master

    Changes to be committed:

      (use "git reset HEAD <file>..." to unstage)           #提示如何撤銷

    modified:   a.py

    modified:   b.py

git reset HEAD b.py                                         #取消暫存b.py

    Unstaged changes after reset:

    M b.py

      (use "git reset HEAD <file>..." to unstage)

    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)         #提示可以撤銷對檔案的修改

撤消對檔案的修改

    git checkout b.py

    git status 

        On branch master

        Changes to be committed:

          (use "git reset HEAD <file>..." to unstage)

        modified:   a.py

git checkout -- [file] 是一個危險的指令,如果執行了這個指令你對那個檔案做的任何修改都會消失

遠端倉庫:

    遠端倉庫是指托管在網際網路或其他網絡中的你的項目的版本庫,遠端倉庫可以有多個,通常有些倉庫對你隻讀,有些則可以讀寫

    管理遠端倉庫包括了解如何添加遠端倉庫、移除遠端倉庫、管理不同的遠端分支并定義它們是否被跟蹤等等

檢視遠端倉庫

    git remote              #檢視目前所有的遠端倉庫

        origin              #origin 是Git給你克隆的倉庫伺服器的預設名字

    -v選項,顯示遠端倉庫的簡寫與其對應的URL

    git remote -v

        origin https://github.com/libgit2/libgit2 (fetch)

        origin https://github.com/libgit2/libgit2 (push)

添加遠端倉庫

git remote add <shortname> <url>            #添加一個新的遠端Git倉庫,同時指定一個簡寫

git remote add test https://github.com/huyuan1999/17-10-22.git          #添加遠端倉庫

git remote -v

    origin https://github.com/libgit2/libgit2 (fetch)

    origin https://github.com/libgit2/libgit2 (push)

    test https://github.com/huyuan1999/17-10-22.git (fetch)

    test https://github.com/huyuan1999/17-10-22.git (push)

現在可以在指令行中使用test來代替整個URL

    git fetch test          #拉取遠端倉庫中的資訊(本地工作目錄中沒有的資訊)

從遠端倉庫中抓取與拉取

    git fetch [remote-name]     #拉取遠端倉庫中的資料(不會自動合并分支)

    如果使用clone指令克隆了一個倉庫,并将其添加為遠端倉庫預設以origin為簡寫。是以git fetch origin會抓取克隆後新推送的所有資料

    git pull [remote-name]      #自動的抓取然後合并遠端分支到目前分支

    預設情況下git clone會自動設定本地master分支跟蹤克隆的遠端倉庫master分支,運作git pull通常會從克隆的伺服器上抓取資料并自動嘗試合并到目前分支

推送到遠端倉庫

    git push [remote-name] [branch-name]            #推送指定分支到伺服器中

    git push test master              #git預設使用github做為遠端倉庫伺服器,如果想要推送到遠端倉庫則需要有對應的賬号和密碼

git remote show test

    * remote test                               #本地簡寫

      Fetch URL: https://github.com/huyuan1999/17-10-22.git

      Push  URL: https://github.com/huyuan1999/17-10-22.git

      HEAD branch: master                       #處于的分支                

      Remote branch:

        master tracked                          #掌握跟蹤

      Local ref configured for 'git push':

        master pushes to master (up to date)

遠端倉庫的移除與重命名

    git remote rename test hu               #重命名

    git remote

        origin

        hu

    git remote rm hu                        #移除

本文轉自  紅塵世間  51CTO部落格,原文連結:http://blog.51cto.com/hongchen99/1977551