Git origin and master analysis
First of all, it should be clear that the operation of Git is carried out around three major steps (in fact, almost all SCM are like this)
- Get data from git (GIT clone)
- Change code
- Return changes to git (GIT push)
These three steps involve two repositories, one is remote repository on the remote server, and the other is local repository on your own workspace. among
Steps 1 and 3 involve remote server / remote repository / remote branch,
2. Local repository / local branch is involved. Git clone will copy a copy to your local location according to the remote server / repository / branch you specify. Before git push, you make changes to all files in your local repository. Your changes (local branch) and remote branch are independent (parallel). Gitk shows the local repository.
After the clone is completed, GIT will automatically name the remote warehouse as origin (origin is only equivalent to an alias. Run git remote – V or check. Git / config to see the meaning of origin), Download all the data in it, and establish a pointer to its master branch. We use the form of (remote warehouse name) / (branch name) to represent the remote branch, Therefore, the origin / Master points to a remote branch (from which we clone data to the local), but you cannot change its data locally.
At the same time, GIT will establish a local master branch of your own, which points to the copy you just transferred from the remote server to your local server. As you constantly change the file, GIT add, GIT commit and the master point will move automatically. You can also move the master point through merge (fast forward).
$git branch -a (to show all the branches git knows about)
remotes/origin/HEAD -> origin/master
$git branch -r (to show remote branches git knows about)
origin/HEAD -> origin/master
It can be found that master is a branch in the local repository. Remotes / origin / Master is a branch named master on the remote named origin
$git diff origin/master master （show me the changes between the remote master branch and my master branch).
It should be noted that the directions of remotes / origin / master and origin / master are the same
$git diff origin/master remotes/origin/master
git push origin master
Origin specifies which remote you want to push to
Master is actually a “refspec”. The normal form of “refspec” is “+ < SRC >: < DST >”. Before the colon is the name of the local branch, and after the colon is the name of the branch under the remote repository. Note that if you omit < DST >, GIT thinks you want to push a branch with the same name as the local branch under the remote repository. It sounds a little awkward. Explain how push is used to push the commit pointed to by the local branch to the branch under the remote repository, for example
$git push origin master: Master (find the branch named master in the local repository and use it to update the branch named master under the remote repository. If there is no branch named master under the remote repository, create a new one)
$git push origin master (omitting < DST >, equivalent to “git push origin master: Master”)
$git push origin master: refs / for / mybranch (find the branch named master in the local repository and use it to update the branch named mybranch under the remote repository)
$git push origin head: refs / for / mybranch (head points to the current working branch, and master does not necessarily point to the current working branch, so I think using head is better than Master)
$git push origin: mybranch (find mybranch in the origin repository and delete it. Updating it with an empty one is equivalent to deleting it)