How to recover replaced files in local Git repository after pull request? - github

Originally on my Github are some outdated styles.css and JavaScript files and others.
I was working on my project last night but didn't push it to Github or back it up. It was just saved in my local repository using a series of local, unpushed commits.
Being a newbie that I am, I did a git pull request master, and all the files in my local repository got replaced with the original styles.css and JavaScript that was in my github.
Is there a way to get those files back?
I did a git reset head#{2} where I believe the state of the repository before the pull request was, and it showed some unstaged files.
In Gitshell my command line has "master [+10 ~19 -42 !]" with the text master being yellow.
At this point, what do I do? Currently I seem to have lost a lot of work.

If you have performed commits often you can pretty much get to any of them.
Use git reflog first (https://git-scm.com/docs/git-reflog). It will show you all the interim commits you've made. Once you find a relevant one you can do git reset #commit_id.

Related

How to merge local folder and its structure to github?

I am still a beginner and I locally made a folder of my Project and uploaded it as a repository in GitHub using GitHubDesktop. Now I changed the structure of the files (deleted some sub-folders and moved files here and there locally). Now I don't see those changes in my GitHubDesktopas something which I can push to the GitHub online and reflect the changes there.
I am not sure how to achieve that:
(One way I can think is to reclone and download the online GitHub repository to local, move the changed files again and then commit from GitHubDesktop) , but I am not sure if it will work because I did the almost the same thing just without recloning: but after the reorganization, the changes are not appearing in my GitHubDekstop which I can commit.
Any advice would be great. Thank you.
This is my repository (https://github.com/grammilo/Codes) whose organization I want to copy from a local folder which goes by the same name and was previously cloned from it.
When you go to the directory of the repository in your terminal, checkout your master branch
git checkout master
and use
git diff
this will display any changes made to the files and file structure in green and red. You can delete folders and files directly from your file system or from the terminal, whichever you prefer and these changes will display when you use git diff. After your changes are made use
git add
this will stage your changes to be committed. You can use
git status
to make sure your changes are ones you would like to make.
Use
git pull
to pull any changes from your repository and when you are ready to commit your changes simply use
git push origin master
this will commit your changes to the remote repository on GitHub from your command line without using GitHub Desktop. Check out the git documentation for more in depth explanations but that should achieve what you're looking for.

What should I do when my local branch is at merging state after I pull the remote master?

I am an Android developer, and now I am doing a lot of changes locally and are trying to synchronize the local and github side of the code after I pull the merge to merge successfully and merge the code locally and run successfully.
But when I want to push always show the picture shown in the picture, push rejected. I am very puzzled, the left side of the figure is the local change, since I was a novice, no previous to ignore the file, so a series of build files, .idea files have been submitted to see the error is to show that these files are not merged.
Now I am very puzzled, how can I solve this situation, how can I submit the local code to my github library, who can help me? Many thanks!
Screenshot:
You have commits in the remote version of master that you're pushing to that are not in your local version, and vice versa. Since Git can't resolve this diversion automatically, you can't push your local version to your remote.
You may have to git pull, or if that doesn't work, git rebase, to get your branches to line up in a way that they can be merged.

Stopped Using Git - Now Want to Start Again. How to Get Synced Back Up?

We had a number of developers working on a large website project using Git. We have a GitHub repository and then we have the website on the server, plus all the developers have their local versions.
When we finally launched the project, I got lazy (hangs head in shame) and started making changes directly to the server, without pushing them back to the Github repo. However, other people made changes to the repo, for reasons I don't quite understand, that were never pushed down to the server and are now either outdated or wrong. We have been doing this for almost seven months.
Now the server and repo are hopelessly out of sync. I would now like to get the most updated version of the site (which is the server) back up to the Git repository so we can begin another round of development. I basically want to start with a fresh copy of what is on the server.
How would you recommend I proceed? That was the first time I had used Git. It didn't seem like such a big deal at the time but now seems like it is harder to start up again than I thought.
I have looked for instructions and don't really see anything that fits. Because I am not super confident in my Git skills, I am afraid to just start trying the few ideas I did find and losing what I have on the server.
(I know I could restore from a backup if I really messed it up but would prefer not to do that as it would take the site down.)
Can I uninstall git and start again with a fresh repo? Or is there a safe way to push the current version up to the repo?
Thanks for your help.
UPDATE: I found this answer elsewhere (Replace GitHub repo while preserving issues, wiki, etc) but I am not sure how to do this:
cd into "new" repository
git remote add origin git#github.com:myusername/myrepository (replacing myusername & myrepository accordingly)
git push --force origin master
Possibly delete other remote branches and push new ones.
Not sure what they mean by "new repository"
Make a new branch and push it to GH.
Make a new branch based on the previous
branch.
Switch to the new branch (created on #2).
Delete all the files and folders on this branch repository
except the .git folder and contains
(maintain the README.md,
.gitignore and other files if you want it).
Copy all the files from the server except
.git folder.
Commit.
Switch to local Master (created on #1)
Merge this new branch with the previous one.
Solve conflicts
(I use SmartGITthat have a visual conflict solver and helps me a lot, but you can use gitdiffif you don't want a visual interface)
Commit
Push it to GH.
I hope this helps
I figured this out. What I did was:
Make a new branch on Github to effectively store a backup.
$ git add . to stage all changes
$ git commit -m "Commit message" to commit changes
$ git push --force origin master to force changes from server to remote branch master
Once I did this, there were still hundreds of files I had deleted on the server that were not reflected on the remote github.com repository. I used the following:
$ git rm $(git ls-files --deleted)
See Removing multiple files from a Git repo that have already been deleted from disk
Then repeated git commit and git push. Now my github repo matches my server exactly.
I have not yet deleted the "backup" branch I created on github but I will.
Hope that helps someone.

EGit Commit to Remote Branch without Local Branch

I had a branch I was working on for a long time on my desktop. First I commit changes to my desktop's local branch, then push them to the remote branch and everything is dandy.
This morning, I was working on my laptop and selected the remote branch to change. The image below shows what my EGit repository window looked like on my laptop, except there was no local "MethodMigration" branch (there was a local master though). The remote "MethodMigration" branch had the little black check-mark next to it telling me that I'm viewing it as shown in the picture.
https://docs.google.com/file/d/0B7yGmb99B5enZm5OcTcydnEwLUE/edit (Sorry for the ghetto google drive link. I couldn't figure out how to get an image link)
Anyhow, I started adding a whole bunch of stuff. After I was done, I did a "Team->Commit->Commit & Push" after giving it a message. At that point, I remembered that I hadn't made a local branch for it yet... It told me "Nothing to push" even though a whole bunch of stuff had just been specified (which kind of makes sense since I never cloned the remote branch to make a local copy in the first place). The bad part is that all of my code was reverted back to the original "MethodMigration" remote branch code. Additionally, BitBucket shows no commits or anything.
Where did all my changed code go? Is there a way to get it back?
No worries!
You can use git reflog to find the commit id SHA of the lost commit. After that simply merge it with your local branch:
git merge <commit-id>

Git completely replaced changes in history. How to restore files?

I have git repository on bitbucket. I use it from Eclipse for a while, but then copy Eclipse project with local git repository to another place on the filesystem and work locally for 3 weeks. I'm a newbie with git, so I just copy the project as is.
Yesterday I try to push local changes and then switch to the remote branch. Now I see in git-gui only old bitbucket commits and no new files. I search in git-gui everywhere. But the size of the repository tells me that my new files are still there (on bitbucket 16mb, locally >300mb).
git branch shows me 2 branches (master and * (no branch)) and both have bitbucket commits. I didn't make any branches, I just first cloned the repo from bitbucket, then made commits, then made some actions trying to push the copied repo and then change branch. Any idea of how can I restore my local files will be greatly appreciated!
I'm really having a hard time understanding what the exact situation is.
However, if you say all the files are still in the local repo, a simple:
git reflog
is all you need to see recent commits on all branches. Find the right commit you want to go back to, and take it from there.