A, git clone
The first step is to clone a repository from a remote host using the git clone command
Git cloneCopy the code
This command generates a directory on the local host with the same name as the repository on the remote host. If you want to specify a different directory name, you can use the directory name as the second parameter to the git clone command.
Git clone $git cloneCopy the code
Second, the git remote
For ease of administration, Git requires that each remote host be assigned a hostname. Git remote is used to manage host names. Git remote add git remote add git remote add git remote add git remote add
Git remote addCopy the code
View all remote hosts
git remote -v
Copy the code
View the specified remote host
Git remote showCopy the code
Renames the specified remote host
Git remote rename < git remote rename >Copy the code
Delete the specified remote host (add the host again after deleting it)
Git remote rmCopy the code
Third, git fetch
Git fetch is used when updates to a remote repository (called a COMMIT in Git terminology) need to be brought back locally.
Retrieves all branches of the remote host
git fetch // Ignore the host name, default originGit fetch < remote hostname >Copy the code
Retrieves the specified branch of the remote host
Git fetch < remote host name >Copy the code
Git fetch: < remote host name > < remote host name >
git fetch // Pull all remote branchesGit fetch < remote host name >// Pull the remote specified branch
Copy the code
Four, git branch
In some cases, Git will automatically establish a tracking relationship between local and remote branches. For example, with Git clone, all local branches have the same name as remote hosts by default. In other words, the local master branch automatically “tracks” the Origin /master branch.
Git also allows you to establish tracing relationships manually.
git branch --set-upstream master origin/next Copy the code
View all local branches
git branch
Copy the code
View all remote branches
git branch -r
Copy the code
View all branches of remote + local
git branch -a
Copy the code
When you clone from the remote repository to the local repository, the branch of the remote repository is automatically pulled down with the origin/ prefix
git branch -a // View all branches of the current local repository
Copy the code
Create a branch test1 locally and git branch -a. The test1 branch is not associated with the remote branch with the origin prefix
At this point, Git push pushes a local branch that is not associated with the remote branch
With git push –set-upstream origin test1, you can see that the origin/test1 branch associated with the remote repository has been created and a test1 branch has been added to the remote repository
At this point, modify the master code in the remote repositoryUse the merge command in the local test1 branch
git merge origin origin/master
git merge origin/master
git merge master
Copy the code
It is found that the latest master is not merged, indicating that it is already the latest, because the origin/master is still the remote branch associated with the local and used for push, and has not been updated. Then perform git fetch
git fetch origin master
git fetch
Copy the code
Fetch the latest remote master to the local origin/master, then check the branch informationLocal origin/master is updated, but local mater is not updated, merge on branch test1
git merge master
Copy the code
Git fetch is updated to origin/ Master.
Execute the following command to see that test1 has finally merged the latest information from the remote master branch
git merge origin origin/master
git merge origin/master
Copy the code
The current version number of the master branch is the last one
Switch to the master branch, execute Git pull, and find that the binding version of the master has changed, and it is the same as origin/master
Five, the git pull
The git pull command fetches updates from a branch on a remote host and merges them with a specified branch on the local host. This is equivalent to git fetch and git merge
Retrieves a branch of the remote host and merges it with a local branch
Git pull < remote host name > < remote branch name >Copy the code
For example: fetch the next branch of origin host, merge with the local master branch, need to write as follows.
git pull origin next:master
Copy the code
If the remote branch is merged with the current branch, the part after the colon can be omitted.
git pull origin next
Copy the code
Git pull can omit the remote branch name if the current branch is traced to the remote branch. (Pull the remote branch corresponding to the current branch)
git pull origin
Copy the code
The above command indicates that the current local branch is automatically merged with the remote-tracking branch of the origin host. If the current branch has only one trace branch, even the remote hostname can be omitted. (Could there be two tracking branches? Is it because there are two warehouse sources remotely?
git pull // The current branch has only one trace branch that is connected to the remote branch, and usually only wants to pull the current branch. Therefore, the normal operation is to pull the remote branch from the current branch and merge it with the current branch.
Copy the code
If the merge requires rebase mode, use the –rebase option.
$git pull --rebaseCopy the code
If the remote host deletes a branch, by default git pull does not delete the local branch while pulling the remote branch. This is to prevent Git pull from inadvertently deleting the local branch because someone else is manipulating the remote host.
However, you can change this behavior by adding the -p parameter to locally delete the remote deleted branch.
git pull -p
Copy the code
Git pull -p deletes only the branches that have been connected to the remote end. Git branch -d deletes only the local branches, but not the branches that have been connected to the remote end
Git pull < branch name > git fetch < branch name > git fetch < branch name
Six, git push
The git push command is used to push updates from a local branch to a remote host. The format is similar to the Git pull command.
Git push < remote hostname > < local branch name >Copy the code
Git pull is < remote >:< local > and Git push is < local >:< remote >.
If the remote branch name is omitted, the local branch is pushed to the remote branch with which it has a “tracing relationship” (usually with the same name). If the remote branch does not exist, it is prompted to establish a tracing relationship with the remote branch, and a corresponding tracing relationship will be created on the remote host.
If the local branch name is omitted, the specified remote branch is deleted, as this is equivalent to pushing an empty local branch to the remote branch.
$git push origin: $git push origin --delete masterCopy the code
Delete the master branch of the Origin host.
If there is a trace between the current and remote branches, both the local and remote branches can be omitted.
git push origin
Copy the code
The above command indicates that the current branch is pushed to the corresponding branch of the Origin host.
If the current branch has only one trace branch, then the host name can be omitted.
git push
Copy the code
If the current branch is traced to multiple hosts, you can specify a default host using the -u option so that git push can be used without any arguments
git push -u origin master
Copy the code
Git push = git push = git push = git push = git push = git push = git push = git push = git push = git push = git push = git push = git push
Git push with no arguments pushes only the current branch by default.
The –all option is also used when all local branches are pushed to the remote host regardless of whether corresponding remote branches exist.
git push --all origin
Copy the code
Push all local branches to the Origin host.
If the version of the remote host is later than that of the local host, Git will report an error when you push the remote host. At this point, if you must push, use the –force option.
git push --force origin
Copy the code
The command above uses the –force option, which overwrites the newer version on the remote host. Avoid using the –force option unless you are absolutely sure you want to do so. Finally, Git push does not push tags unless you use the –tags option.
git push origin --tags
Copy the code
Git fetch pulls the origin/ branch name branch from the remote end, and then the local git chechout < branch name > creates the corresponding branch associated with the remote end, changes the code of the local branch, commits the commit, and the commit hash of the local branch changes. Git pushes the local origin branch to the remote origin branch, and the version of the remote Origin branch is the same as that of the local origin branch
Consider: why create a local origin/ branch name that connects to a remote trace branch? The origin/ branch name branch represents the current version of the remote branch corresponding to the local branch. Git fetch is fetching the remote branch, and only updates the local remote branch. If there is no such branch, where will the code that is pulled and does not want to merge go? Git commit commits only the local branch, the local origin branch will not change, so that the push time will be linked to the corresponding Origin branch, push the corresponding Origin branch to the remote end, and update the latest version of the local Origin branch. Push is the local Origin branch, pull is the remote Origin branch, the local branch is just for our development.