How can I check the HEAD version of my remote / tracking branch

In git, how can I check the HEAD version of my remote / tracking branch? Basically, I want to do "svn checkout" in git.

I think the closest I find is “git fetch”, but from the man page I don’t know how to check 1 specific file using this?

+7
git
source share
2 answers

First, a note: a remote repository can have more than one branch; also HEAD means (for local development and local branches) the currently verified branch, and for the remote branch of remote tracking (symbolic link) remote/HEAD means the default branch on the remote computer. There is no such thing as a “HEAD OF A BRANCH”; HEAD is a pointer to a branch (or sometimes a commit).

Secondly, Git works (as Novelocrat wrote ) at the whole tree level (all files in the repository). In Git, you check the branch (although you can also check the version of the file from some branch to the working directory, it works less with commkon).


In Git, you can only create a new commit on top of some local branch. You cannot create commits directly on remote tracking branches, as they are designed to track the branches of a remote repository (and you will lose your work when retrieving).

Therefore, a simple " git checkout origin git checkout origin/master " (assuming the remote call is called origin ), which is a shortcut to " git checkout origin/HEAD ", which usually " git checkout origin/master " will check the status of remote tracking branch to an unnamed branch, the so-called remote head. This is a good solution if you want to see / view the status of remote tracking.

If you use " git checkout --track origin/master ", you will create a local master branch that is designed to track (track) the remote tracking of the origin/master branch . Note that git -clone automatically sets such a local branch (usually master ) for the default branch ( origin/HEAD ) of the remote origin (usually origin/master ), so you don't have to do this. Then a simple " git pull ", when the master branch will be extracted (if necessary), and try to combine your local changes with the changes in the corresponding branch in the remote repository. If there were no changes in the remote control, this is an “updated” state and your local branch will not change; if there were no changes in your local branch, but they were deleted, then the local branch simply switched to the state of the remote monitoring branch, which is called fast forward. You can think of git pull here as a very, very crude equivalent of svn update .

You can also force your local branch to rewind the remote tracking branch using " git reset --hard origin ". Please note that --hard means here to force overwriting the working directory, so any unsuccessful changes will be lost! Use with caution.

Finally, if you want to check one file from a remote tracking branch (that is, have a version of the file in the working directory, as in the remote tracking branch), you can simply use the “pathspec” form of git -checkout, namely “ git checkout origin -- file ". If you want to see what the file looks like in the remote tracking branch, use" git show origin:path/to/file ".

NTN

+15
source share

Git has no idea of ​​getting a single file. It always moves through the network through the network.

After you have made the git clone some remote repository, you can git checkout remotename/branch to get the latest files on this branch in your working copy. If you want to change them, you must git checkout -b <yourbranchname> remotename/branch to run the local branch. It can be better done as

 git branch --track mybranchname remotename/branchname git checkout mybranchname 

That your local branch "tracked" a remote branch and by default pressed and pulled from this branch.

+4
source share

All Articles