0

This might sound like a bit of a question that gives the impression of me being too lazy to do my own research, but I've actually failed to find resources that assist in my issue.

I'm currently working with two Git clones from the same repository that were each cloned on my personal laptop and a computer I use at work. Sometimes when I push to the repository from my laptop, I forget to pull on my work computer and later when I try to commit changes from my work computer to the repository I get an error message telling me to perform git pull first. Is there any way that I can undo the pushed changes in this case that were made from my laptop?

Thanks in advance.

3
  • 1
    You can undo what you pushed from your laptop from your laptop, by git reset, and then git push --force to rewrite the history on remote. But in the end it's not recommended. Git tells you to git pull from your work computer, then just do it. What's wrong with git pull? It's trivial to do, far more simple and safer than --force stuff. Commented Dec 24, 2019 at 10:28
  • 1
    I mostly agree with @LoiNguyenHuynh on his assessment (+1). Let's also note that, if you're working alone on the repo, since the work you earlier pushed from your laptop is still on your laptop's local, an alternative would be to indeed --force your push, and later pull/push from the laptop. Then again, I find Loi's advice to be the common sense answer, you should consider it first. Commented Dec 24, 2019 at 10:44
  • I agree that if I were working with others then this would be a problem, but thankfully this isn't the case and I'm working alone on this particular project. The reason why I'm so reluctant to use git pull is because the changes that I had made on my work computer were quite a lot and I'm afraid that pulling would reset my work. Would that be the case? Commented Dec 24, 2019 at 13:37

1 Answer 1

2

You are doing nothing wrong. You don't have to undo the commits if they are needed. It's quite common in collaborative programming to encounter such errors. Git complains just because by default it does not allow a branch to be updated by a diverged branch. Say we have 2 branches, A and B. When A has one or more commits new to B and at the same time B has one or more commits new to A, they are diverged.

To avoid this error, run git pull as the log suggests. But in your case the better way is to use git pull -r or more specifically git pull origin -r <branch>. git pull is equivalent to git fetch && git merge while adding -r or --rebase makes it git fetch && git rebase. With rebase, you can avoid redundant merge commits and keep the branch history clean and neat.

Of course, it's also a solution to use git push --force to undo the commits. But this way, you have to make them again if you actually need their changes. It just costs you more time and energy.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.