Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to untrack files (git and git GUI)

Tags:

git

git-gui

I am using git GUI (together with Git bash). Now, I know if I want to ignore files for git I have to put them in a .gitignore file.

1)I have come to know that git in windows does not create a .gitignore file for you and you have to create it yourself. Also git GUI won't do that. Is this correct?

Now my special situation:

I made a mistake in the beginning of managing this project and together with my .c and .h files I also tracked another file (let's call it "shouldnottrack.file"). This file is generated by the build process, so in principle should not be tracked.

But I did it and it was included in the commits (staged, commited etc).

Now, I want to untrack this file.

I have done it temporalily by doing git checkout -- shouldnottrack.file but this works only once

  1. How can I untrack this file? (GUI, command, any method ok)

  2. If I include this file in the .gitignore file, will git untrack it automatically?

like image 847
KansaiRobot Avatar asked Dec 09 '16 00:12

KansaiRobot


People also ask

How do I add untracked files to git GUI?

If the file is not shown in the Unstaged Changes Window in the main Git Gui window press the Rescan Button. Then to click the Stage Changed button. Then a Confirm window will be shown to add the untracked file. Untracked means Git sees a file that it didn't had in the previous commit.

How do I Untrack a file in Git Visual Studio?

Visual Studio 2019 | Visual Studio 2022 For files that aren't tracked by Git, you can use a . gitignore or exclude file. For files that are tracked by Git, you can tell Git to stop tracking them and to ignore changes.


2 Answers

You have to add it to .gitignore and remove it from the git repository so that it stop to track it.

.gitignore has no effects on file already tracked.

You have 2 possibilities depending if you still want to keep your files in the working directory or not:

  • If you want to delete the files in your working directory, do git rm.
  • If you want to keep them but just stop to track them, do git rm --cached.

Then, commit this file deletion.

like image 192
Philippe Avatar answered Sep 24 '22 02:09

Philippe


Not sure about your first question as I exclusively use the Git CLI however for your second question try this:

'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE' \
--prune-empty --tag-name-filter cat -- --all```

Replacing PATH-TO-YOUR-FILE with the actual file name you want to scrub from the history. You should see something along the lines of:

Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266) Ref refs/heads/master was rewritten


If it was successful - After this if you have already pushed to your remote you will need to do a `git push --force` to overwrite it. Security settings on the branch may deny the push.

Finally for your last question to prevent this happening again add the file in question to your ignore file and git will prevent any tracking from occurring.

The above answer will work however I strongly suggest this approach if the data was sensitive - ie passwords, ssh keys etc as the files will still be in history.

You can read more here https://help.github.com/articles/removing-files-from-a-repository-s-history/ and here https://help.github.com/articles/remove-sensitive-data/.
like image 23
Dylan Scott Avatar answered Sep 24 '22 02:09

Dylan Scott