how to undo git pull or git push?

I use often git but I have not a very deep understanding of the details under the hood and of the "git language"...

I have two directories: one on my pc one on my laptop. Both contain a .git repository (that is not bare !).

My laptop connects via ssh to my pc and from the laptop I push to the pc (having added a ssh remote).

Because I cannot push to a branch that is checked out (on the pc from the laptop), I have on the pc two branches (master and laptop-master). So I push and then on the pc I merge the branches.

In some way I have messed up the right order and I have conflicts.

I am trying with git log, git diff (and gitk) to clean things up.

For undoing a merge I had used

 git reset --merge

Is this command dangerous ?

How can I undo a pull or a push from/to a remote ?

Do I need a "central" repository for avoiding conflicts (I think this means a bare repository) ?

Why it is not possible to push to a branch on a remote that is checked out ? Can I force this in some not-dangerous way ?

Every hint is good... thanks

Answers


To undo a push to the remote repository:

git push -f origin HEAD^:master

this command will rewind the remote repository back on one commit.

To undo a pull, you can do this:

git reset --hard {hash of the last commit before pull}

Having a central bare repository will be helpful, but... it's just another git repository where you can push to and pull from;


  • It is not dangerous to do a git reset --merge.

  • A git pull can be "undone" by doing a git reset --hard to the commit before the pull was done.

  • A git push can be "undone" by doing git reset to the commit you actually want in your local repo and then force push it by doing git push -f

  • A central repository would be a more standard setup.

Why it is not possible to push to a branch on a remote that is checked out?

Because it would normally be confusing to get changes into your repository by some external repo pushing changes into it.

Can I force this in some not-dangerous way?

You can configure git to allow it by issuing this command in that repo (your pc repo as I understand it):

git config receive.denyCurrentBranch warn

Then you can push changes to the checked out branch. However, when you do switch to the pc repo, you will have a confusing state, since those changes will not be reflected in the working tree. It will actually look like you had undone those changes (because they are in the repo but not in the working tree).

You can get the pushed changes by a git reset --hard, but that will throw away any local changes you have. If you have local changes, you will have to manually restore each file that has been pushed by using git reset HEAD <file> and git checkout -- <file>

Here is an example where I set the config, switch to laptop, make a change, push that change, switch to pc, and reset that repo to get the change in the working tree:

C:\git\pc_repo [master]> git config receive.denyCurrentBranch warn
C:\git\pc_repo [master]> cd ..
C:\git> cd .\laptop_repo
C:\git\laptop_repo [master]> echo hello >> text.txt
C:\git\laptop_repo [master]> git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   text.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
C:\git\laptop_repo [master +0 ~1 -0]> git commit -am "modified text"
[master 4925c26] modified text
 1 file changed, 2 insertions(+), 1 deletion(-)
C:\git\laptop_repo [master]> git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: warning: updating the current branch
To C:/git/.\pc_repo\.git
   eb4f07f..4925c26  master -> master
C:\git\laptop_repo [master]> cd ..
C:\git> cd .\pc_repo
C:\git\pc_repo [master +0 ~1 -0]> git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   text.txt
#
C:\git\pc_repo [master +0 ~1 -0]> git reset --hard
HEAD is now at 4925c26 modified text
C:\git\pc_repo [master]> git status
# On branch master
nothing to commit, working directory clean
C:\git\pc_repo [master]>

Need Your Help

what does the $ld$add$os10.4$ prefix mean when I use nm to look at a symbol list on a Mac?

ios symbols undefined-symbol nm

I'm having some problems with my iPhone app not linking for the iPhone Simulator. It tells me there are undefined symbols: