How to commit all file except one in GitHub for Windows
Asked Answered
B

3

24

I want to commit all files except for one file. Can I do that using the GitHub for Windows client or do I need to use the command line? If I need to use the command line, how do I use it?

Brasier answered 30/10, 2014 at 7:33 Comment(3)
Yes, you can do it via the clientValerio
#4475957 Please check this link. This might help you.Kaila
Thanks for suggestion, but command removes all the modified files. I just want to commit all except one file and file should have changes.Brasier
S
50

As far as I know this is not possible without using the git shell/command line. In the Git Shell you have several options. All of them yield a slightly different result.

  1. If you only want to exclude the file for a little time (maybe one commit) and add it in a future commit you can execute the following commands:

    git add .
    git reset filename
    git commit -m "commit message"
    

    The first command adds all files to the staging area. Then the second command removes the one file from the staging area. This means the file is not added in the commit but the changes to it are preserved on your local drive.

  2. If the file is already committed to the repository but you only sporadically want to commit further changes of the file, you can use git update-index --assume-unchanged in this way:

    `git update-index --assume-unchanged filename`
    

    If you then change the file locally it will not get added to the staging area when you execute something to add all files, like git add .. When, after some time, you want to commit changes to the file again, you can run git update-index --no-assume-unchanged filename to stop ignoring the changes.

  3. You can use the .gitignore file if you don't want to track a file at all. To ignore a file named filename you create, or edit, a file called .gitignore. Put filename on a line of its own to ignore that file. Now the file is not added to the staging area when you execute git add .. Remark: If the file is already checked in you have to remove it from the repository to actually start ignoring it. Execute the following to do just that:

    `git rm --cached filename`
    

    The --cached option specifies that the file should only be removed from the index. The local file, whether changed or not, will stay the same. You can add the .gitignore file and commit to make the file get ignored at other machines, with the same repository, as well.

  4. If you want to ignore an untracked file but don't want to share this ignoring with other repository contributors you can put the names of ignored files into the file .git/info/exclude. The .git directory is normally hidden but you can make it visible by altering the folder options. As with the .gitignore file, you have to execute git rm --cached filename if the file was already checked in by a previous commit.

Some notes:

  • Change filename into the actual name of the file you want to exclude.
  • Instead of excluding a single file you can also exclude a complete directory. You can do that by simply substituting the directory name in the place of filename.
  • An easy way of starting a Git Shell is by starting GitHub for Windows, right clicking on the project in which you want to exclude/ignore the file and selecting the option Open in Git Shell. Now you are at the root of the git repository and you can start executing the commands that are shown and described in this answer.
Sonatina answered 8/11, 2014 at 23:46 Comment(0)
S
1

You can just create a .gitignore file and add the name of the file you want to exclude in it.

Seesaw answered 21/3, 2019 at 18:0 Comment(0)
C
0

UPDATE: This was "the old way" of doing it. Now there is a command called git reset which you should use in stead, as it removes the risk of deleting files you do want in your commit.


I would do the following:

git add .
git checkout filename
git commit -m "commit message"

The first line adds all files on the list, and the second line removes the file you specify, where "filename" should be the name of the file you don't want in your commit.

Or

git checkout filename
git commit -am "commit message"

git commit -am adds files and makes the commit message at the same time.

And if you need to REMOVE the changes you made in the last file:

git stash -u
Crispation answered 2/10, 2017 at 7:5 Comment(2)
THIS IS DANGEROUS! The checkout command deletes all the changes you made to a file from the last commit and that isn't the kind of command you would use if you want to commit all the files except one as the OP wanted. You should use reset in replace.Garay
In 2017 when I wrote this answer, git reset did not exist, but Thank you jmm for pointing this out - I will update my answer.Crispation

© 2022 - 2024 — McMap. All rights reserved.