Git

Git Merge Without Auto Commit

The “git merge” is a Git command used to merge one branch to another. Most developers use the git merge command to integrate the branch’s commits into the main or master branch. When merging takes place, it will automatically be committed. However, sometimes the Developers want to merge the branch without the commit, and they think there is any way to merge the Git branch without auto-commit.

In this post, we will explain how to Git merge without auto-commit.

How to Git Merge Without Auto Commit?

To merge the Git branch without auto-commit, utilize the “git merge branch –no-commit” command by following the provided procedure.

Step 1: Open Git Terminal

From the Windows Startup menu, open the “Git Bash” Git terminal:

Step 2: Move to Git Repository

Utilize the “cd” command to move to the Git repository:

$ cd "C:\Git\Demo2"

Step 3: Create New Branch

Create a new branch by executing the “git branch <branch-name>” command:

$ git branch work

Step 4: Move to New Branch

Switch to the newly created branch through the provided command:

$ git checkout work

Step 5: Create New File

Use the “touch” command and create a new file:

$ touch File1.txt

Step 6: Add File to Tracking Index

Next, add the file to the tracking index using the below-mentioned command:

$ git add File1.txt

Verify the Git repository state by checking its status and ensure that the untracked changes are added to tracking an index or not:

$ git status

Step 7: Commit Changes

With the help of the “git commit” command, commit the tracked changes:

$ git commit -m "File1 is committed"

Check the repository log to confirm if the changes are committed or not:

$ git log

Step 8: Switch to Master Branch

Next, switch to the branch in which you want to merge the new branch using the “git checkout” command. For instance, we have opened a “master” branch:

$ git checkout master

Step 9: Merge Branch

Next, merge the Git branch without an auto-commit:

$ git merge work --no-commit --no-ff

Check the git log to verify whether the merge commit is generated or not:

$ git log

The output indicates that we have used the “git merge” command without auto-commit:

Bonus Tip: Remove Commit After Git Merge

To remove commits after Git merge operation without canceling the merge process, you can utilize the “git reset <head reference number>” command. For this purpose, check out the provided instructions.

Step 1: Merge Branch

First, merge the branch through the “git merge” command:

$ git merge work

Upon doing so, the text editor will appear on the screen. Edit the commit message and hit the “CTRL+S” key to save the message. Then, press the “CTRL+X” to exit the nano editor:

Check the repository logs to confirm whether the commit message is saved or not:

$ git log

The output shows that we have merged the branch with the commit:

Step 2: Remove Commit Only After Git Merge

Now, remove the commit message after the Git merge by using the “git reset” command:

$ git reset "@~1"

Again, verify the Git repository log to check if the commit message is removed or not:

$ git log

Here, you can see we have removed the commit only after the Git merge:

We have demonstrated how to use Git merge without auto-commit.

Conclusion

To use the “git merge” command without auto-commit to merge branches, first open the Git local repository. Switch to the branch in which you want to merge another branch through the “git checkout <branch-name>” command. After that, use the “git merge <branch-name> –no-commit” command to merge the branch without auto-commit. This post has elaborated on how to use the “git merge” without auto-commit.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.