Home > Back-end >  how to commit changes in git with untracked files
how to commit changes in git with untracked files

Time:07-27

When creating a project in GitLab, a file that has to be untracked was added (venv). I deleted it with git rm -r --cached venv and when I tried to commit these changes, I receive this message.

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .idea/vcs.xml
        venv/

nothing added to commit but untracked files present (use "git add" to track)

How to commit these changes and what was my mistake?

CodePudding user response:

You must tell Git, which file you want to commit with git add (by the way, that's what the error message says!)

So simple use git add -A

Update the index not only where the working tree has a file matching but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

(The whole documentation can be consulted here)

After added the files, you must git commit -am "added newly untracked files" and git push to make the remote repository clear, which files should no longer be tracked.

CodePudding user response:

When creating a project in GitLab, a file that has to be untracked was added (venv). I deleted it with git rm -r --cached venv ...

This part is fine.

[but] when I tried to commit these changes, I receive this message.

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .idea/vcs.xml
        venv/

nothing added to commit but untracked files present (use "git add" to track)

This means you have no changes with respect to the current commit.

That is, the venv/* are both untracked and not in the current commit. The .idea/vcs.xml file is likewise untracked and not in the current commit.

That means there's literally nothing to do at this point!

You might, however, wish to make Git shut up about these untracked files. To make that happen you should edit the .gitignore file to add:

.idea/vcs.xml
venv/

to it. Note that at least some files in .idea should not be committed, but some say that some files should be committed: see Which files in .idea folder should be tracked by Git?. The accepted and highest-scored answers (which at the moment are different) both recommend adding and committing .idea/vcs.xml, so if you wish to do that, you should not list it in your .gitignore.

(It is your decision which files to list. Remember that .gitignore does not, by itself, cause a file to be ignored. Instead, it causes Git to shut up about a currently-untracked file, and it makes git add --all and other en-masse "add" operations not add the file. As such, the name .git-do-not-complain-about-these-files-when-they-are-untracked-and-do-not-add-them-when-using-an-en-masse-add-command-either-if-they-are-untracked-at-that-time-because-they-are-supposed-to-stay-untracked-whenever-they-are-currently-untracked might be more accurate. But who will type in this file name?)

If you do update your .gitignore file (or create one), you should then git add the .gitignore, and now you will have some changes with respect to the current commit and will need to make a new commit.

Otherwise—if you're content to have Git continue to complain like this—there's really nothing to do.

Further discussion

Remember, a tracked file is one that is in Git's index / staging-area right now. An untracked file is one that is not in Git's index / staging-area right now, but is in your working tree. That's all there is to whether some file is tracked or untracked: it's about what's in the index and your working tree right now.

You can, however, change which files are in your working tree at any time, by creating, removing, or modifying such files. You can change which files are in Git's index at any time, by running git add or, as you did, by running git rm --cached. These actions change which files are tracked right now. They have no effect on any existing commit. (It is literally impossible to change any existing commit.)

The set of files that are in Git's index right now are the set of files that would be in a new commit, if you ran git commit right now. That's why you run git add or git rm --cached: to change the set of files that git commit would freeze forever into a new commit.

As you do work, you will run git add. This copies the current version of the working tree file into Git's index, so that your proposed next comimt snapshot is now updated. If that was a mistake, you can change the file back and run git add again. Or, if—as in this case—git add-ing the file at all was the mistake, you can run git rm --cached on the file, to remove it from Git's index without affecting the working tree copy.

If the file wasn't in Git's index, it was untracked. If you then added it to Git's index it became tracked. If you then remove it again it is back to being untracked. Until you run git commit, each of these updates to Git's index merely changes your proposed next commit, not any actual commit. So make all the changes you like, and when they're right, run git commit to freeze them into a new commit.

  • Related