How to revert back to a fresh branch on git (removing all uncommitted changes) - eclipse

How can I revert/delete all my changes that I did manually on local.
Example I have 2 branches
1) master branch
2) branch2
I checked out branch 2 and started to do some changes locally. I accidentally replaced a word for all the files. So now all my files are unsynced.
I want to start fresh again. (What ever I have in branch 2 that was committed and pushed) and remove all my local changes.
I've tried
git pull branch2
git reset --hard
git --hard branch2
git checkout .
But my recent changes are still existing in my local. Is there a command to revert this or do I have to clone the branch and start from there?
PS all my local changes were not committed at all.
Thank you

You could do a git stash to temporarily store the changes you made and don't want to commit. And then do a git stash pop later to get them back. If you never want to see those changes ever again, then you can overwrite your local changes with a
git reset --hard
git pull
You said you didn't commit files so there are probably untracked local files that need to be removed. I would run a git clean -f . That'll remove the untracked files. If you also need to remove untracked directories (folders) you can do a git clean -df
Hope that helps!

This will vanish all the changes done on local and pull down the last committed changes on your branch.
git reset .
git checkout .
git checkout branch2
git fetch && git reset --hard origin/branch2

Related

How can i do git rebase after git pull request?

I have a trouble git rebase.
because, I had pushed my local code to origin dev. And, I send a pull request to 'upstream'.
But, There are 1 PR and 7 commits like below the picture
I want to 7commits squash to 1 commit.
What shall I do?
( I have tried : 'git rebase -i HEAD~7', but I have met the message that 'error: cannot rebase: You have unstaged changes.' )
As per my comment, you must commit, stash, or discard all your changes before rebasing. If your changes are complete, you most likely want to commit them (git add --all followed by git commit).
If they are not complete but you want to continue working on them later, use git stash to save your unfinished changes while also resetting to HEAD. Perform your rebase and then recover these changes with git stash pop.
To instead discard all your changes, git reset --hard HEAD will do the trick; reset to unstage your changes, --hard to discard them, and HEAD to specify the commit to which you are resetting. You should then be able to rebase.
It's worth noting that a stash or reset will not affect untracked files. To have these files included, you must first stage them with git add.

Github file removal after push

I accidentally pushed a file to my Github repository, and I am trying to understand where it is.
I have read many posts in here, but none of them had the solution, so I wanted to ask.
My steps were:
git status
git add normal_file.txt
git add sensitive_content_file.txt
git rm --cached sensitive_content_file.txt
#It was the time that I realized I have added a wrong file and need to remove it.
git commit -m "new changes"
git push
I saw that it pushed the "normal_file.txt " into my repository however I have not seen the "sensitive_content_file.txt" in my repository, so I assume it is not being pushed? So that all is okay?
I have tried to clean cache but now my repository complains as:
" Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively."
However, I did not do any changes to itself.
How can I clear every connection to this file with I tried to remove its caches?
git rm --cached sensitive_content_file.txt before the commit was enough to not pushing it to GitHub.
But now, it would be best to add that file to your .gitignore, in order to be sure to not add it again by mistake:
echo sensitive_content_file.txt>>.gitignore
git add .gitignore
git commit -m "Ingore sensitive_content_file.txt"
git push
If you did any rebase/commit --amend, that would change the local history compared to what was pushed.
If you have not done any local modification, a git reset --hard origin/master would reset master, then you can modify the .gitignore and push.

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

Git branch and local changes

Lets say I do a git checkout -b my_new_branch. Making some changes to my local files and after this I add all files with git add. and commit it and push. After this i realize that my branch is messed up and I want to delete it.
So Im going back to my master with git checkout master and delete the branch with git branch -D my_new_branch.
Will all local changes be reversed?
Once you checkout the master branch, all of the changes that you made on my_new_branch will no longer be in your working directory. They will still be on your branch and on the server.
Once you delete my_new_branch, the ref to that branch will be deleted. The commits will still exist in your local repository until a garbage collection is run. The commits and branch will also still exist on the remote server.