How to Merge Without Commit in Git
Git is one of the most common systems in modern programming. It is used by almost all companies that develop and sell professional software, as it makes tracking code changes that much easier.
While using Git, you simply cannot skip over the Git Merge and Commit commands. Git Merge allows you to attach the code that you’ve been working on to the entire branch. It is commonly followed by the Commit command to save the changes.
But can you use Merge without the Commit command? This article will give you the answer.
NOTE: If you want to follow this tutorial you must have at least some background in programming and should be familiar with Git’s basic elements.
First Things First
Before we jump into the main part of the article, let’s clarify the key concepts that we need for this tutorial. We’ll start with Git Merge.
Git Merge is a command that joins two or more development histories together. In other words, this command incorporates changes from the new commits into the branch that’s already stored in your Git repository.
The basic syntax that you can use for this command looks like this:
git merge [-n] [–stat] [–no-commit] [–squash]
[-s <strategy>] [-X <strategy-option>]
[–[no-]rerere-autoupdate] [-m <msg>] <commit>…
git merge <msg> HEAD <commit>…
git merge –abort
The Git Merge command is mostly used through Git Pull, which is another Git command that incorporates changes from a different repository. You can execute the command manually when you need to merge changes from one branch to another one.
The basic syntax mostly used for this command looks like this:
git commit -m <message> //sets a commit message
git commit -a //includes all changed files in the current commit
git commit –amend // rewrites the last commit
On the other hand, the Git Commit command serves to save all of the changes that you’ve made, to the local repository.
We’ll illustrate this with an example.
Let’s say that you’ve created a new branch feature that’s simply called “new-feature”. The feature will be branched out from the master branch. You will then push a commit named “Finished with the new feature.” At the very same time that you’ve pushed your commit, a colleague of yours also pushed their own called “Colleague’s feature.”
If you then create a pull request, through the Pull method mentioned above, and your branch gets merged, you’ll be able to see a new merge commit called “Merge Branch new-feature” in your console.
Since you’ve been working on GitHub, for example, you’ll check its commit history. The user interface will show you all of the commits ordered by the time they were pushed. So, everything is linear.
That means that if multiple people merge multiple branches of the project, they could get mixed up and tangled together. That’s why some developer teams avoid using merge and pull requests by all means necessary.
To avoid mixing up branches and losing important parts of your project, it is advised that everyone pushes directly to the master branch. That will maintain a linear history while keeping the branches unique. But is it possible to develop in branches and merge them together without using the Commit command?
Using Git Merge Without Commit
If you type the man command next to Git Merge (man git merge) in your command prompt, you will open the HELP page for that specific command. That’s where you can read its documentation and all additional arguments that you can later use to further customize its operations.
Judging by the documentation, Git Merge calls the Commit command by default. Commit has the autocommit attribute in Git Merge which automatically enables it as soon as Merge is executed.
On the same HELP page, you’ll also see the following message:
With –no-commit perform the merge but pretend the merge failed and do not autocommit,
to give the user a chance to inspect and further tweak the merge result before
What that means is that you can add an additional argument to your Git Merge command (–no commit) to stop the command from automatically activating Git Commit.
Your command prompt line should look something like this:
$> ~/git/testrepo$ git merge –no-commit v1.0
Of course, depending on your user name and project, the left side of the command will differ, but everything right of “Git Merge” should stay the same.
A common problem that might interfere with this type of Git Merge is called Fast Forward. Check the output once you’ve hit Enter on the previous command and if the message says Fast Forward, change the command line into this: git merge v1.0 –no-commit –no-ff
The –no-ff argument prevents Fast Forwarding and executes the command how you want it.
Good Luck With Your Future Projects
Learning Git takes time, but the basics are not that complicated. If you’re looking to up your Git game, you can start looking for ways to streamline your work and save time. Cutting the Commit command is a good place to start.
If you need more information on certain commands or arguments, check GitHub’s full documentation.
Have you managed to execute Git Merge without committing? Did you get the Fast Forward message? Tell us in the comments below.