hbt
hbt

Reputation: 1067

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 everything. How can I stash my config files (only)?

Thanks

Upvotes: 4

Views: 2652

Answers (4)

hbt
hbt

Reputation: 1067

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

Upvotes: 0

Alex Brown
Alex Brown

Reputation: 42892

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.

Upvotes: 3

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

Two things.

  1. 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 using git add -p) you want to commit using git add. You should be doing this.

  2. Unless your config file is an integral part of your application (and you want to version control it), you should be ignoreing and not even version controlling it.

Upvotes: 7

lprsd
lprsd

Reputation: 87095

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.

Upvotes: 2

Related Questions