0

I accidentally forgot to include a .gitignore file when pushing an existing local git repository to Bitbucket. As a result, a large amount of unwanted .aux files appears on the remote repository.

Deleting them directly on Bitbucket is at best going to take a long time. Is there any good way to do this locally? Or is there a safe way to essentially "start over" so that I can include the .gitignore file?

Also, for whatever reason the .aux files are nowhere to be found in my local files. They just appeared on Bitbucket after I did my initial push.

I am a novice with git, so explicit solutions are especially appreciated.

UPDATE: Unfortunately, I didn't manage to solve the problem with any of the proposed solutions (which I am grateful for). I ended up just copying my files and then reverting the initial commit. I then copied the files back to the original directory. Finally, I re-pushed them with the .gitignore file included this time.

CuriousKid7
  • 123
  • 1
  • 5
  • Possible duplicate of [Can I rewrite an entire git repository's history to include something we forgot?](https://stackoverflow.com/questions/27927933/can-i-rewrite-an-entire-git-repositorys-history-to-include-something-we-forgot) – phd Jan 05 '19 at 13:26
  • https://stackoverflow.com/search?q=%5Bgit%5D+add+.gitignore+retroactively – phd Jan 05 '19 at 13:26
  • 1
    https://stackoverflow.com/search?q=%5Bgit%5D+remove+file+remote+repository – phd Jan 05 '19 at 13:26
  • Is this your personal repo or a shared repo? – Manish Bansal Jan 05 '19 at 13:57

5 Answers5

1

add the path you want to ignore to .gitignore

then stop tracking the file at specified path by removing it from index by executing:

git rm --cached <file_path>

next commit and push will remove the files at the specified path.

AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
0

There are few ways to do this, e.g.:

  • go to a new branch using git checkout -b <newbranch>and delete the files locally
  • ammend the commit using git commit --amend or push it as new commit
  • go back to your master branch
  • then you can add the .gitignore and commit it as well

Another way:

  • revert the commit using git revert <commitId> (this will create a new commit which removes the commit entirely)
  • add the commit again (e.g. using git cherry-pick <commitId>)
  • add the files to the .gitignore
  • commit this new commit

A third way is using

git rm --cached <file> which is well documented here.

Let me know if you need any further informations.

PS: always verify your commits using git status

elp
  • 840
  • 3
  • 12
  • 36
0

Ok. Below is the strategy I would follow. Before that, remove the .gitignore from your project. Now, your working tree and staging area would be clean. And both local and remote repo would be having .aux files.

Step 1: Remove the last commit from the local repo.

git reset --soft head~1 

This will remove the last commit which effectively will move your .aux files from commit area to the staging area.

Step 2: Push the local repo forcefully into remote bitbucket repo.

git push -f origin master

At this point, if you check your bitbucket repo, you would find that your previous commit is removed. So, half the damage is recovered.

Step 3: Bring all the staging area files to the working area.

git reset head

Step 4: Add .gitignore to the project.

Step 5: Execute git status to check if you can see your ignored files. I am sure you won't see them. However, you will see all your wanted files which you wished to commit earlier but got committed along with un-wanted ones.

Step 6: Add all working directory files to staging area, commit them and push to the remote repo.

git add .
git commit -m "message"
git push origin master
Manish Bansal
  • 2,400
  • 2
  • 21
  • 37
  • Do I need to remove the .gitignore from both local and remote repos? – CuriousKid7 Jan 05 '19 at 16:08
  • I thought you added .gitignore in your local only after pushing .aux in remote repo by mistake. My reason to ask you remove .gitignore is to keep working and staging area clean. If git status is showing nothing to commit, no need to do anything. Just proceed with step 1. – Manish Bansal Jan 05 '19 at 16:21
0

If you want to purge git repo from unwanted files to minimize the size of the bare repo and remove them from history like it never happen, it is possible, the filter option, but it would rewrite the full history with whatever start point you want and every excising commit would have a new commit hash, and you would have to force-push the whole repo. And it's ok as long as everyone on the team stops the work, meaning they pushed everything and they are not commiting new changes locally. And they would have to delete the repo locally, not just a branch the whole local cloned folder

You would re create the repo, force push, and ask them clone fresh. You can google
"Git filter-branch" but here you have some initial info

https://torchbox.com/blog/how-remove-unwanted-files-your-git-repository/

Before you play with advanced operations like this I recommend you make 100% clone backup to another origin in your account, like this the URLs are for presentation only, you need to use yours obviously

git clone --bare https://bitbucket.org/YouInstance/your-repo.git
cd your-repo.git 
git push --mirror https://bitbucket.org/YouInstance/your-backup-repo.git
 
# delete cloned bare repo
rm -rf your-repo.git 

Yes the extension is part of folder name because it's a bare repo as stored on the server. rm -rf is Linux/bash style command to force delete folder, so if you are working from windows CMD, it will be rmdir

Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
-1

first untrack all existing files using the following command:

git rm -r --cached

and then update your .gitignore and commit/push again.

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Md Golam Rahman Tushar
  • 2,175
  • 1
  • 14
  • 29