1. 程式人生 > >Git中remote、fetch、pull、push命令介紹

Git中remote、fetch、pull、push命令介紹

Unlike centralized version control systems that have a client that is very different from a server, Git repositories are all basically equal and you simply synchronize between them. This makes it easy to have more than one remote repository - you can have some that you have read-only access to and others that you can write to as well.

So that you don't have to use the full URL of a remote repository every time you want to synchronize with it, Git stores an alias or nickname for each remote repository URL you are interested in. You use the git remote command to manage this list of remote repos that you care about.

git remote list your remote aliases

Without any arguments, Git will simply show you the remote repository aliases that it has stored. By default, if you cloned the project (as opposed to creating a new one locally), Git will automatically add the URL of the repository that you cloned from under the name 'origin'. If you run the command with the -v

 option, you can see the actual URL for each alias.

$ git remote
origin
$ git remote -v
origin	[email protected]:github/git-reference.git (fetch)
origin	[email protected]:github/git-reference.git (push)

You see the URL there twice because Git allows you to have different push and fetch URLs for each remote in case you want to use different protocols for reads and writes.

git remote add add a new remote repository of your project

If you want to share a locally created repository, or you want to take contributions from someone else's repository - if you want to interact in any way with a new repository, it's generally easiest to add it as a remote. You do that by running git remote add [alias] [url]. That adds [url] under a local remote named [alias].

For example, if we want to share our Hello World program with the world, we can create a new repository on a server (Using GitHub as an example), which should give you a URL, in this case "[email protected]:schacon/hw.git". To add that to our project so we can push to it and fetch updates from it we would do this:

$ git remote
$ git remote add github [email protected]:schacon/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)

Like the branch naming, remote alias names are arbitrary - just as 'master' has no special meaning but is widely used because git init sets it up by default, 'origin' is often used as a remote name because git clone sets it up by default as the cloned-from URL. In this case we'll name the remote 'github', but you could name it just about anything.

git remote rm removing an existing remote alias

Git addeth and Git taketh away. If you need to remove a remote - you are not using it anymore, the project is gone, etc - you can remove it with git remote rm [alias].

$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
$ git remote add origin git://github.com/pjhyett/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/pjhyett/hw.git (fetch)
origin	git://github.com/pjhyett/hw.git (push)
$ git remote rm origin
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)

git remote rename [old-alias] [new-alias] rename remote aliases

If you want to rename remote aliases without having to delete them and add them again you can do that by running git remote rename [old-alias] [new-alias]. This will allow you to modify the current name of the remote.

$ git remote add github [email protected]:schacon/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
$ git remote rename github origin
$ git remote -v
origin	[email protected]:schacon/hw.git (fetch)
origin	[email protected]:schacon/hw.git (push)

In a nutshell with git remote you can list our remote repositories and whatever URL that repository is using. You can use git remote add to add new remotes, git remote rm to delete existing ones or git remote rename [old-alias] [new-alias] to rename them.

git remote set-url update an existing remote URL

Should you ever need to update a remote's URL, you can do so with the git remote set-url command.

$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/pjhyett/hw.git (fetch)
origin	git://github.com/pjhyett/hw.git (push)
$ git remote set-url origin git://github.com/github/git-reference.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/github/git-reference.git (push)

In addition to this, you can set a different push URL when you include the --push flag. This allows you to fetch from one repo while pushing to another and yet both use the same remote alias.

$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/pjhyett/hw.git (push)

Internally, the git remote set-url command calls git config remote, but has the added benefit of reporting back any errors. git config remote on the other hand, will silently fail if you mistype an argument or option and not actually set anything.

For example, we'll update the github remote but instead reference it as guhflub in both invocations.

$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/github/git-reference.git (push)
$ git config remote.guhflub git://github.com/mojombo/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/github/git-reference.git (push)
$ git remote set-url guhflub git://github.com/mojombo/hw.git
fatal: No such remote 'guhflub'

In a nutshell, you can update the locations of your remotes with git remote set-url. You can also set different push and fetch URLs under the same remote alias.

相關推薦

Gitremotefetchpullpush命令介紹

Unlike centralized version control systems that have a client that is very different from a server, Git repositories are all basically equal and you simp

git命令git遠端操作相關命令(remote pushfetch pull)

開發十年,就只剩下這套架構體系了! >>>   

git遠端操作相關命令(remote pushfetch pull)

git remote 為了便於管理,Git要求每個遠端倉庫都必須指定一個倉庫名。為了便於管理,Git要求每個遠端倉庫都必須指定一個倉庫名。 git remote【檢視創庫名】 git remote 在我們clone了剛才的專案之後,預設會看到一個origin的遠端倉庫 git remote -v/--

git fetch git pullgit merge 的理解

真正理解 git fetch, git pull  要講清楚git fetch,git pull,必須要附加講清楚git remote,git merge 、遠端repo, branch 、 commit-id 以及 FETCH_HEAD。 1. 【git remote】首先

