- Git Version Control Cookbook
- Aske Olsson Rasmus Voss
- 894字
- 2021-12-08 12:33:52
Branches with remotes
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.
Getting ready
- We will start by checking out a local branch that tracks a remote branch:
$ 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'
- The previous command creates and checks out the
remoteBugFix
branch that will track theorigin/stable-3.2
branch. So, for instance, executinggit status
will automatically show how different your branch is fromorigin/stable-3.2
, and it will also show whether your branch'sHEAD
can be fast forwarded to theHEAD
of the remote branch or not. - To provide an example of how the previous step works, we need to do some manual work that will simulate this situation. First, we find a commit:
$ 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
- The command will list the last 10 commits on the
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
- This will reset the
remoteBugFix
branch to the2e0d178
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.
How to do it…
Here, we will try a few commands that assist you when you have a remote tracking branch:
- Start by executing
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 usegit pull
to update your local branch, which we will try in the next example. Now, we will just perform the merge:Tip
The
git pull
command is just agit fetch
command and then agit merge
command with the remote tracking branch.$ 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
- From the output, you can see it is a fast-forward merge, as Git predicted in the output of
git status
.
There's more...
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:
- Start by creating a local branch at the
2e0d17
commit:$ git checkout -b remoteBugFix2 2e0d17 Switched to a new branch 'remoteBugFix2'
- The
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 thegit branch
command:$ git branch --set-upstream-to origin/stable-3.2 Branch remoteBugFix2 set up to track remote branch stable-3.2 from origin.
- As you can see from the Git output, we are now tracking the
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
- You can see from the Git output that you are nine commits ahead, and you can use
git pull
to update the branch. Remember that agit pull
command is just agit fetch
command, and then agit 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.
- From the output, you can see the branch has been fast forwarded to the
f839d383e6fbbda26729db7fd57fc917fa47db44
commit hash, which is equivalent toorigin/stable-3.2
. You can verify this withgit log
:$ git log -1 origin/stable-3.2 --format=format:%H f839d383e6fbbda26729db7fd57fc917fa47db44
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- C/C++算法從菜鳥到達(dá)人
- Xcode 7 Essentials(Second Edition)
- TestNG Beginner's Guide
- Building Cross-Platform Desktop Applications with Electron
- 用Flutter極速構(gòu)建原生應(yīng)用
- 劍指大數(shù)據(jù):企業(yè)級數(shù)據(jù)倉庫項目實戰(zhàn)(在線教育版)
- Solr Cookbook(Third Edition)
- Mastering Python Design Patterns
- C編程技巧:117個問題解決方案示例
- Java Web應(yīng)用開發(fā)給力起飛
- 零基礎(chǔ)輕松學(xué)C++:青少年趣味編程(全彩版)
- Python大規(guī)模機器學(xué)習(xí)
- 軟硬件綜合系統(tǒng)軟件需求建模及可靠性綜合試驗、分析、評價技術(shù)
- Java EE 7 Development with WildFly