How to prevent git commit from deleting files - node.js

I just did a git commit and it deleted a few files seemingly randomly from my node_modules folder. Has anyone had this issue?
To be clear, my latest git commit deleted
delete mode 100644 node_modules/grunt-google-cdn/node_modules/google-cdn/node_modules/cdnjs-cdn-data/external/cdnjs.json
delete mode 100644 node_modules/grunt-node-inspector/node_modules/node-inspector/front-end-node/Images/src/favicon.eps
delete mode 100644 node_modules/moment/min/tests.js
delete mode 100755 node_modules/requirejs/bin/r.js
I've never seen this before and I can't begin to understand why this happened. This prevents me from building my app for deployment. Does anyone know how/why this happened and how this can be prevented in the future?

My expectation is that those files were deleted at some point (via what mechanism, I can't say).
If you're using Git 2.0 or greater, when you typed git add ., you staged the deleted files for commit. git add . basically says "I want to stage all of the changes in this repo", which in this case included deleting files.
The behavior was different in prior versions (I just validated different behavior against Git 1.9 and 2.6.3).

I only did git add . beforehand
So you asked Git to stage everything in . (the current directory). This includes deleted files:
e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree
If these files were deleted by some non-git process and you subsequently ran git add . and then committed, the deletes would have been committed.
Comparing the documentation for version 1.9.2 and version 2.0.0 of git-add shows that this behaviour changed in version 2.

To tell git to ignore deleted file(s) in the commit and commit all the other changes:
git add --ignore-removal .

Related

How to go back to the Working Directory that was not committed

I forgot to commit on the directory where I was working and then I checkout to the previous commit and now I want to back to the directory where I used to work. What can i do ?
If your checkout of the previous version somehow deleted your folder, which was not added/committed, then you would need to rely on a file recovery utility, as detailed in "Can I restore deleted files (undo a git clean -fdx)?", or on your IDE. Some of them keep a local history (IntelliJ IDEA, VSCode through an extension, ...): you might get back some of your files content that way.
In the end, it depends on the exact git checkout command you did, considering this command can update files and/or branches.
That is why it is recommanded to use git switch (branch only) or git restore (files only), instead of git checkout.

Moved local git folder and now its asking to stage most of my project files?

I reinstalled my OS (KDE neon) and I thought why not backup my project git folder, before I reinstalled I made sure I commited any code and everything in my git project folder was up-to-date.
So after reinstalling, I moved my project back onto my PC from my backup drive and ran git-status and it wants to stage nearly all of my project?
What do I do? I don't want to commit all these files again when I have not editing any of them? Its nearly 800 files it wants to stage?
You could reset your project using:
git reset --hard branch_name
This would lose any unstaged files.
Or you could stash the changes:
git stash save 'msg'
This works like the previous one, but instead it saves the unstaged files in a separate "memory" (in case you need them later on).
Now, I may assume that the reason you got that situation in the first place is maybe when you restored your data back from the drive, the OS changed some properties on those files, stuff like creation/modification dates, permissions...etc. While the content of the files has not changed but the properties are. Still, git sees them as modified files.

Handling line endings for git

Here is what I messed up so far,
I copied (not cloned) a rails project from windows to linux pc
Then created a new branch and made lots of changes
At last commited twice whole project and pushed it to a remote repository with windows line endings.
Problem: so my problem is that, since I copied the project from windows to linux, when I run git status whole project shows modified. I don't see the changes I made specifically. Since I committed and pushed whole project, I lost history of files change.
What is needed: So I want to remove my last two commit but I want to keep my changes that I made. Then I want to convert the line endings from windows to unix of whole project so that when I run git status I see only the files that I changed, not whole project. And then I want to commit and push to remote.
It would be nice if there is a solution for this mess.
I am not sure if this will be helpful, I am working on a rails project and my IDE is rubymine.
To undo your last two commits run this:
git reset --soft HEAD~2. This will put the files involved in the commits into your working directory.
Run git status and they will show as staged changes (ready for commit).
Edit your files.
Stage and commit your changes. Important: Don't push
Run git pull.
Run git push
Make sure you commit your changes and do a git pull prior to git push, otherwise your attempt to push will cause conflict and will be rejected.
You can use git filter-branch.
Similar question here.

Git checkout untracked issue

I'm collaborating with a few other people on a Drupal website which we are version controlling Git. We setup a local Git repository containing our commits.
After a colleague pushed some updates and I fetched and merged into my local dev branch, I began experiencing the following problems:
user#server:/var/www/Intranet/sites/intranet/modules/custom$ git checkout dev
error: The following untracked working tree files would be overwritten by checkout:
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix-bryant-flipped.png
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix-bryant-flipped.tpl.php
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix_bryant_flipped.inc
Please move or remove them before you can switch branches.
Aborting
The issue above typically shows up when I try to checkout into other branches which fails and I am effectively trapped in my current branch.
Referring to this question, there is a suggestion my issue is related to the gitignore file. However, my .gitignore file has nothing indicating any part of my themes directory should be ignored as the following shows:
# .gitignore for a standard Drupal 7 build based in the sites subdirectory.
# Drupal
files
settings.php
settings.*.php
# Sass.
.sass-cache
# Composer
vendor/
# Migrate sourec files
modules/custom/haringeygovuk_migrate/source_data
As mentioned above, my attempts to execute git checkout into any branch fails with the message above. I decided to force it with the -f switch and successfully switched into my target branch but I lost a couple of hundred lines of code - which I'd love to avoid going forward.
I work on a Linux-Ubuntu VirtualBox which my colleagues prefer working in a WAMP setup and use the Git Bash terminal emulator for executing the Git commands. Could the difference in environments be causing these serious issues?
How can I resolve this issue?
Well, the situation is rather simple. You, in your current branch, don't have certain files under the control of Git, but at the same time, you have those files in your working tree. The branch you're trying to switch to, has those files, so git would need to override files in the working tree to perform checkout.
To prevent possible data loss, Git stops the process of switching the branches and notifies you that you should either add those files under the control of Git in a separate commit in your current branch, and only then perform the switch, or simply remove those files from the git way.
Likely you have chosen the second way. Generally you should "force" any operation only if you really understand what you're doing.

Ignoring files in Git after they have been committed

I have recently switched to git.
In my previous and first commit since using git, I noticed it also listed the pyc files during the commit. I didn't think anything about it and commited and pushed them.
Now I realize they keep getting updated during the development and its very annoying seeing them in the list. It just produces too much noise.
I did some research and did echo "*.pyc" >> .gitignore in the project directory.
This didn't help though as the pyc files are still being shown. Could it be that because I have previously committed those pyc files I can no longer ignore them? (since they are tracked now and the state has changed again) If so I am damned forever or is there still hope to delete the files in the hindsight from the repository?
Thanks
Just git rm the .pyc files, and make sure your .gitignore is set to ignore them from now on. You are correct that git will not ignore the committed files because they've already been added. If you are keen about not deleting the off the disk, then use git rm --cached to delete the files. That just deletes them from the index without deleting from the disk.

Resources