how can i revert git pull, how to bring repos to old state - github

1347
Is there any way to revert or undo git pull so that my source/repos will come to old state that was before doing git pull ? I want to do this because it merged some files which I didn't want to do so, but only merge other remaining files. So, I want to get those files back, is that possible?

You can do git reset --hard HEAD~1 to revert to the commit before the merge commit (assuming that's the latest). If you've made additional commits after the merge one, you would do git reset --hard HEAD~n where n is the number of commits after the merge + 1.

Related

Accidentally "Merged remote-tracking branch 'origin/x' into x", how can I remove this completely from my repository?

I accidentally made the same commit twice, and then somehow merged them and now there's this commit called "Merge remote-tracking branch 'origin/lab1' into lab1" Also my local repository is apparently not synced, this seems simple but I'm stuck
Tried squashing commits into one on github desktop, but it says "Unable to squash. Squashing replays all commits up to the last one required for the squash. A merge commit cannot exist among those commits."
I know how to fix this on my local repository, but how do I remove that merge commit from github?
If that merge commit is the latest one on lab1, you can simply reset one commit earlier (make sure you don't have any work in progress):
git switch lab1
git reset --hard lab1~
git fetch
git rebase origin/lab1
git push
But you have done commits after that merge commit, then you need mark them and replay them:
git switch lab1
git branch <sha1 of the merge commit> m
git branch lab1 tmp
git reset --hard m~
git fetch
git rebase origin/lab1
git rebase --onto lab1 m tmp
git switch lab1
git merge tmp
git push

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.

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

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

GIT workspace synced in multiple system

GIT workspace synced in multiple system
Is it possible to have the same git workspace , in multiple system.
Say for example I have a workspace in my office system, I want to carry on the task in my personal laptop after coming home.
Any changes I make in the workspace, adding, deleting , editing java/class files.
will be reflected automatically to the other system
You can use git, Use branches and commit your code to the desired branch at the end of each day even if your code is not complete.
when you get home fetch the changes git fetch --all --prune and you can continue to work from the point you stopped at work.
When you done you can commit your changes with a simple commit or with
commit --ammend and once you done use git squash to align your commit log as you want it to be.
In order to do a git squash follow those steps:
// X is the number of commits you wish to squash, in your case 6
git rebase -i HEAD~X
Once you squash your commits - choose the s for squash = it will combine all the commits into a single commit.
You also have the --root flag in case you need it
try: git rebase -i --root
--root
Rebase all commits reachable from <branch>, instead of limiting them with
an <upstream>.
This allows you to rebase the root commit(s) on a branch.
When used with --onto, it will skip changes already contained in `<newbase>`
(instead of `<upstream>`) whereas without --onto it will operate on every
change. When used together with both --onto and --preserve-merges, all root
commits will be rewritten to have `<newbase>` as parent instead.`

Eclipse Git (EGit) - How to revert local master back to remote master after a foul merge & commit with a local branch

I have the following setup:
Remote origin/master (default branch)
Locally, I got the master and created another branch - NewBranch.
Someone in my team updated master on the remote. I was able to pull in all the changes that they made.
However, while merging I had conflicts. Unintentionally, instead of updating NewBranch with the conflicted result, I have updated the local master with NewBranch (because master was the one that was currently checked out-or "active" in Eclipse). Furthermore I committed this change (locally) to my local master branch...
I was able to switch to NewBranch and merge it with all the latest changes (so my Newbranch is perfectly the way I want it).
Now, I'd like the master to point to the same version as remote master does.
So that in the future I have a clean merge between my NewBranch and the Master
I've tried to "Reset" the master, but after performing a hard reset, the Hash Ids b/w the local master and remote master still do not match.
I also have References in my git eclipse and the reference of FETCH_HEAD is the one I'd like my master to revert to.
How can I do this using git in Eclipse?
Thanks in advance
I am guessing you are asking these 2 questions?
Now, I'd like the master to point to the same version as remote master
does
If I'm right follow this, if you master should point to remote master
git pull origin master
If your NewBranch should point to remote master
git pull origin NewBranch
I also have References in my git eclipse and the reference of
FETCH_HEAD is the one I'd like my master to revert to.
Please right-click on project in your eclipse--->go to team----> check the git options
These commands will help you to revert to a specific commit
git checkout master
git reset --hard e3f1e37
git push --force origin master
# Then to prove it (it won't print any diff)
git diff master..origin/master
Alternative
Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.
git revert ~specificCommit
But remember this after git revert, if you want to go back, you need to read this Git revert be careful
before you do it.
git revert 45ae34 e34526a #created two revert commits
git revert HEAD~2..HEAD #by taking ranges, it will revert last 2 commits
git revert -m 1 <merge_commit_sha> #this basically reverts a merge commit
# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 34e2w34 .
git commit #commit here
Docs:
git docs: undo merges