Situational guide to Git for new engineers - undo commit without losing changes

Situation: You made a local commit and want to push your branch to remote but remote tells you not in sync

There is a situation you have your local and remote branch in sync and you assume that your current change can be committed and pushed to remote. But you get a surprise, your colleague made a commit to your your branch and now your local version is 1 commit behind the remote and you already made a local commit.

Undo last commit command:

git reset --soft HEAD

What does command do

Reset is the ultimate reset changes command and it comes with a number of modes. We are using a soft mode as it allows us to keep our most recent local commit. When this command executes you will return local branch to the state.

HEAD is an alias for the most recent commit in the current branch.

Now you have options of what to do next, you can stash your changes and do a git pull to get the most recent remote version of the branch and then reapply your current changes and repeat your commit. You might need to deal with merge conflicts too.