How to restore changes to local files - macos

I just made a HUGE mistake. (I obviously don't know enough about git)
I was trying to update my directory on remote. I created a dev branch then typed git fetch && git checkout dev. Why? Because BitBucket said so! (I know. I'm a f****** idiot). Well, now all of my files have either been removed or changed to what I had days ago. I just lost A LOT of work.
Please tell me how I can get my work back. PLEASE!
EDIT: I'm not trying to revert to a previous commit. I'm trying to revert my local files back to what I had moments ago before my git fetch && git checkout dev mistake.

Woohoo, I figured it out! Here's what I did:
git reflog
2fe6344 HEAD#{0}: checkout: moving from master to dev
f1cc9bc HEAD#{1}: commit (initial): camera doesnt work
git reset f1cc9bc
git checkout .
That's it! I honestly couldn't find this anywhere on SO and I'm usually pretty darn good at researching. If this answer already exists, I'm sorry. But hopefully it helps someone out there in a tough jam.

Related

How do I undo the most recent commits in Git?

I am not able to undo the commits in git. It shows technical errors while doing so.
I have tried to navigate to the folder for my git repo and expecting to solve the problem at the earliest.
This post has explained undoing commits previously, presuming the issue is sourced in your local repository.
Link: How do I undo the most recent local commits in Git?
command:
git revert HEAD
you can mention the commit ID to revert the commit, if mentioned HEAD it will revert the last commit

how do I restore to a previous version wherein the .env file is available [duplicate]

This question already has answers here:
How do I reset or revert a file to a specific revision?
(35 answers)
Closed 3 years ago.
Just a dumb question from just a newb in git, Messed up alot with git, but now I was wondering if how can I restore to a previous version in the application, since my .env environment configuration file is now gone in my current master branch, I did merge my other branch with this one. I knew I made my latest work on my chat-feature branch, pretty much the 100% of the application was there, since the chat part and the payment feature of the application was there but when I checkout there, its not even working, I was wondering how can I go back to that previous commit that I made. When I git log I want to be back to this specific version.
Restore + push --force
I don't like to recommend anything that requires --force but in your case it may be the cleanest solution. If you shared your code with others, don't follow this - you're changing history.
Let's make master be like chat-feature.
Make a copy of the repo (on your drive), just in case.
git checkout master
git checkout -b master_copy (optional, but let's store what we currently have in master as a new branch; this will make it easy to use it if rewind too much)
git checkout chat-feature
Check that this is what you want master to look like.
git log -n 1
Copy the commit id
git checkout master
git reset --hard <commit-id>
Double check that things work and that's what you want.
git push --force

'Discard all changes' has deleted pod files

I did a commit on friday, discard all changes on monday, now all my pod related files are in the trash. I realise I probably did something wrong, as I'm quite inexperienced with git etc, but now i really need to restore all my files.
Has anyone come across this before and know what to do?
Description
This should revert your home directory to state the state it was prior to those changes you refer to in original post.
Example
Run git reflog
Find HEAD#{ID} (Ex. HEAD#{1},HEAD#{2} etc.) prior to changes that you've made
Run git reset --hard HEAD#{ID} (ID is a number refering to a particular HEAD as described in point 2.
Reference
git reflog
Useful link
Recovering Lost Commits with Git Reflog and Reset
Alright, incase I ever do this accidentally again (I've done it before), this is what to do.
Basically, it's the pod files that went.
Run sudo gem install cocoapods
Add .xcodeproj/project.pbxproj file back, for some reason it was put in the trash when I selected 'Discard All Changes'
Run pod Init
Add details to that file, then run pod Install.

Fatal: not a git repository - after BSoD

I was committing and then my Windows ( .. :( ) machine crashed and presented me with the lovely and infamous Blue Screen of Death. I rebooted and went right back to work, only to discover that something's seriously wrong with my local git repo.
"Fatal: not a git repository"
... Well, that escalated quickly.
I've been reading up on this issue (also here on StackOverflow), but haven't found a solution yet. The things I did include:
git init (says within 0.1s: Reinitialized existing Git repository)
git status (gives same fatal error)
git push origin develop (same fatal error)
The .git folder is still there and looks normal (although I wouldn't directly spot any anomalies).
It looks like there's nothing I can do on the command line to revive my repository.
I have found this reference, but I have no idea how to "change HEAD to a valid ref/hash".
Is there anyone who knows where to go from here?
(I know this questions shows up multiple times on SO, but none of those problems have the same origin and their solutions don't work. Please be sure this question really is a duplicate if you mark it as such.)
(I'm the asker.)
Unfortunately I didn't have the time to solve this problem in time. The pressure in this project is on and I needed to continue. I decided to do a complete new checkout. Everything works fine again.
Lesson learned: never get a BSoD while committing.
If anyone comes across this in the future:
Ran into this issue today, BSoD followed by git no recognizing I was in a git repo.
In my case .git/HEAD had become just null characters which is why git didn't recognize the directory as a git repo. If this is also the case for you try running:
echo 'ref: refs/heads/main' >.git/HEAD
This is just overwriting the current contents of .git/HEAD with a valid head which can be the head to any branch head file listed in the .git/refs/heads directory. Once .git/HEAD is valid, git should instantly recognize that your in a git repo again.

Moved file location in Xcode 4, can't push to Github

Git newbie here. Using Xcode 4.3.2. Had to move my project file directory. Commit still works fine but when I do a git push, I get Everything up-to-date, which is incorrect.
How do I get back on track?
Thanks
Check also if you are not in a DETACHED HEAD mode.
That happens if you checkout a tag or a file (see git checkout illustration in gotgit):
That was the case for the XCode question "Git (no branch) in xcode"
When you moved the project directory, did you move the .git folder with it? An easy way to check is to run git log and see if all of your previous commits are still there.
If you moved the .git folder with the rest of the files, then your remotes would still be setup correctly.
If it really isn't hooked up for some weird reason, try running git remote show origin and check to make sure that the url is correct and everything.

Resources