Maintaining a different directory structure in different git branches

I had a problem with git. I have a project repository that is aging and you want to start a new version. I created a new branch ( 3.0-wip ) and deleted files and folders to start over. However, if I check my main branch, the files and folders are gone too.

How can I create my repository so that my main branch has everything that has a remote repo, but a completely different working directory in my 3.0-wip , which I can change in my hearty contents without deleting files and folders in any other branch?

+6
source share
2 answers

The recorded changes in one branch do not affect the other branch. You also need to record all deletions that Dominic tried to explain. Just do:

 $ git co $NEWBRANCH $ git status 

And you will see many unconditional deletions. You must use git rm to delete files.
Not sure if git add --all will handle deletions. In any case, this IMO is not the best way to start over, as the new branch will still have an old history.

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

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

Now go to the source repo and:

  • Add it as remote: git remote add start-anew $DIR/.git
  • select: git fetch start-anew
  • pull it in: git co -b start-anew start-anew/master

And voila branch with a fresh empty story.

You might want to remove the remote control as well as change the remote for the branch. I usually edit .git/config manually, so far without an accident.

+6
source

What happens, you are not using git correctly. There is absolutely no problem with creating a branch and deleting files and folders. When you return to the main branch, deleted files and folders will be restored, since they were not deleted from this branch.

What happens in your case is that you deleted the file / folders from git using the removal of the operating system, and not the "git rm" command. This is why git status tells them to be "remote". Also why files remain deleted when switching branches. git expects files to be there, but they are not.

Git status indicates that you are running "git add", but that does not work. Since the file no longer exists, running git add will fail. You may not notice, but the message will also tell you to run "git rm". You might think that you cannot do this because the file is gone, but it is not. The rm command will still be able to delete the file from the repo, even if you have already deleted it from the file system.

Another, and easier than you can do, do git add -A. The -A flag will say add to mark 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) 
+8
source

Source: https://habr.com/ru/post/903953/


All Articles