Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing a local Git repository with a remote one

Tags:

git

I want to synchronize my local repository with a remote one so that my local repository becomes a 100% copy of the remote one - meaning that if certain files differ in these repositories, we override the local ones with the remote ones, and if there are files in local repositories that do not exist in the remote, the local files get removed.

Is there any way to achieve that other than by doing a fresh clone of remote repository?

Similar question as Sync local git repo with remote in one shot discarding local changes/commits.

like image 763
Axl Avatar asked Jun 16 '11 14:06

Axl


People also ask

How do I link a local repository to a remote one?

Explanation: navigate to your local repo. tell git where the remote repo is located. upload/push your local branches to the remote repo.


4 Answers

git fetch --prune

-p, --prune
After fetching, remove any remote-tracking branches which no longer exist on the remote. prune options

like image 163
jobwat Avatar answered Oct 10 '22 03:10

jobwat


These steps will do it:

git reset --hard HEAD
git clean -f -x -d -n

then without -n

This will take care of all local changes. Now the commits...

git status

and note the line such as:

Your branch is ahead of 'xxxx' by N commits.

Take a note of number 'N' now:

git reset --hard HEAD~N
git pull

and finally:

git status

should show nothing to add/commit. All clean.

However, a fresh clone can do the same (but is much slow).

===Updated===

As my git knowledge slightly improved over the the time, I have come up with yet another simpler way to do the same. Here is how (#with explanation). While in your working branch:

git fetch # This updates 'remote' portion of local repo. 
git reset --hard origin/<your-working-branch>
# this will sync your local copy with remote content, discarding any committed
# or uncommitted changes.

Although your local commits and changes will disappear from sight after this, it is possible to recover committed changes, if necessary.

like image 22
FractalSpace Avatar answered Oct 10 '22 05:10

FractalSpace


You want to do

git fetch --prune origin
git reset --hard origin/master
git clean -f -d

This makes your local repo exactly like your remote repo.

Remember to replace origin and master with the remote and branch that you want to synchronize with.

like image 22
aandis Avatar answered Oct 10 '22 05:10

aandis


You need to understand that a Git repository is not just a tree of directories and files, but also stores a history of those trees - which might contain branches and merges.

When fetching from a repository, you will copy all or some of the branches there to your repository. These are then in your repository as "remote tracking branches", e.g. branches named like remotes/origin/master or such.

Fetching new commits from the remote repository will not change anything about your local working copy.

Your working copy has normally a commit checked out, called HEAD. This commit is usually the tip of one of your local branches.

I think you want to update your local branch (or maybe all the local branches?) to the corresponding remote branch, and then check out the latest branch.

To avoid any conflicts with your working copy (which might have local changes), you first clean everything which is not versioned (using git clean). Then you check out the local branch corresponding to the remote branch you want to update to, and use git reset to switch it to the fetched remote branch. (git pull will incorporate all updates of the remote branch in your local one, which might do the same, or create a merge commit if you have local commits.)

(But then you will really lose any local changes - both in working copy and local commits. Make sure that you really want this - otherwise better use a new branch, this saves your local commits. And use git stash to save changes which are not yet committed.)


Edit: If you have only one local branch and are tracking one remote branch, all you need to do is

git pull

from inside the working directory.

This will fetch the current version of all tracked remote branches and update the current branch (and the working directory) to the current version of the remote branch it is tracking.

like image 35
Paŭlo Ebermann Avatar answered Oct 10 '22 05:10

Paŭlo Ebermann