Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain different directory structure in different git branches

Tags:

git

I had a problem with git. I have a repository of a project that's getting old and want to start a new version. I've created a new branch (3.0-wip) and deleted the files and folders to start again. However, if I checkout my master branch, the files and folders are gone from there too.

How can I have my repository so my master branch has everything the remote repo has, but a completely different working directory in my 3.0-wip that I can modify to my heart's content without destroying the files and folders in any other branch?

like image 693
Martin Bean Avatar asked Dec 16 '11 21:12

Martin Bean


2 Answers

What's happening is you're not using git correctly. There's absolutely no problem with creating a branch and deleting files and folders. When you switch back to your master branch, the deleted files and folders will be restored, as they haven't been deleted from that branch.

What's happening in your case is that you've removed the file/folders out from under git, by using the operating system delete, rather than the "git rm" command. That's why git status reports them as "deleted". It's also why the files stay deleted when you switch branches. git is expecting the files to be there and they're not.

Git status tells you to run "git add" but that doesn't work. Since the file is no longer there, running git add will fail silently. You might not notice but the message also tells you to run "git rm". You may think you can't do that since the file is gone, but that's not true. The rm command will still succeed in removing the file from the repo, even though you've already removed it from the file system.

The other, and easier thing you can do is run git add -A. The -A flag will tell add to notice any deleted or added files.

Try this for a test:

git init test-delete
cd test-delete
touch filea
mkdir dir_b
touch dir_b/fileb
git add .
git commit -m "Initial commit"
git checkout -b new_branch
rm -rf dir_b
git add -A
git commit -m "deleted files from branch"
(ls to prove files arent' there)
git checkout master
(ls to show files have been restored)
git checkout new_branch
(ls to show files are gone again)
like image 53
wadesworld Avatar answered Sep 20 '22 01:09

wadesworld


Commited changes in one branch do not affect another branch. You need to commit all the deletes as well, that's what Dominic tried to explain. Simply do:

$ git co $NEWBRANCH
$ git status

And you will see zillions of uncommited deletes. You have to use git rm to remove files.
Not sure git add --all will handle deletes as well. Anyway, this is IMO not the best way to start anew as the new branch will still have the old history.

I think it is better to create a new "disconected" commit:

  1. Create a new repo: mkdir $DIR; cd $DIR; git init
  2. Create the initial commit. touch README; git add --all; git commit -m "Init"

Now go to to original repo and:

  1. Add it as a remote: git remote add start-anew $DIR/.git
  2. fetch it: git fetch start-anew
  3. pull it in: git co -b start-anew start-anew/master

And voila a branch with fresh empty history.

You may want to delete the remote then, and also change the remote for the branch. I am usually editing .git/config manually, so far without an accident.

like image 45
Martian Avatar answered Sep 19 '22 01:09

Martian