Week 6

Day 4 - "Help I'm no longer up to date?"

Pulling changes, not teeth

With a remote tracking branch, we can pull in the changes from the remote repository. In our case, the remote repository is in the coderepo directory that we created at the start of the book. In the coderepo-cl directory we have a separate, self-contained copy of the repository. At the moment, because no changes have occurred to either, they contain exactly the same data.

By running the git remote tool again, we can see which branches are set up locally to track their remote counterparts in origin.
john@satsuki:~/coderepo-cl$ git remote show origin
* remote origin
Fetch URL: /home/john/coderepo
Push URL: /home/john/coderepo
HEAD branch: master
Remote branches:
master tracked
wonderful tracked
zaney tracked
Local branches configured for 'git pull':
master merges with remote master
wonderful merges with remote wonderful
Local refs configured for 'git push':
master pushes to master (up to date)
wonderful pushes to wonderful (up to date)
john@satsuki:~/coderepo-cl$

What we are going to do now is make some changes to our original repository and see how we can view those changes and indeed pull them into our current local working repository, or clone. Note that in the following code examples we have switched back to working on our original repository, in the coderepo folder.
john@satsuki:~/coderepo-cl$ cd ../coderepo
john@satsuki:~/coderepo$ git branch
* master
wonderful
zaney
john@satsuki:~/coderepo$ git checkout wonderful
Switched to branch 'wonderful'
john@satsuki:~/coderepo$ git merge master
Updating cfbecab..37950f8
Fast-forward
another_file | 1 +
my_third_committed_file | 1 -
newfile1 | 3 ++-
newfile2 | 2 +-
4 files changed, 4 insertions(+), 3 deletions(-)
delete mode 100644 my_third_committed_file
john@satsuki:~/coderepo$ echo "These changes are in the origin" >> newfile3
john@satsuki:~/coderepo$ git add newfile3
john@satsuki:~/coderepo$ git commit -a -m 'Added a new file'
[wonderful 1c3206a] Added a new file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 newfile3
john@satsuki:~/coderepo$

As you can see, we have switched back to working on our old origin and have bought wonderful to be in line with master and have added a new file and committed the changes. Now let us go back to our clone and see if we can see those changes.
john@satsuki:~/coderepo$ cd ../coderepo-cl/
john@satsuki:~/coderepo-cl$ git diff wonderful origin/wonderful
john@satsuki:~/coderepo-cl$

That seems a little odd at first glance. We have tried to view a diff between our local wonderful branch and the remote origin/wonderful branch. Interestingly, Git is telling us that there is no difference. Hang on though, we just made some changes above.

Remember before we discussed the fact that our clone was disconnected from the origin copy? What this means is that unless we ask Git, it won't update details about what is present in the remote copy. Once this is up to date, we can pull the changes into our local copy of the remote branch? Interestingly there are actually two ways to do this.

The first method is by running a git pull command. If you remember the output from the git remote show origin, you may remember that it showed that there was a local branch configured for 'git pull' and the details it gave for the wonderful branch were as follows; wonderful merges with remote wonderful. What this means, is that when we run a git pull from inside the wonderful branch, Git will automatically contact the remote repository, update the list of changes and merge them into the local branch if possible.

We mentioned that there were two methods to perform the procedure. Whilst the first one is to use git pull, the second one actually achieves an almost identical result, but by running two commands instead of one. In point of fact, these two commands are executed by the git pull command. Running them as two separate commands allows us to understand a little more about what is happening. Let us run the command and then explain what we have done.
john@satsuki:~/coderepo-cl$ git fetch origin
remote: Counting objects: 4, done.
remote: Compressing objects: 100remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100From /home/john/coderepo
cfbecab..1c3206a wonderful -> origin/wonderful
john@satsuki:~/coderepo-cl$ git diff wonderful origin/wonderful
diff --git a/another_file b/another_file
index dba885d..b3a5cc5 100644
--- a/another_file
+++ b/another_file
@@ -1 +1,2 @@
New stuff
+Number strings rule 1234
...
...
...

john@satsuki:~/coderepo-cl$ git merge origin/wonderful
Updating cfbecab..1c3206a
Fast-forward
another_file | 1 +
my_third_committed_file | 1 -
newfile1 | 3 ++-
newfile2 | 2 +-
newfile3 | 1 +
5 files changed, 5 insertions(+), 3 deletions(-)
delete mode 100644 my_third_committed_file
create mode 100644 newfile3
john@satsuki:~/coderepo-cl$

As you can see, the two commands that we used were git fetch and the more familiar git merge. The git fetch command literally visits the remote repository, in our case we asked for origin, and finds which objects are new and do not exist in our local clone. These are then copied to the local clone, and the HEADs of the various remote branches are updated. We now rerun our git diff, only this time, we see a great many more changes than before. Finally we initiate a git merge to pull the changes from the now up to date remote branch to our local one.

We could have used the git pull command here, and if we had supplied no parameters to it, it would have achieved the exact same outcome. The only difference would have been running one command instead of two. Please note, that there are occasions when you would require one over the other, but as an introduction, this should be sufficient.

Previous Day

Next Day

 
   
home | download | read now | source | feedback | legal stuff