Reputation: 3385
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.
Upvotes: 0
Views: 60
Reputation: 30868
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.
Upvotes: 2