Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: commit before merge?

I was advised to commit all my changes before running git pull and merge. Does it make sense ? What if I run pull and merge before committing my changes and commit them after the merge?

like image 261
Michael Avatar asked Jan 04 '14 18:01

Michael


People also ask

Do I need to commit before I merge git?

If you try to merge changes on files that have modifications, git will just refuse to do it. That is why committing first is a good idea.

Do I need to commit after git merge?

If there were uncommitted worktree changes present when the merge started, git merge --abort will in some cases be unable to reconstruct these changes. It is therefore recommended to always commit or stash your changes before running git merge.

How do you checkout a commit before a merge?

You can do a git reset --hard [commit hash] to go back to the commit before the merge, and it's like traveling back in time. However, you'll also undo any changes that were added after the commit you specify in the reset command, if you have any.

Can you commit to an already merged branch?

You can continue working on your branch and then when you merge with master again, it will bring the commits that are missing on master.


1 Answers

Committing before pulling is not always advisable -- you should consider stashing your work instead.

A better way to think about this is in terms of your staging area. Ideally, you'd like it to be clean before you attempt to merge in remote changes (remember, git pull = git fetch + git merge).

git commit is one way of accomplishing this, but it will alter your history--thereby polluting it if you're concerned about leaving your repo in a constantly working state.

git stash, on the other hand, was built for use cases just like this.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory.

source: http://git-scm.com/docs/git-stash

You workflow would then look something like this:

$ git stash
$ git pull origin master
$ git stash pop stash@{0}

This will allow the pull to execute without issues, vocally warn you when applying your stash has resulted in conflicts.

like image 79
jonathan3692bf Avatar answered Oct 12 '22 06:10

jonathan3692bf