gokuzzzz
gokuzzzz

Reputation: 11

Git Remove a file from branch but keep it for the "child" branch

I have a question for Git. For example, I have a branch person_branch which has three files, eat.txt, drink.txt and study.txt. I have another branch student_branch(the "child") which branched off person_branch and has the exact same files. Both are pushed to remote.

Now I want to remove study.txt from person_branch because it makes more sense to have it in the student_branch. I tried to git reset HEAD~1 for person_branch, then git add eat.txt and drink.txt only. and committed, but git push -f origin person_branch is now not happy. It gives this message:

remote: To prevent you from losing history, non-fast-forward updates were rejected
remote: Merge the remote changes (e.g. 'git pull') before pushing again.  See the 'Note
remote: about fast-forwards' section of 'git push --help' for details.

and gives an error message says ![remote rejected] person_branch -> person_branch(pre-receive hook declined) and error: failed to push some refs

What am I doing wrong and what should I've done instead? Any help would be appreciated.

Upvotes: 1

Views: 41

Answers (1)

user28731
user28731

Reputation: 318

It sounds like your git server is configured to reject pushes that rewrite history (regardless of your git push -f force option). So unless you have a way to reconfigure the server you might not be able to literally "rollback" commits (if this is github, as suggested by the tag, then the "Allow force pushes" setting is what you'd need in the branch permissions).

Your best bet, which doesn't require a force push, is probably to make a new commit on person_branch that deletes the unwanted files. ie:

git checkout person_branch 
git reset --hard origin/person_branch 
git rm -f study.txt
git commit -a -m "remove study.txt"
git push origin person_branch 

Upvotes: 1

Related Questions