Aerodynamika
Aerodynamika

Reputation: 8433

How to commit only some new files and ignore the modified ones?

Using git I want to commit only some newly added files into the repo, but to ignore some modified ones (that were infected by a virus).

How do I do that?

I want to keep those files in the repo, I just don't want to commit the corrupted ones.

I tried

git add _my_new_files_

and I can also do

git rm _updated_files_not_to_add_

But then those files will be removed from the repo and I want to keep the old versions in...

Maybe I can add the files I want to add and stash the rest?

Upvotes: 0

Views: 198

Answers (3)

torek
torek

Reputation: 490078

To add a bit to Ron Nabuurs' answer, note that git commit obtains the commit content from Git's index, not from the work-tree. (The index is also called the staging area and sometimes the cache.) When you run git add file, Git copies the content of the given file from the work-tree into the index.

If some of your work-tree files are damaged, but the index versions are intact, you can simply avoid running git add on the work-tree files. The index versions remain unchanged and git commit will use them.

If the index version is also damaged, but the HEAD version is intact, you can instruct Git to copy the HEAD version into the index, using git reset as Ron showed: git reset file.

Think of the index as sitting "between" the current (HEAD) commit and the work-tree, because it does:

HEAD commit      index        work-tree
-----------    ---------      ---------
README.md      README.md      README.md
file.ext       file.ext       file.ext

The operation that copies from work-tree to index is git add, and the operation that copies from commit to index is git reset.

(Any committed file is by definition read-only, so there is no operation to copy from index to commit. Of course, you can make a new commit, which becomes your HEAD commit, using git commit to turn the index into a commit.)

It's also possible to copy from any commit, to the work-tree, using git checkout in the git checkout commit -- file form of the command. Note, however, that when you do this, git checkout first copies the file from the given commit to the index, then from the index to the work-tree, so this kind of copy replaces both index and work-tree versions.

Upvotes: 1

Alex028502
Alex028502

Reputation: 3824

I do most git operations on the command line, but I find staging files and committing much easier with the standard gui.

In linux, osx, or git bash type git gui &. If it does not come up, you need to install it. If you are using a mac, I think the brew version of git comes with the gui, but the github one doesn't. (not sure)

This allows you to visualise which files are staged, and very easily stage lines of files even.

You can even stage a file in the gui, and then type git status on the command line and see that the file is staged.

Upvotes: 0

Ron Nabuurs
Ron Nabuurs

Reputation: 1556

To add the files you want to add to the remote use the command as you said.

git add _my_new_files_

This should be all you have to do, after that commit and push.

If you end up with added the wrong files to your commit, for example the _updated_files_not_to_add_ then you can remove them from your commit before pushing with the following command:

git reset _updated_files_not_to_add_

Upvotes: 1

Related Questions