GIT: fatal: ambiguous argument 'FETCH_HEAD': unknown revision or path not in the working tree - github

I'm working on branch of my git repository and I'm trying to reset all my changes using:
git reset --hard FETCH_HEAD
But I'm getting the following error:
fatal: ambiguous argument 'FETCH_HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Is the first I get this kind error reseting my changes on my repositories. Any of knows why or way around this error to reset my repository?
I'll really appreciate your help.

To overwrite any local changes and synchronize your repo to "origin", run these commands:
git fetch
git reset --hard origin
This is also useful when you rewrite git history with a rebase and git push -f to origin.
I use this when I'm working on changes on my dev system and building on a remote server. I don't necessarily want all of the little back and forth changes in my git history so I rebase and squash, but then a normal pull will not work on the remote server because the histories no longer align.

Related

git reset --hard origin/master still giving different code

hello guys i want to ask about my GITHUB problem, why is my local master branch is different from remote?, i was modifying without committing then git branch -b and git add and git commit, and then after i push the new branch, create pull request, and merged to master, its different when i comeback to local master and git pull
i also already do git fetch origin and git reset --hard origin/master
the code is almost the same as remote but its just weird, a few declaration is missing and one entity naming is the same as my other branch
it turns out, that there is open tab in VSCode that i havent saved from another branch, close it out and dont save it solved the problem

Having trouble pushing up my projects to GitHub

I am having difficulty pushing up my code to GitHub.
I have a GitHub account and have created the repo:
cd into project folder
git init
Result:
Reinitialized existing Git repository in /Users/blakeflowers/Documents/fake-news-app/.git/
git remote add origin https://github.com/devnoob-flowers/shit-they-say.git
Result:
fatal: remote origin already exists.
git add .
Results: seems to do nothing
git commit -m "Practice Push"
Results:
On branch master
nothing to commit, working tree clean
git push -u origin master
To https://github.com/devnoob-flowers/shit-they-say.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/devnoob-flowers/shit-they-say.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
So clearly I have no idea what the heck is going on.
Once again im trying to push my code up to GitHub and eventually deploy the site using Github pages. just something im doing for practice.
I solved it by finding some terminal code but I don't understand it. Can anyone explain?
This solved my problem:
git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp

failed to push some ref to my remote repository

i am working on a project and i can't push my files from local machine to remote repository
it's showing error when i try to push my repository
****! [rejected] master -> master (fetch first)
error: failed to push some refs to ' .git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.****
after referring i also tried $git fetch origin and $ git merge origin YOUR_BRANCH_NAME then also it dosen't works
The solution to your problem has given in the instruction. You have to run "git pull" as the remote repository contains work that you do not have in the local repository.
Running "git fetch" won't be enough as it only fetch the changes to the local repository and doesn't merge with your codes in the staging area.
If for some reason you dont want to run "git pull", then you can try the alternative commands like following:
git fetch
git merge FETCH_HEAD
git actually runs the above two commands when you run "git pull". Here FETCH_HEAD is the the reference to the codes you just fetched in the local repository.
Let me know if you don't understand any part.

How to revert / undo my last two pushes (commit and revert) in github?

I got a Github repository from client where there is no code.
I cloned this repository and pasted my code folder to this repository and pushed the code to git.
But now I need to revert back my commit. For this I did the following steps:
git revert xxxxxxxxxxxxxxxx(commit)
git add .
git commit -a -m "reverted back"
git push origin master
After this I went to my github page, there I find project folder with app/views, config, db, log, public, tmp folders where revert is not properly done.
How to undo my last 2 commits (both commit and revert) and get back to the main repository as it is?
Please help me, I am worried.
The simplest way is just to hard reset to commit preceding the bad one:
git reset --hard xxxxxxxxxxxxxxxx^
git push -f origin <your-branch-name>
There xxxxxxxxxxxxxxxx is your sha, that you reverted in the first time.
Note the ^ before xxxxxxxxxxxxxxxx. It means that you reset to the commit before xxxxxxxxxxxxxxxx.
Or you can specify exact hash to reset to, i.e.
git reset --hard A
Before resetting your branch is looking like:
A -> xxxxxxxxxxxxxxxx -> B(bad)
After git reset --hard xxxxxxxxxxxxxxxx^
A
git push -f origin <your-branch-name> pushes your local changes made on the previous step to the Github server
Be careful with force pushing though, because you force your local repository state on the server. So you lose server state forever

git / github unresolved conflicts but no visible differences

I am using eclipse, git and github. A friend of mine forked my github repo and changed some code. There was a pull request, I accepted the changes to my github repo. Now I tried to commit my own files and I am not able to 'commit and push'. As you can see in my screenshot there are no visible differences in the file. What procedure will solve this conflict?
Assuming your friend's work got merged into master and you are on branch your-feature-branch then:
git checkout master
git pull
git checkout your-feature-branch
git rebase master
will attempt to rebase your work on top of master, which already includes your friend's changes. The rebase procedure will process your commits one at a time and inform you of any conflicts which you can then resolve. Each file with conflicts will have conflict markers that tell you the changes introduced by each branch. After resolving the conflicts stage your changes then do
git rebase --continue
Repeat until rebase finishes. If at any point you become confused you can always abandon the rebase with
git rebase --abort
Once your work is rebased push your-feature-branch to origin, open a pull request, then merge.
I got the same issue sometimes, without a visible reason. Using a git rm on the directory containing the bad file, followed by a git reset --hard solved the issue for me.
git rm --cached -r DIRECTORY
git reset --hard