1

The following is a very common error message encountered when we try to pull a git repository locally with some existing local changes in the local repository.

Please commit your changes or stash them before you merge. Aborting

I have a question about stdcall's answer in this post.

He has mentioned the following 3 options over there.

enter image description here

Question: If I just commit my local changes using the command git commit -m "Saving my local changes" Is git pull not going to throw the same error that I have mentioned above? I mean I don't need to do a merge (using git merge) or anything while pushing to gitlab?

In a normal workflow when I don't see any error, I follow the steps below to commit and push my changes:

  1. git add .
  2. git commit -m "My message"
  3. git push

Since I am getting above error, are the following steps correct:

  1. git commit -m "Saving my local changes'
  2. git pull // This will pull whatever is on the branch including my local changes
  3. git push //This will push my local changes to git

I have used git stash in the past and it has resulted in my local changes getting wiped out so I am scared of using it. I didn't do git stash pop though and probably that's the reason it behaved like that.

GoodDeeds
  • 7,956
  • 5
  • 34
  • 61
Tan
  • 1,433
  • 5
  • 27
  • 47

2 Answers2

0

git pull fetches the latest changes and performs a merge via a single command. From its docs:

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

So what you describe will work. First, your git commit will create a local commit of your changes. Then, git pull will get the new updates from the remote and merge it with your local changes, creating a new merge commit if necessary. Then git push will push all of it back to the remote.

GoodDeeds
  • 7,956
  • 5
  • 34
  • 61
0

git pull is throwing that error because the working directory is not clean for it to do a merge. Since, and I am oversimplifying it here, git pull is basically a fetch + merge, git needs a way to merge files. It can't merge if there are changes that are not saved/discarded/stashed.

This does not save you from conflicts - getting remote changes might cause local conflicts - remote changes touch upon files that you have touched thus causing conflicts. Thus, standard conflict resolution is needed. See this answer for more details.


I don't merge when I push

You don't but not every push you do is successful. Sometimes, there are changes in the remote you don't have. Then git will tell you "Hey! There are things in the remote that you don't have and I can't override just like that. Pull them so that you can resolve whatever you need to resolve so that you can then push cleanly". See, now git tells you to pull - hence merge - so that you can push (of course unless you push --force...)

mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • 1
    Thanks. So what I have described is not a recommended approach you would say? – Tan Apr 20 '21 at 17:36
  • It's perfectly valid. I just linked to an another answer since there it's discussed the possibility of `git pull --rebase`. There is nothing wrong with committing and pulling. – mnestorov Apr 20 '21 at 17:38
  • Ok, that worked. However, in one of the file - properties.js, after git pull , my IDE was asking whether I should accept incoming merge or not with some <<<<< symbols. So I clicked on it and it showed the exact changes that I was pulling from the git. – Tan Apr 20 '21 at 18:03
  • This is expected for a so called [git conflict](https://www.atlassian.com/git/tutorials/using-branches/merge-conflicts). Your IDE probably tries to make your life easier to resolve such conflicts by just clicking on what you want to save and what to discard. – mnestorov Apr 20 '21 at 18:05