Git Stashing specific files before a commit
I'm not sure if what I'm looking for is a git-stash
but here is what I want to do.
I have configuration files customized for local use. Those files already exist in Git. Now, if I add a new feature (change other files), I want to stash my config and hit commit and only commit the files related to my new feature.
If I use git-stash, it stashes eve开发者_JAVA技巧rything. How can I stash my config files (only)?
Thanks
Two things.
You should stage only the files you want to for the commit. This is done using
git add
. Even if you have a ton of changes, you can stage only the fields (and pieces of files usinggit add -p
) you want to commit using git add. You should be doing this.Unless your config file is an integral part of your application (and you want to version control it), you should be
ignore
ing and not even version controlling it.
use git add -p
and select only those changes you want to commit. this is good practise anyway, because it acts as a little code review.
stash
intendeds to save all local changes uncommitted into any branch into a temporary unnamed branch. Essentially, it's internals are same as that of branch.
You should probably not even check in the config files and if you do, you should put it in a branch you merge only on the local system.
This is late but I found a better solution (a few years ago).
Using git-assume-status and git-unassume-status
#!/usr/bin/env ruby
# used to ignore modified files -- e.g configuration files
ret=%x{git status --porcelain}
files=ret.lines.map { |x| x.split[1] }
files.each do |file|
# makes files resistant to git reset hard
%x{git update-index --skip-worktree #{file}}
%x{git update-index --assume-unchanged #{file}}
end
and for git-unassume
#!/usr/bin/env bash
# used to ignore modified files -- e.g configuration files
cd .git
rm index
cd ..
git reset
This way you can have your configuration files modified but ignored by status and other git add commands. If you change your mind, you run git-unassume
精彩评论