Search code examples
gitintellij-ideastaging

Automatically stage new files Git


Here is my problem:

The .idea/ folder is updated each time I make a changes to the code, thus I have to stage it each time I make a change, which is tiring.

I'd like to know if there is a way through Git bash(preferably) or IntelliJ so that it gets staged automatically.

Thank you for your time.


Solution

  • Before we get to the answer... as others have noted, committing IDE-specific files is not usually a good idea. I'm aware that IDEA stores a lot of configuration in the .idea directory, and if you're not using a build tool (like Maven if you're as old school as me, or Gradle if you're into more modern tooling), then it may be that committing these files preserves work you don't want to redo; but I would recommend using an IDE-neutral build tool and committing the corresponding project definition file (e.g. pom for maven, etc.) Then you can safely remove the .idea files and add them to your ignore rules.

    But ok, your question is how to make sure changes at a certain path (.idea/**) are included in every commit. Since you prefer a git bash-based solution, I would say that an alias or script is your best bet; as long as you use the alias or script instead of regular git commit, you can have it add the .idea directory before the commit.

    git config alias.ci '!git add :/:.idea && git commit'
    

    The changes will still show up as "unstaged" in git status. There's not a lot you can do about that.