Monday, April 27, 2015

Git : How to keep local repository in sync with remote repository?

To keep our  local repository in sync with the remote repository by fetching the updates from the remote repository, run the following from our local repo machine

git checkout master
git fetch origin
git merge origin/master

In Detail
=========
$ git branch -r                                                                                                            
  origin/HEAD -> origin/master
  origin/blue-feature
  origin/master

Here, "origin" refers to the remote central repo

$ git fetch origin
testuser@localhost's password:
remote: Counting objects: 5, done.                                                                                                                                      
remote: Compressing objects: 100% (3/3), done.                                                                                                                          
remote: Total 3 (delta 0), reused 0 (delta 0)                                                                                                                           
Unpacking objects: 100% (3/3), done.
From ssh://localhost/home/testuser/testgit/remote-repo
   854dc70..1d80eb3  master     -> origin/master

git fetch doesn't touch your local working tree at all. So gives you a little breathing space to decide what you want to do next. To actually bring the changes from the remote branch into your working tree, you have to do a git merge.

Before merging the remote repository into our local repository, to see the differences between our local repo and remote repo

$ git diff master origin/master                                                                                            
diff --git a/third.txt b/third.txt
index 6cfb7aa..6b22503 100644
--- a/third.txt
+++ b/third.txt
@@ -1,3 +1,5 @@
-thirs file
+this file
 saturday started work
 sunday let us see
+again sunday
+one more line

Now merge the remote repo "origin/master" into our local repo

$ git merge origin/master                                                                                                  
Updating 0b5c193..1d80eb3
Fast-forward
 third.txt |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

Check if the update from remote repo has reflected in the local repo by checking a file which has the changes. In my case, it was a file by name "third.txt"

$ cat third.txt 
this file
saturday started work
sunday let us see
again sunday
one more line

No comments:

Post a Comment