Skip to content
Created: November 18, 2017

Git remote warehouse management ​

bash
git remote -v                    # 查看远程服务器地址和仓库名称  
git remote show origin           # 查看远程服务器仓库状态  
git remote add origin git@ github:glitz/glitz_site.git         # 添加远程仓库地址  
git remote set-url origin git@ github.com:glitz/glitz_site.git # 设置远程仓库地址(用于修改远程仓库地址)  
git remote rm <repository>       # 删除远程仓库

Create a remote warehouse ​

bash
git clone --bare glitz_site glitz_site.git  # 用带版本的项目创建纯版本仓库  
scp -r my_project.git git@ git.csdn.net:~      # 将纯仓库上传到服务器上  
mkdir glitz_site.git && cd glitz_site.git && git --bare init # 在服务器创建纯仓库  
git remote add origin git@ github.com:glitz/glitz_site.git    # 设置远程仓库地址  
git push -u origin master                                      # 客户端首次提交  
git push -u origin develop  # 首次将本地develop分支提交到远程develop分支,并且track  
git remote set-head origin master   # 设置远程仓库的HEAD指向master分支

You can also command to set up tracking remote libraries and local libraries ​

bash
git branch --set-upstream master origin/master  
git branch --set-upstream develop origin/develop

The first code submission to the warehouse ​

  • Create a README.md .gitignore locally
bash
echo '.idea' >> .gitignore
echo 'welcome' >> README.md
  • Initialize warehouse
bash
git init
  • Submit and associate the warehouse
bash
git add .
git commit -m ''
git remote add origin 地址
git remote -v 查看所有关联
git remote rm origin 删除
  • Push to remote warehouse
bash
git push origin master(可以推送其他分支) -u(upstream 下次提交不必再输入origin master)
  • Merge code from two different repositories
bash
git pull origin master --allow-unrelated-histories

Resolve git conflicts ​

If some configuration files in the system are modified on the server, and then some new configuration items are added during subsequent development, When publishing files, code conflicts will occur:
error: Your local changes to the following files would be overwritten by merge: protected/config/main.php Please, commit your changes or stash them before you can merge. If you want to keep the changes made on the production server and just incorporate the new configuration items, do the following:

bash
git stash       # 存储工作区
git stash list  # 存储的工作区列表
git pull
git stash pop   # 读取暂存区内容并从暂存区删除 相当于apply + drop
git stash apply # 用一下暂存区的内容
git stash drop  # 删除暂存区内容

You can then use git diff -w + file name to confirm that the code is automatically merged.

Conversely, if you want to completely overwrite the local working version with files in the code base. The method is as follows:

bash
git reset --hard
git pull
其中git reset是针对版本,如果想针对文件回退本地修改,使用
git checkout HEAD file/to/restore

github encountered a problem ​

Submitting the code failed and reported an error

bash
fatal: 无法访问 'https://github.com/happy760690/blog.git/':LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

Then I changed sourcetree and git command line to submit, but it couldn't be submitted. Then I searched online based on this error message and found that it was due to a problem with Git's HTTP proxy. Git supports three protocols: git://, ssh:// and http://,本来push的时候应该走ssh隧道的,但是因为设置了http代理,所以就走了http的代理,于是就提交不了了。
OK, now that we have found the reason, we can cancel the http proxy.

Solution:

bash
git config --global --unset http.proxy

Git branch - remote branch ​

Remote reference is a reference (pointer) to the remote warehouse, including branches, tags, etc.

The remote branch is essentially a pointer, pointing to the remote address

View remote reference lists and information ​

sh
git ls-remote <remote> # 远程引用的完整列表
git remote show <remote> # 远程分支的更多信息

The above two lines of commands are relatively rarely used, and a more common approach is to use remote tracking branches.

Remote tracking branches ​

A remote-tracking branch is a reference to the state of a remote branch .They are local references that you cannot move.Once you have network communications, Git will move them for you to Accurately reflect the status of the remote warehouse .please send them See as a bookmark , which reminds you that the location of the branch in the remote repository is the last time you connected to them.

them to <remote>/<branch> Named in the form of .For example, if you want View last time with remote repository origin When communicating master branch status , you can view origin/master branch.You work with a colleague on an issue and they push a iss53 branch, you may have your own local iss53 branch, however the branch on the server will end up with origin/iss53 to express.

