Git Origin Who Uploaded Branch?
git fetch
The git fetch
control downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you practice when you lot desire to run across what everybody else has been working on. It's similar to svn update
in that it lets you see how the cardinal history has progressed, but information technology doesn't force you to really merge the changes into your repository. Git isolates fetched content from existing local content; it has admittedly no effect on your local evolution work. Fetched content has to be explicitly checked out using the git checkout
command. This makes fetching a rubber way to review commits before integrating them with your local repository.
When downloading content from a remote repo, git pull
and git fetch
commands are available to accomplish the task. You tin can consider git fetch
the 'safe' version of the two commands. It will download the remote content just not update your local repo'south working state, leaving your current work intact. git pull
is the more aggressive alternative; it will download the remote content for the active local branch and immediately execute git merge
to create a merge commit for the new remote content. If you accept pending changes in progress this will crusade conflicts and kick-off the merge conflict resolution flow.
How git fetch works with remote branches
To better understand how git fetch
works permit us hash out how Git organizes and stores commits. Behind the scenes, in the repository's ./.git/objects
directory, Git stores all commits, local and remote. Git keeps remote and local branch commits distinctly separate through the employ of co-operative refs. The refs for local branches are stored in the ./.git/refs/heads/
. Executing the git branch
command volition output a list of the local branch refs. The following is an example of git branch
output with some demo branch names.
git branch
primary
feature1
debug2
Examining the contents of the /.git/refs/heads/
directory would reveal similar output.
ls ./.git/refs/heads/
master
feature1
debug2
Remote branches are just like local branches, except they map to commits from somebody else'south repository. Remote branches are prefixed past the remote they belong to and then that you lot don't mix them up with local branches. Like local branches, Git besides has refs for remote branches. Remote branch refs live in the ./.git/refs/remotes/
directory. The next example code snippet shows the branches you might see subsequently fetching a remote repo conveniently named remote-repo:
git branch -r
# origin/principal
# origin/feature1
# origin/debug2
# remote-repo/main
# remote-repo/other-feature
This output displays the local branches we had previously examined but at present displays them prefixed with origin/
. Additionally, nosotros now see the remote branches prefixed with remote-repo
. You can check out a remote branch just like a local one, merely this puts you in a detached Caput
state (only like checking out an old commit). You can think of them as read-only branches. To view your remote branches, simply pass the -r
flag to the git co-operative
command.
You can inspect remote branches with the usual git checkout
and git log
commands. If y'all approve the changes a remote branch contains, you can merge information technology into a local branch with a normal git merge
. And then, unlike SVN, synchronizing your local repository with a remote repository is actually a 2-stride procedure: fetch, then merge. The git pull
command is a convenient shortcut for this process.
Git fetch commands and options
Fetch all of the branches from the repository. This as well downloads all of the required commits and files from the other repository.
git fetch <remote> <branch>
Same every bit the above command, but only fetch the specified co-operative.
A ability motility which fetches all registered remotes and their branches:
The --dry-run
selection volition perform a demo run of the control. It will output examples of deportment information technology will accept during the fetch but not utilize them.
Git fetch examples
git fetch a remote branch
The following example will demonstrate how to fetch a remote branch and update your local working country to the remote contents. In this example, let united states of america presume in that location is a central repo origin from which the local repository has been cloned from using the git clone
command. Let us also assume an additional remote repository named coworkers_repo that contains a feature_branch which we will configure and fetch. With these assumptions prepare let us continue the case.
Firstly we will need to configure the remote repo using the git remote
command.
git remote add coworkers_repo git@bitbucket.org:coworker/coworkers_repo.git
Here nosotros take created a reference to the coworker's repo using the repo URL. We will at present pass that remote name to git fetch
to download the contents.
git fetch coworkers_repo coworkers/feature_branch
fetching coworkers/feature_branch
We now locally have the contents of coworkers/feature_branch nosotros will need the integrate this into our local working copy. We brainstorm this process past using the git checkout
command to checkout the newly downloaded remote co-operative.
git checkout coworkers/feature_branch
Note: checking out coworkers/feature_branch'.You are in 'detached Caput' country. You can look around, make experimental
changes and commit them, and you tin can discard any commits you make in this
state without impacting any branches by performing another checkout.
If y'all want to create a new co-operative to retain commits y'all create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
The output from this checkout operation indicates that we are in a detached Caput
state. This is expected and means that our HEAD
ref is pointing to a ref that is not in sequence with our local history. Being that Head
is pointed at the coworkers/feature_branch ref, we can create a new local branch from that ref. The 'detached Head
' output shows us how to practice this using the git checkout
command:
git checkout -b local_feature_branch
Here we have created a new local branch named local_feature_branch. This puts updates Head
to indicate at the latest remote content and nosotros can keep development on information technology from this point.
Synchronize origin with git fetch
The following example walks through the typical workflow for synchronizing your local repository with the central repository'southward main branch.
This will brandish the branches that were downloaded:
a1e8fb5..45e66a4 primary -> origin/main
a1e8fb5..9e8ab1c develop -> origin/develop
* [new branch] some-characteristic -> origin/some-feature
The commits from these new remote branches are shown equally squares instead of circles in the diagram below. Every bit you can run across, git fetch
gives you admission to the unabridged branch structure of another repository.
To encounter what commits have been added to the upstream main, you lot can run a git log
using origin/chief as a filter:
git log --oneline chief..origin/main
To approve the changes and merge them into your local primary branch apply the post-obit commands:
git checkout main
git log origin/master
Then we can use git merge origin/main
:
The origin/main and main branches now point to the aforementioned commit, and you are synchronized with the upstream developments.
Git fetch summary
In review, git fetch
is a chief command used to download contents from a remote repository. git fetch
is used in conjunction with git remote
, git branch
, git checkout
, and git reset
to update a local repository to the country of a remote. The git fetch
command is a critical piece of collaborative git work flows. git fetch
has similar beliefs to git pull
, however,git fetch
can be considered a safer, nondestructive version.
Source: https://www.atlassian.com/git/tutorials/syncing/git-fetch
Posted by: juarezfinstiout.blogspot.com
0 Response to "Git Origin Who Uploaded Branch?"
Post a Comment