Git遠端倉庫的新增刪除,git pullgit featch的使用

一、 github新增、刪除遠端倉庫 git remote用於管理遠端倉庫 git remote 不帶引數時可以參看遠端倉庫名稱 git remote -v 可以檢視遠端倉庫名稱和網址 git remote add  倉庫名  倉庫地址  新增遠端倉庫,同時設定

Git ResetRevertCheckout的區別

git reset 、 git checkout 和 git revert 是Git中常用命令。經常傻傻分不清他們之間的區別。最近工作不忙,抽出時間參考了其他文件,對其總結了下。提交層面的操作傳給 git reset 和 git checkout 的引數決定了它們的作用域。如果其後引數不是 filename,

Git知識總覽(五) Git的mergerebasecherry-pick以及互動式rebase

上篇部落格聊了《》本篇部落格我們就以Learning Git中的關卡進行展開。下方列舉了LearningGit中的 merge、rebase、reset、revert、cherry-pick 以及互動式rebase相關關卡的操作以及對應的解析。後邊在聊互動式rebase操作是,不單單給出了LearningGi

Git的mergerebasecherry-pickrevert等圖形理解

1.在merge與rebase    在master分支下    merge命令:git merge dev    rebase命令:git rebase --onto master master dev (git rebase master dev)    圖形效果

Git倉庫恢復已刪除的分支檔案或丟失的commit

在使用Git的過程中,有時可能會有一些誤操作 比如:執行checkout -f 或 reset -hard 或 branch -d刪除一個分支 結果造成本地(遠端)的分支或某些commit丟失 可以通過reflog來進行恢復,前提是丟失的分支或commit資訊沒有被git gc清除 一般情況下,gc對那些

解決SVN誤操作--使用Git倉庫恢復已刪除的分支檔案或丟失的操作

timtiandeiMac:WelfareManager timtian$ git branch recover_branch[tzfhead] 99fbfd5 fatal: 'recover_branch[tzfhead]' is not a valid

Git Gui for Windows的建庫克隆(clone)上傳(push下載(pull合併

  關於linux上建庫等操作請看文章: ——————————————————————————————————————————————     本教程將講述:gitk的Git Gui的部分常用功能和使用方法,包括:建庫、克隆(clone)、上傳(push)、下載(pull - fetch)、合併(pu

Git pullpush 免賬號密碼輸入

先進入你的系統使用者目錄,例如 C:\users\Administrator 建立檔案,命名為 _netrc,並編輯 machine {git account name}.github.co

eclipse匯入遠端git程式碼及(pushpull及maven工程匯入)

1、下載eclipse,安裝(需要安裝jdk,對jdk設定) 打包好egit外掛的eclipse下載地址:http://pan.baidu.com/s/1i3feiCd(下載此版本eclipse則不需要第2步的安裝外掛了) 2、安裝git外掛egit:(方法自

(轉)SQL的循環for循環遊標

from clas copy itl let alt 執行 循環 int 我們使用SQL語句處理數據時,可能會碰到一些需要循環遍歷某個表並對其進行相應的操作(添加、修改、刪除),這時我們就需要用到咱們在編程中常常用的for或foreach,但是在SQL中寫循環往往顯得那麽吃

C語言字符字符串字符數組

文件 getc order 知識點 技術 ima 數組 c語言 align char a = ‘h‘; memory h char a[] = "Hello"; memory H e l l o ‘\0‘ stri

c#如何獲取本機MAC地址IP地址硬盤IDCPU序列號等系統信息

finall ipaddress reac 地址 computer mod urn aca rop public class Computer { public static string CpuID; //1.cpu序列號 pub

C#的方法傳參與switchif結構(4)

判斷 1.2 菱形 條件表達式 執行 代碼 輸出 分類 簡易 一、方法傳參的2種方式    1、按值傳遞       傳遞的是值的副本,值會更改但未保留,值最終並未更改     2、按引用傳遞(形參用ref關鍵字修飾)【P86頁】 傳遞的是地址,值會更改且保留,值最終更改

關於Unitystretch的分開使用預制體Scroll View的UI節點

pre 左右 類型 ane 不變 rac tor pos initial 一、上次講的菊花的四個花瓣,只講了四個花瓣和在一起的時候的作用,現在是分開的菊花的四個花瓣的作用 1.創建一個Canvas2.對Canvas進行初始化3.創建一個Image的UI節點作為Canvas的

sql server的開窗函數over視圖事物

sel 開啟 row 分數 over 兩個 color span art 一、開窗函數over的作用有兩個: 1、排序order by,row_number,翻頁 2、劃區partition by,結合聚合函數針對某部分數據進行匯總 翻頁的sql server 語句: s

關於在ES6的 Object.defineProperty(objpropdescription)的相關參數問題

num des 取值 tab function 參考 content fin img 對象是由多個名/值對組成的無序的集合。對象中每個屬性對應任意類型的值。 定義對象可以使用構造函數或字面量的形式: var obj = new Object; //obj = {} obj