get previous commit that got deleted on git repository - eclipse

Yesterday when I was at work, I was working on my project that i have on my home computer. The problem was when I tried cloning the repo it eclipse would crash. So instead I downloaded it manually and initialized the repo and then force pushed to my home computer. This deleted all the history and files in on the repo. I did some googling and tried reverting the commit but it the force push erased the commit history so I can only go back to the force push commit.
I have a computer at home which hasn't pulled the updates since this mistake and I was able to get its commit SHA. I used that on the web and found the last version its still there but I can't pull that commit because it got "deleted" and won't show in the repos history. Is there a way I can use the SHA from the last good commit and make that the master branch restoring my history?

When changing the HEAD (the working position in the git repository), it tracks your history in the reflog. Executing git reflog in the command line will show your complete history. You can read more about it on git-scm.com
When you have the desired commit SHA, you can do a git checkout with it. Executing git checkout [SHA1] will take you to the desired position in the git history. From there, you can make a new branch from it by running git checkout -b "new_branchname".
If you want to set your current branch to the commit, you can run git reset --hard [SHA1]. This would reset the current branch, but the previous state can however be found in the reflog.

I do NOT recommend doing this as general practice BUT...You should be able to force push from the other computer, thus resetting the repo to its former state. Providing you don't want to keep anything you did while making a giant mess of your git repo yesterday.

Since you know the right version you want to recover, it will make things easier.
In your local repo, use below commands:
git reset --hard <commit sha value>
git push -f
Note: if you execute the commands in the local not initialized (not cloned from remote), you should fetch first by git fetch --all, and then execute the two commands .
Now the version will go back to the latest as you need both locally and remotely.

Related

Git revert and reset : Files in the repository are not getting rolled back

I want my entire repository to roll back to a certain previous commit (that is 20 commits behind my latest commit).
I tried using git reset --hard <commit#> and git revert <commit#>
Both the git commands run successfully with the message - Head is now at <commithead>
But then when I see the code in my repo, I can still see the edits I made in my latest commits. How can I remove all the edits after the particular commit I am reverting to?
Am I missing out on some step here?
After your git reset --hard, use to be sure the git restore command
cd /path/to/repository
git restore -- .
That should restore (as its name implies) the working tree with what is in HEAD (and you just reset HEAD to the right commit)

Can't to push commits from local branch to github repository

I was committing and pushing in ordinary for my repository.
but once i used command of git checkout for change to the previous version of my repository.
after that i tried to commit and push, then it can not completed.
i try to use the --no-verify command to push the commit but it also not success.
error: failed to push some refs to 'https://github.com/ruwanliyanage123/Hair-4-U-Hospital.git'
i want to push my commit into github repository
Since you switched to a previous version of your repository, your head is most probably detached. You can't just go back anywhere in your history and make commits.
Consider making a branch from there and then commit to it.
Try to first check in which branch you are working do git branch, check is the one you are working. Then I sometimes do git pull to just make sure that the connection is working this should not delete your progress the you should be able to do git push. If you are afraid youll mess up first do a local back up of all project files except for the .git one which are hidden by default in windows.Lastly I would suggest never posting the actual link to your github repository in case whatever you are working is important, you can just replace with
https://github.com/user/projectname.git

How to recover lost changes locally

I have a project on GitHub. I made some changes to it and I wanted to apply git push but it showed some error. I googled for solution and someone said to do git stash git pull, so I did those two. In the end all my changes to the projects on my local machine are lost. It became the same project as on GitHub without all the changes.
Tried git fsck --lost-found but didn't work.
git stash saves you changes but resets the state to the last commit. To restore the saved (not commited) changes, simply run git stash apply. (Learn more in the docs.)

Deleting a commit in github

Is it possible to undo one or two faulty commits I have made to Github. I'm not very good at coding either so if there's a way to do it without coding, that would be great.
Thank you very much
You can do this via doing the following command in your local workspace:
git reset --merge <hash>
Where <hash> is the commit prior to the commits that you want to revert.
The above command is enough if you haven't pushed to GitHub. If you have pushed to GitHub, you then run the following to force an update of the remote branch:
git push --force origin <branch>
(Note: This is assuming that your remote is named origin, but rename as appropriate.)
Note that this re-writes history on the remote branch, and is not recommended if other developers are using the same branch. If you know who the developers are, you can communicate with them to delete their local copies of the modified branch and ask them to run git fetch to get a new copy of the branch.

Reverting to last commit in Xcode 4 using Git

We just moved over to Git from SVN. In trying to clean up some unused files. I saved before deleting one folder that I thought we weren't using. I did not push this to the origin. I realized we are using one of the files in the folder after all, and would like to revert to my last commit. This is on my own branch from the master. I can't find a way to do that in Xcode. Am I missing something? Thanks.
You can see here for rsanchezsaez answer: Xcode 4 git integration
Xcode 4 won't let you to checkout older commits within the user interface, unless you created a new branch for that commit. Nevertheless, you can do it from the command line. For that, use the following command from your project folder
$ git log --format=oneline
to get the hash code of the commit you want to go to, and then use:
$ git checkout desired-hash-code
to checkout that particular version. Once there, you can make tests, changes, and possibly create a new branch. If you do a commit without creating a new branch, you will lose the newer commits in your current branch. If you want to go back to the newest commit after having performed some tests on your older version use:
$ git checkout master
note again that this won't work if you do a new commit from your old code version without creating a new branch, because newer commits in the current branch get dereferenced.
Also, please consider searching SO before asking. Many questions had already been asked and answered.
From the command line, run a program called gitk - this will allow you to visualize the commits you currently have. Find the ID of the commit you want (e.g. the previous commit) and do the following on your branch:
git tag JustInCase
git reset --hard <commit ID>
Refresh gitk, and if you're happy with the results then delete the tag using:
git tag -d JustInCase
If you're not happy with it, just do:
git reset --hard JustInCase
git tag -d JustInCase
To visualize this for you:
1) Start
2) After tagging and resetting your branch to the previous commit.
3) After deleting the tag and doing Reload in gitk.