Mickael Caruso
Mickael Caruso

Reputation: 9471

How to Sync the Same Exact EXISTING Project on Machine2 that was Pushed To a Git Repository from Machine1?

Before anyone says this is a duplicate, most answers tell me to first clone the remote repository, which means that on Machine2, I should not have had the project in the first place.

What I did in the first place was - from Machine1 containing Project X: git init, git commit -am "Initial Commit", (setting up remote not shown for brevity), then git push -u origin master.

However, on Machine2, I have the exact same Project X, except without the .git files and folders. Now, I want to set up Machine2://Project X to also pull and push to the same remote repository. Should I set up git on Machine2, then commit, then pull? I'm not sure what the right thing to do is for what I'm asking.


Here's what I've done, which I think works, but is tedious, or possibly wrong. I want a shorter and better way.

When I typed git log --oneline, I noticed that there's the most recent commit called "Merge [remote repo url]", then the commit I created, "Initial Commit Machine2", then the repo's commits in the expected descending order. I didn't want to include the Machine 2 commits, so,

From Machine1, I was able to pull the changes committed/pushed from Machine2, which is what I want.

This just seems so tedious especially that I have quite a few repositories to sync between the two machines.

Upvotes: 4

Views: 818

Answers (3)

Peter J. Hart
Peter J. Hart

Reputation: 377

I had a similar situation: all the source except the .git directory. This worked for me:

git init
git remote add origin ssh://...
git fetch
git add .
git checkout master 

This way, there is no commit required.

Also, instead of reset, you can

git checkout -f master

Upvotes: 2

Nick Humrich
Nick Humrich

Reputation: 15765

git init
git add
git commit
git remote add origin [remote repo url]
git pull --rebase origin master
git push --set-upstream origin master

This will give you the message:

Branch master set up to track remote branch master from origin.
Everything up-to-date

And no history will be sent! (Like the merge history you are seeing)

Note that git pull is actually git fetch; git merge in one command for convenience. You do not want to merge, so you need to pick the rebase strategy git pull --rebase which is actually git fetch; git rebase.

Now you should be up to date. Any changes you now want to make you can make by standard convention.


Note: there is not really a "more convenient" way to do this, as well, this sort of violates the purpose of git. And if Machine B has an exact copy of Machine A, whats the harm of just removing all the files, then doing a clone?

Upvotes: 1

VonC
VonC

Reputation: 1326782

Maybe an shorter version could work on Machine2:

cd project2
git init .
git remote add origin /url/of/remote/bare/repo
git fetch
git symbolic-ref HEAD refs/heads/master
git status

The git symbolic-ref HEAD refs/heads/master is for "swithing branch without checkout", in order to not mess with the potential local modifications you might have on Machine2.

Then the git status can tell you how your current working tree (which is untouched at this point) differs from the index.

Upvotes: 1

Related Questions