At some point, it is very likely that you have cloned somebody's repository. This means you have an associated remote. The remote is usually called origin because it is where the source originated from.
While working with Git and remotes, you will get some benefits from Git.
We can start with git status
and see what we get while working with the remote.
$ git checkout -b remoteBugFix --track origin/stable-3.2 Branch remoteBugFix set up to track remote branch stable-3.2 from origin. Switched to a new branch 'remoteBugFix'
remoteBugFix
branch that will track the origin/stable-3.2
branch. So, for instance, executing git status
will automatically show how different your branch is from origin/stable-3.2
, and it will also show whether your branch's HEAD
can be fast forwarded to the HEAD
of the remote branch or not. $ git log -10 origin/stable-3.2 --oneline f839d38 Prepare post 3.2.0 builds 699900c JGit v3.2.0.201312181205-r 0ff691c Revert "Fix for core.autocrlf=input resulting in modified fil 1def0a1 Fix for core.autocrlf=input resulting in modified file and un 0ce61ca Canonicalize worktree path in BaseRepositoryBuilder if set vi be7942f Add missing @since tags for new public methods in Config ea04d23 Don't use API exception in RebaseTodoLine 3a063a0 Merge "Fix aborting rebase with detached head" into stable-3. e90438c Fix aborting rebase with detached head 2e0d178 Add recursive variant of Config.getNames() methods
stable-3.2
branch from the remote origin. The --oneline
option will show the abbreviated commit hash and the commit subject. For this recipe, we will be using the following commit:$ git reset --hard 2e0d178 HEAD is now at 2e0d178 Add recursive variant of Config.getNames() m
remoteBugFix
branch to the 2e0d178
commit hash. We are now ready to continue using the free benefits of Git when we have a remote tracking branch.We are resetting to a commit that is accessible from the origin/stable-3.2
remote tracking branch; this is done to simulate that we have performed a Git fetch and new commits were downloaded for the origin/stable-3.2
branch.
Here, we will try a few commands that assist you when you have a remote tracking branch:
git status
:$ git status On branch remoteBugFix Your branch is behind 'origin/stable-3.2' by 9 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working directory clean
Git is very descriptive when you have a tracking branch and you use git status
. As you can see from the message, you can use git pull
to update your local branch, which we will try in the next example. Now, we will just perform the merge:
$ git merge origin/stable-3.2 Updating 2e0d178..f839d38 Fast-forward .../org/eclipse/jgit/api/RebaseCommandTest.java | 213 +++++++++++ .../src/org/eclipse/jgit/api/RebaseCommand.java | 31 +-- .../jgit/errors/IllegalTodoFileModification.java | 59 ++++++ .../eclipse/jgit/lib/BaseRepositoryBuilder.java | 2 +- .../src/org/eclipse/jgit/lib/Config.java | 2 +.../src/org/eclipse/jgit/lib/RebaseTodoLine.java | 16 +- 6 files changed, 266 insertions(+), 57 deletions(-) create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/errors/Ille
git status
.You can also add a remote to an existing branch, which is very handy when you realize that you actually wanted a remote tracking branch but forgot to add the tracking information while creating the branch:
2e0d17
commit:$ git checkout -b remoteBugFix2 2e0d17 Switched to a new branch 'remoteBugFix2'
remoteBugFix2
branch is just a local branch at the moment with no tracking information; to set the tracking branch, we need to use --set-upstream-to
or –u
as a flag to the git branch
command:$ git branch --set-upstream-to origin/stable-3.2 Branch remoteBugFix2 set up to track remote branch stable-3.2 from origin.
stable-3.2
branch from the origin:$ git status On branch remoteBugFix2 Your branch is behind 'origin/stable-3.2' by 9 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working directory clean
git pull
to update the branch. Remember that a git pull
command is just a git fetch
command, and then a git merge
command with the upstream branch, which we also call the remote tracking branch:$ git pull remote: Counting objects: 1657, done remote: Finding sources: 100% (102/102) remote: Total 102 (delta 32), reused 98 (delta 32) Receiving objects: 100% (102/102), 65.44 KiB | 0 bytes/s, done. Resolving deltas: 100% (32/32), completed with 19 local objects. From https://git.eclipse.org/r/jgit/jgit 25fe20b..50a830f master -> origin/master First, rewinding head to replay your work on top of it... Fast-forwarded remoteBugFix2 to f839d383e6fbbda26729db7fd57fc917fa47db44.
f839d383e6fbbda26729db7fd57fc917fa47db44
commit hash, which is equivalent to origin/stable-3.2
. You can verify this with git log
:$ git log -1 origin/stable-3.2 --format=format:%H f839d383e6fbbda26729db7fd57fc917fa47db44