Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Fetch fails to work on bare repo, but git pull works on normal repo

Tags:

git

First, the big picture: I'm trying to write a git post-receive script for a Redmine / Gitolite server I'm running. As per various recommendations, I'm creating a bare and local repository for Redmine to read from, and I'm setting up a post-receive script on Gitolite to push changes into the Redmine repo.

However, I'm very noobish with Git, so I'm unable to even do a simple task here >_<. I think if I figure this out, I should be able to write the above script. After setting up my test repo, I've created two repos as a test.

(The "Central Repo" is a Gitolite repository at git@localhost:testing)

cd /tmp
mkdir /tmp/test
$ git clone git@localhost:testing
$ git clone git@localhost:testing testing2
$ git clone git@localhost:testing --bare

Now when I run ls:

$ ls
testing  testing2  testing.git

Now, I change the test file inside of the testing2, and then push the changes to the central repo.

$ cd testing2
$ echo 'testline' >> test && git commit --allow-empty-message -a -m '' && git push 

As expected, if I run "git pull" on the "testing" folder, everything works as expected.

$ cd testing
$ git pull
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From localhost:testing
   3242dba..a1ca5ba  master     -> origin/master
Updating 3242dba..a1ca5ba
Fast-forward
 test |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
$ diff ./test ../testing2/test
$

As shown with the last "diff", the "testing" directory and "testing2" directory work exactly as expected. The "git pull" command synchronizes the two directories.

However, if I cd into testing.git (aka: the bare repo), a git fetch / git reset --soft fails to update the bare repo to the latest version.

$ ls
branches  config  description  HEAD  hooks  info  objects  packed-refs  refs
$ git fetch
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From localhost:testing
 * branch            HEAD       -> FETCH_HEAD
$ git reset --soft
$ cd ..
$ git clone ./testing.git testing3
Cloning into testing3...
done.
$ cd testing3
$ diff test ../testing2/test
5a6
> testline

As you can see from the last example, the bare repository failed to get updated, and there is somehow a difference between the two files. What did I do wrong?

Thanks in advance

like image 864
Dragontamer5788 Avatar asked May 22 '12 06:05

Dragontamer5788


People also ask

Is git pull and git fetch same?

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.

Should I use git fetch or git pull?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.

Why git pull is not recommended?

it modifies your working directory in unpredictable ways. pausing what you are doing to review someone else's work is annoying with git pull. it makes it hard to correctly rebase onto the remote branch. it doesn't clean up branches that were deleted in the remote repo.


1 Answers

Your fetch hasn't updated the master branch, only FETCH_HEAD (see "What does FETCH_HEAD in Git mean?").

As mentioned in "how do I pull to a bare repository?", you should do a:

git fetch origin master:master

Or, for all the branches:

git fetch origin +refs/heads/*:refs/heads/*

Colin D Bennett added:

If you want to fetch these on a regular basis, you should consider:

git config remote.origin.fetch +refs/heads/*:refs/heads/*

which will allow you to type git fetch to sync your branches with the remote.
Note that this make sense only in a bare repository where local branches are not supposed to be edited.

like image 107
VonC Avatar answered Oct 31 '22 13:10

VonC