Do I have to commit my changes before pulling in Git?
You can do a commit anytime you want - all your commits are local.
It's only when you need to push
to the server that you need to have its latest copy. So it's a good idea that you always pull
from the remote repository before you push
your changes.
So, I think what you mean is "Should I push changes before issuing git pull or should I do git pull and then do git push". You should ideally pull
before you push
which adheres to the basic idea of adding code to the most recent copy of the public repository.
You might be notified of some merge conflicts obtained by merging the public repository, which you need to resolve before you can finally push your changes.
Since git doesn't let you merge when you have uncommitted changes you need to address those changes before doing a git pull (which does a fetch then merge).
You can either
checkout to erase those changes
make a commit to save them
my preferred halfway approach - git stash
By doing a git stash
first, you can then pull (fetch and merge) in the changes, then git stash pop
to apply your changes back in. If there are conflicts, the stash isn't dropped, and you get a chance to resolve those conflicts.