Redsandro
Redsandro

Reputation: 11356

Should .git contain all files? How do I fix problems?

I have a plain copy backup of a .git directory. I don't have the actual files outside of the .git directory in the backup (git status shows that all files would be deleted on commit), only the actual .git directory.

When I do git checkout master, I get about half of the files that should be there. I was under the impression that this should recreate all the project files.

Does this mean that somewhere in the git history there is an error? Some files changes clearly couldn't resolve all the way to a complete file. If I get a backup from the complete project including the files, how can I 'fix' my history to make sure it contains everything?

I'm not a git-pro, but the backup, unpacked to an empty directory, is currently sitting in a feature branch. So according to git status, a million files would be deleted if I would do git commit. I reckoned doing git checkout master would give me all the files in the latest master commit in the empty directory.

Upvotes: 0

Views: 167

Answers (3)

netdigger
netdigger

Reputation: 3789

Dont know why half the files are missing, but you could try:

git clone .git /path/to/new/repo

Then you would trick git into beliving it's a bare repository, and clone from it to /path/to/new/repo, where master would be checked out.

On your edit: The modification since last commit will not stay on one branch. So for example, in your case, git status shows that all files are deleted, changing branch then doesnt "keep" this changes on that branch, but the changes brought to the next brach you checkout, unless you stash it first.

Consider this example:

git init gittest
cd gittest
echo "First commit" > a
git status
   Untracked files:
      a

git add a
git commit -m "Initial commit"
git branch test
git checkout test
echo "Editing a" >> a
git status
   modified: a
git checkout master
M      a
Switched to branch 'master'
git status
   modified: a
cat a
First commit
Editing a

Here I work in master and commit the first commit, then change branch to test, edit a, then move back master, and see, the changes are kept! This is what happends to you, in your feature branch, all files are deleted, and when you checkout master, you bring these changes (the removed file chanes) to master as well.

You see? It's a bit messy explanation, but hope you get it.

If you want to undelede the files, you can use whats described in this answer How do I revert all local changes in Git managed project to previous state?, by doing:

git checkout .
git reset
# vìola

Upvotes: 1

dseminara
dseminara

Reputation: 11935

Git stash would undo all the changes on working copy (including 'deleted' files) restoring all your files to working copy

git stash

The same result like if you were using "git clone" again

After this, if you want to delete the recently created stash, you can do:

git stash drop stash@{0}

I don't recommend deleting stash, since you wouldn't know at first when you would need the stashed info, as also stashes are not really using too much space (stashes are internally represented in the same structure as commits objects)

Upvotes: 1

Mudassir Razvi
Mudassir Razvi

Reputation: 1833

A better and safe option would be:

  1. Copy the contents of a .git dierctory into a new directory

  2. cd into the new directory and run git config --bool core.bare true This will make the new directory as a bare repo

  3. Now run git clone <URL-To-Bare> <New-repo>

hope it solves the problem!

Upvotes: 1

Related Questions