This might be a little hard to understand, so let's look at an example.Suppose there is a person in your network git.ourcompany.com Git server.If you clone from here, Git's clone The command will automatically name it for you origin , pull all its data, and create a pointer to it master branch pointer, and Locally name it origin/master .Git will also give you a link to origin master The branch is local to the same place master Branch so you have a base to work from.

Note: The repository cloned from the remote has a remote tracking branch called origin/master and a local master branch

Note: "origin" has no special meaning. The remote repository name "origin", like the branch name "master", does not have any special meaning in Git.While "master" is the default starting branch name when you run git init, simply because of its widespread use, "origin" is the default remote repository name when you run git clone.If you run git clone -o booyah, your default remote branch name will be booyah/master.

The server and local repository after cloning.

Figure 1. Server and local warehouse after cloning ▲

If you do some work on your local master branch and someone else pushes a commit to git.ourcompany.com and updates its master branch during the same period of time, your commit history has gone in a different direction.Even so, as long as you remain disconnected from the origin server (and pull data), your origin/master pointer will not move.

Local and remote work can be forked.

Figure 2. Local and remote work can be bifurcated ▲

If you want to synchronize data with a given remote repository, run the git fetch <remote> command (in this case git fetch origin).This command finds which server "origin" is (in this case, it is git.ourcompany.com), fetches data from it that is not available locally, and updates the local database, moving the origin/master pointer to the updated location.

 updates your remote repository reference.

Figure 3. git fetch updates your remote tracking branch ▲

Note: The local master branch may be forked from the remote tracking branch origin/master

To demonstrate the situation of having multiple remote repositories and remote branches, we assume that you have another internal Git server that only serves one of your agile development teams.This server is located at git.team1.ourcompany.com .You can run git remote add The command adds a new remote warehouse reference to the current project. We will use this command in Git basics Detailed description in .Set this remote repository named teamone , as an abbreviation for the full URL 。 The remote warehouse name is essentially the abbreviation of the remote URL

Add another remote repository.

Figure 4. Add another remote warehouse ▲

Now, you can run git fetch teamone to fetch the data that the remote repository teamone has but not locally.Because the existing data on that server is a subset of the origin server, Git will not fetch the data but will set the remote tracking branch teamone/master to point to the master branch of teamone.

Remote tracking branch .

Figure 5. Remote tracking branch teamone/master ▲

Push ​

When you want to share a branch publicly, you need to push it to a remote repository with write permissions.Local branches are not automatically synchronized with the remote repository - you must explicitly push the branch you want to share.In this way, you can put the content you don't want to share to the private branch, and push the content that needs to be collaborated with others to the public branch.

If you want to work with others on a branch called serverfix, you can push it just like you pushed the first branch.Run git push <remote> <branch>:

sh
$ git push origin serverfix
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/schacon/simplegit
 * [new branch]      serverfix -> serverfix

Some work is simplified here. Git automatically serverfix The branch name expands to refs/heads/serverfix:refs/heads/serverfix , That means, "Push the local serverfix Branch to update the remote repository serverfix branch.” We will learn in detail Git internals of refs/heads/ part, but you can put it aside for now.You can also run git push origin serverfix:serverfix , which will do the same thing - that is, " push local serverfix branch as a remote repository serverfix branch ” You can use this format to push a local branch to a remote branch with a different name.

Rename the branch name on the remote repository ​

If you don’t want the branch on the remote repository to be called serverfix, you can run git push origin serverfix:awesomebranch to push the local serverfix branch to the awesomebranch branch on the remote repository.

NoteHow to avoid entering your password every time If you are using an HTTPS URL to push, the Git server will ask for a username and password.By default it prompts in the terminal whether the server allows you to push.If you don't want to enter your username and password every time you push, you can set up a "credential cache".The easiest way is to keep it in memory for a few minutes, you can simply run git config --global credential.helper cache to set it up.To learn more about the different authentication caching options available, see Credential Storage.

The next time other collaborators scrape data from the server, they will locally generate a remote branch origin/serverfix with a reference to the server's serverfix branch:

sh
$ git fetch origin
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/schacon/simplegit
 * [new branch]      serverfix    -> origin/serverfix

