Handling line endings for git - linux

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.

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.

Git: Stashed Changes But Still Can't Pull

I have looked all over and followed several different sets of instructions on this but none seems to work for me.
I have a GitHub repository and I have made some changes on my local machine and some on my Linux server and was hoping to merge the two.
In the past, I have used the git stash command and then been able to pull and then pop the stash to merge. I have never done this on Linux but I assumed it would the same as windows. However, even though I have stashed my changes on the server I am still unable to pull and receive this error:
error: Your local changes to the following files would be overwritten by merge:
aFolder/aDifferentFolder/aFile.py
So naturally I checked to see if my stash had gone through using git stash show and I got confirmation that the stash existed because the top line showed this:
aFolder/aDifferentFolder/aFile.py | 2110 +++++++++++++++++++++++++++++++++++++++----------------------------------------
This is very strange. But decided that I didn't even want my changes on the server that much anyway so I decided to delete them using git reset --hard and then tried to pull and was met with the same error!
So in summary a list of things I have tried:
git stash
git reset --hard
git clean -f
git add . && git stash
git reset aFolder/aDifferentFolder/aFile.py
EDIT: Just to add more info here is the message I get upon calling stash:
Saved working directory and index state WIP on master: 1d49f3a Merge branch 'master' of https://github.com/username/repository
HEAD is now at 1d49f3a Merge branch 'master' of https://github.com/username/repository
I never found out why this happened but a solution that worked for me was creating a new branch pushing to that and then merging with the master branch. Afterwards I deleted the useless branch.

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.

Failsafe Automated Git Update/Add/Commit

I have a website that I need to commit system generated files and folders to an existing git repository via linux command line. I have an automated process that monitors a folder for new bash scripts and runs them. The website creates the scripts and saves them to the folder.
I keep getting issues where something has changed on either the remote repo or my local one that is stopping git from completing the following commands
git pull --rebase origin
git add [repo path to updated file(s)]/*
git commit -m "commit message"
git push master
I need to bullet proof this process so that it will just run and I can forget about it. Right now, permissions issues on files pulled down, or merge conflicts, etc... keep getting the repo out of sync. How can I bullet proof the commands above so they will pull down any remote changes and then commit any new ones necessary?

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.

Resources