Add all files to a commit except a single file?

前端 未结 13 765
遇见更好的自我
遇见更好的自我 2020-11-28 17:03

I have a bunch of files in a changeset, but I want to specifically ignore a single modified file. Looks like this after git status:

# modified:          


        
相关标签:
13条回答
  • 2020-11-28 17:27

    Use git add -A to add all modified and newly added files at once.

    Example

    git add -A
    git reset -- main/dontcheckmein.txt
    
    0 讨论(0)
  • 2020-11-28 17:28
    git add -u
    git reset -- main/dontcheckmein.txt
    
    0 讨论(0)
  • 2020-11-28 17:32

    While Ben Jackson is correct, I thought I would add how I've been using that solution as well. Below is a very simple script I use (that I call gitadd) to add all changes except a select few that I keep listed in a file called .gittrackignore (very similar to how .gitignore works).

    #!/bin/bash
    set -e
    
    git add -A
    git reset `cat .gittrackignore`
    

    And this is what my current .gittrackignore looks like.

    project.properties
    

    I'm working on an Android project that I compile from the command line when deploying. This project depends on SherlockActionBar, so it needs to be referenced in project.properties, but that messes with the compilation, so now I just type gitadd and add all of the changes to git without having to un-add project.properties every single time.

    0 讨论(0)
  • 2020-11-28 17:39

    Now git supports exclude certain paths and files by pathspec magic :(exclude) and its short form :!. So you can easily achieve it as the following command.

    git add --all -- :!main/dontcheckmein.txt
    git add -- . :!main/dontcheckmein.txt
    

    Actually you can specify more:

    git add --all -- :!path/to/file1 :!path/to/file2 :!path/to/folder1/*
    git add -- . :!path/to/file1 :!path/to/file2 :!path/to/folder1/*
    

    For Mac and Linux, surround each file/folder path with quotes

    git add --all -- ':!path/to/file1' ':!path/to/file2' ':!path/to/folder1/*'
    
    0 讨论(0)
  • 2020-11-28 17:40

    I use git add --patch quite a bit and wanted something like this to avoid having to hit d all the time through the same files. I whipped up a very hacky couple of git aliases to get the job done:

    [alias]
        HELPER-CHANGED-FILTERED = "!f() { git status --porcelain | cut -c4- | ( [[ \"$1\" ]] && egrep -v \"$1\" || cat ); }; f"
        ap                      = "!git add --patch -- $(git HELPER-CHANGED-FILTERED 'min.(js|css)$' || echo 'THIS_FILE_PROBABLY_DOESNT_EXIST' )"
    

    In my case I just wanted to ignore certain minified files all the time, but you could make it use an environment variable like $GIT_EXCLUDE_PATTERN for a more general use case.

    0 讨论(0)
  • 2020-11-28 17:44

    For a File

    git add -u
    git reset -- main/dontcheckmein.txt
    

    For a folder

    git add -u
    git reset -- main/*
    
    0 讨论(0)
提交回复
热议问题