How to work in Git interactive mode - unable to work with the text editor?

2.6k Views Asked by At

EDIT:

It turned out that it was the editor VIM that is hard to use and not the commands. I am posting a link to, how to exit out of VIM editor for everybody's reference:

How do I exit the Vim editor?


I know this is a pretty newbie question but everywhere I have read tutorials, they just say open in interactive mode, pick this, squash that and then save and exit. I am unable to do any of these things. Can anyone explain in laymen terms, how to proceed about this?

BTW, I am talking in particular about the command, git rebase -i branch and mode looks like this:

enter image description here

1

There are 1 best solutions below

0
On

To squash means combine. With the help of squash you can combine last X commits in your git repo.

Lets understand this through one simple example

  1. Create 4 files fileA, fileB, fileC, fileD in any useless git directory using

    touch fileA fileB fileC fileD

If you check git status you can see these files newly created.

  1. Now add files as follow

    git add fileA
    
    git commit -m "fileA added"
    
    git add fileB
    
    git commit -m "fileB added"
    
    git add fileC
    
    git commit -m "fileC added"
    
    git add fileD
    
    git commit -m "fileD added"
    

If you do git log, you can see commit messages similar to below :

last commit - fileD added

2nd last commit - fileC added

3rd last commit - fileB added

4th last commit - fileA added

Now To squash 2nd , 3rd and 4th last commit. You can do as follow:

git rebase -i HEAD~4

You can see something like this (observe that order reverse compare to git log output):

pick d16e7b5 fileA added

pick 221b175 fileB added

pick 8006a22 fileC added

pick 4fb6454 fileD added

4th last commit is shown at top, then 3rd last and so on.

Our goal is to squash 2nd , 3rd and 4th last commit. For that you can to word pick with squash as follow:

pick d16e7b5 fileA added

squash 221b175 fileB added

squash 8006a22 fileC added

pick 4fb6454 fileD added

Once you save this you can find 2nd, 3rd and 4th last commit will be combined(squashed). In above case you will never need to use squash for d16e7b5 because that is first commit in rebase there is no previous commit you have provided.

git log will show you :

last commit - fileD added

2ns last commit - " FileA added

                FileB added

                FileC added"

I suspect that you must be using squash at first commit that's why it's not working

Note : Following case always give you error if you try in this example.

squash d16e7b5 fileA added

pick 221b175 fileB added

pick 8006a22 fileC added

pick 4fb6454 fileD added