How to remove a file from git repo on local and remote

gdbj

I wish I knew git better.

I have some project settings files in my repo that conflict with a colleague's each time we commit our project. Not totally sure why, but it's time these files were removed from the repo due to the noise they create. I cannot get this to work to save my life.

Before starting, I ensured that my branch is completely up to date with the remote. Then, I used

 git filter-branch -f --index-filter \ 'git rm -r --cached --ignore-unmatch *.csproj' HEAD

I added the -f flag due to some issue with backups, since I've run this a couple times now.

Running this command seems to remove all the files from my repo. However, when I try to push this change to the server, it gives an error saying my repo is 2 commits behind the remote version. It recommends that I pull, which then undoes the command above.

What am I missing to get this change up to my server? (Bitbucket)

Protagonist

If I understand your problem correctly, you cannot push to the remote repository because your local history does not match to the remote history. This is what usually happens when you change hit commit locally with rebase or with filter branch.

If you push your local changes to the remote, you will rewrite some or all of the history of your projects. This means that every developer on your project will hit a bump when they try to git pull because now their local history is different from the remote's history. In a case like this, you usually want to message your co-developers about this happening because this can be pretty confusing as well as time consuming if they don't know what your filter-branch did.

Having said that, I believe you're looking for the --force option for git push. You want to run git push --force origin master. This will, as the name implies, overwrite whatever is in the remote with your local git history.

Please, read the docs before you do this and ask more questions if you have any as this is a potentially destructive thing to do.

Edit: Your coworker has two option that I'm aware of. They can either go the fetch/reset route or the rebase route.

The fetch/reset route entails reseting your coworker's local repository to be exactly like the remote repository. This can be done with:

git fetch origin && git checkout master
git reset --hard origin/master

The rebase route entails applying your coworker's local commits on top of the newly cleaned master. In interactive mode, they'll have the chance to review every commit before including it. If the commit contains the file you wanted gone, they can simply omit it. This can be done with

git fetch git rebase -i origin/master

The first option would be preferable if both of you agree that your repository should be the canonical one as it's faster. The second approach in turn gives you finer grained control.

You can check out this SO question that tackles the issue of pulling after force pushing more thoroughly: git pull after forced update

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Git: How to remove remote origin from Git repo

From Dev

How to disconnect local git repo from remote master

From Dev

How to compare local with remote git repo in PhpStorm?

From Dev

How to compare GIT Remote Repo and local in Netbeans

From Dev

Git: How to remove secondary remote repo

From Dev

Git / GitHub: How to get new local repo into empty remote repo

From Dev

How do I tell git to ignore my local changes, but leave the file in my remote repo?

From Dev

How to know local repo is different from remote repo, without fetch?

From Dev

How to know local repo is different from remote repo, without fetch?

From Dev

Can I add a file to my local Git repo but not push it to remote

From Dev

Creating a git repo on a remote server from a local machine

From Dev

Going remote from local repo: Git and forgetting large files

From Dev

Going remote from local repo: Git and forgetting large files

From Dev

Git - Removing multiple previous commits from both local and remote repo

From Dev

How to remove a directory level from a GIT repo?

From Dev

Deleted local files automatically remove from git repo?

From Dev

Deleted local files automatically remove from git repo?

From Dev

How to prevent a local file being synced with the remote repo?

From Dev

How to remove/revert commited changes to local git repo

From Dev

Don't remove file from repo when is not in local

From Dev

If on a local branch, how should I push that repo to remote from local repo that already exists?

From Dev

Why does Git show my local "pull from remote" when I push my commit to the remote repo?

From Dev

Check if local git repo is ahead/behind remote

From Dev

Merge local git repo with remote one

From Dev

Bring a local folder to remote git repo

From Dev

git not overwriting remote repo with local files?

From Dev

How to remove the git remote master from bitbucket?

From Dev

How to remove remote git urls from xcode?

From Dev

How to remove remote git urls from xcode?

Related Related

  1. 1

    Git: How to remove remote origin from Git repo

  2. 2

    How to disconnect local git repo from remote master

  3. 3

    How to compare local with remote git repo in PhpStorm?

  4. 4

    How to compare GIT Remote Repo and local in Netbeans

  5. 5

    Git: How to remove secondary remote repo

  6. 6

    Git / GitHub: How to get new local repo into empty remote repo

  7. 7

    How do I tell git to ignore my local changes, but leave the file in my remote repo?

  8. 8

    How to know local repo is different from remote repo, without fetch?

  9. 9

    How to know local repo is different from remote repo, without fetch?

  10. 10

    Can I add a file to my local Git repo but not push it to remote

  11. 11

    Creating a git repo on a remote server from a local machine

  12. 12

    Going remote from local repo: Git and forgetting large files

  13. 13

    Going remote from local repo: Git and forgetting large files

  14. 14

    Git - Removing multiple previous commits from both local and remote repo

  15. 15

    How to remove a directory level from a GIT repo?

  16. 16

    Deleted local files automatically remove from git repo?

  17. 17

    Deleted local files automatically remove from git repo?

  18. 18

    How to prevent a local file being synced with the remote repo?

  19. 19

    How to remove/revert commited changes to local git repo

  20. 20

    Don't remove file from repo when is not in local

  21. 21

    If on a local branch, how should I push that repo to remote from local repo that already exists?

  22. 22

    Why does Git show my local "pull from remote" when I push my commit to the remote repo?

  23. 23

    Check if local git repo is ahead/behind remote

  24. 24

    Merge local git repo with remote one

  25. 25

    Bring a local folder to remote git repo

  26. 26

    git not overwriting remote repo with local files?

  27. 27

    How to remove the git remote master from bitbucket?

  28. 28

    How to remove remote git urls from xcode?

  29. 29

    How to remove remote git urls from xcode?

HotTag

Archive