One thing to pay special attention to is that when a new remote tracking branch is fetched, an editable copy (copy) will not be automatically generated locally.In other words, in this case, there will not be a new serverfix branch - just an unmodifiable origin/serverfix pointer.

You can run git merge origin/serverfix to merge this work into the current branch.If you want to work on your own serverfix branch, you can base it on the remote tracking branch:

sh
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

This will give you a local branch to work from, starting at origin/serverfix.

Track branches ​

Checking out a local branch from a remote tracking branch automatically creates what is called a "tracking branch" (the branch it tracks is called the "upstream branch").A tracking branch is a local branch that has a direct relationship to a remote branch.If you type git pull on a tracking branch, Git can automatically identify which server to fetch and merge into which branch.

When cloning a repository, it usually automatically creates a master branch tracking origin/master.However, you can set up another tracking branch if you wish, either a tracking branch on another remote repository, or not track the master branch.The simplest example is to run git checkout -b <branch> <remote>/<branch> as we saw earlier.This is a very common operation so Git provides the --track shortcut:

sh
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

Since this operation is so common, there is a shortcut for the shortcut itself.If the branch you're trying to check out (a) doesn't exist and (b) happens to have only a remote branch with a matching name, Git will create a tracking branch for you:

sh
$ git checkout serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

If you want to set the local branch to a different name than the remote branch, you can easily add a local branch with a different name using the previous command:

sh
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin.
Switched to a new branch 'sf'

Now, the local branch sf will automatically pull from origin/serverfix.

To set an existing local branch to track a remote branch that has just been pulled, or to modify the upstream branch that is being tracked, you can set it explicitly at any time by running git branch with the -u or --set-upstream-to option.

sh
$ git branch -u origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
NoteUpstream shortcut When a tracking branch is set up, its upstream branch can be referenced by the abbreviation @{upstream} or @{u}.So when you are on the master branch and it is tracking origin/master, you can use git merge @{u} instead of git merge origin/master if you like.

View tracking branches ​

If you want to see all tracking branches set up, you can use the -vv option of git branch.This will list all local branches and include more information, such as which remote branch each branch is tracking and whether the local branch is ahead of, behind, or both.

sh
$ git branch -vv
  iss53     7e424c3 [origin/iss53: ahead 2] forgot the brackets
  master    1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it
  testing   5ea463a trying something new

Here you can see that the iss53 branch is tracking origin/iss53 and "ahead" is 2, which means that there are two local commits that have not been pushed to the server.You can also see that the master branch is tracking the origin/master branch and is up to date.Next, you can see that the serverfix branch is tracking the server-fix-good branch on the teamone server and is leading by 3 and lagging behind by 1, which means that there is one submission on the server that has not been merged yet, and there are three local submissions that have not been pushed yet.Finally see that the testing branch does not track any remote branches.

It's important to note that the values for these numbers come from the last time you fetched data from each server.This command does not connect to the server, it only tells you about locally cached server data.If you want to count the latest leading and lagging numbers, you need to crawl all remote repositories before running this command.This can be done like this:

sh
$ git fetch --all; git branch -vv

pull ​

When the git fetch command fetches data that is not available locally from the server, it does not modify the contents of the working directory.It just takes the data and lets you merge it yourself.However, there is a command called git pull which in most cases means a git fetch followed by a git merge command.If you have a tracking branch set up as demonstrated in the previous chapter, whether it was set up explicitly or created for you via the clone or checkout command, git pull will look for the server and branch that the current branch is tracking, grab the data from the server and try to merge into that remote branch.

Because the magic of git pull is often confusing it is often better to use the fetch and merge commands explicitly separately.

Delete remote branch ​

Assume that you have done all your work via the remote branch - that is, you and your collaborators have completed a feature and merged it into the remote repository's master branch (or any other stable code branch).You can delete a remote branch by running the git push command with the --delete option.If you want to remove the serverfix branch from the server, run the following command:

sh
$ git push origin --delete serverfix
To https://github.com/schacon/simplegit
 - [deleted]         serverfix

Basically what this command does is remove this pointer from the server.Git servers usually retain data for a while until garbage collection runs, so if you accidentally delete it, it's usually easy to recover.