0

On commit I am rebuilding my application for a node project and the built files are placed in the dist directory. To avoid problems with files in the dist directory already being staged, I do the following:

  • Unstage the dist directory
  • Reverse any changes in the dist directory
  • Stash any unstaged or untracked files
  • Build the application
  • Stage the dist directory
  • Unstash the unstaged and untracked files

These are the commands I use:

git reset HEAD -- dist && \
git checkout -- dist && \
git stash push -k -u -m "build" && \
npm run build && \
git add . && \
git stash pop

This worked fine until today. I am getting some odd behavior with a deleted file that is undeleted and untracked after the above commands are run.

Before running the above commands the git status output looks like this:

On branch master                                        
Your branch is ahead of 'origin/master' by 1 commit.    
  (use "git push" to publish your local commits)        

Changes to be committed:                                
  (use "git reset HEAD <file>..." to unstage)           

        deleted:    scripts/substenv.js                 

After running the commands the git status output looks like this:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    scripts/substenv.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        scripts/substenv.js                 

The untracked file appears after the git stash push -k -u -m "build" command.

Can anyone please explain why/what is happening?

nidkil
  • 1,295
  • 1
  • 17
  • 28
  • If you have verified that `git status` before `git stash push -k -u` says that `scripts/substenv.js` is to be deleted (i.e., is not in the index despite being in the `HEAD` commit) and that immediately after `git stash push -k -u`, `scripts/substenv.js` exists in the work-tree, then you have found a bug in `git stash`. However, if it's *not* in the work-tree after that point, but appears later, that's probably all normal. To prove it's a bug in `git stash`, reduce the example further, to get a [mcve]. – torek May 21 '19 at 15:29

1 Answers1

0

You need to add the file to the .gitignore to stop seeing it in the Untracked files list.

Basically your stash has the file in it, then it adds it back when you re-apply the stash.

On a side note, I'm kinda confused by your setup/commands, why don't you just add the dist folder to the .gitignore file?

Update based on comments:

I think you might need to update your npm build script. Since you need to publish your dist folder, the files in it should be checked in. Use the build folder as an intermediate step, and add it to .gitignore.

  • Build the app with npm build
  • This will create bunch of files in build/ folder (which should be ignored)
  • Copy the files you want/need to check-in into your dist folder
  • Profit
HRK44
  • 2,382
  • 2
  • 13
  • 30
  • Hi, thx for the quick response. If I understand what you are suggesting, this means I would also have to add any future file I delete to the .gitignore file. Correct? For a one off this is acceptable, but I would prefer a solution that wouldn't require me to manually intervene. – nidkil May 21 '19 at 09:22
  • To answer your question: the dist folder is required for publication to a private npm registry. – nidkil May 21 '19 at 09:23
  • Well this all depends on your build script, if you just want the same files to be committed like `myapp.min.js` (output of your build script), then you can ignore all the **other files** in the dist folder. Check this link to see how to do it https://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files – HRK44 May 21 '19 at 09:27