- Some knowledge of the terminal.
- Some previous use of VIM would be advantageous to create, edit and merge files.
Git: Git is a distributed version-control system for tracking changes in source code during software development
The step-by-step tutorial, vimdiff
The following process assumes that you are using a Mac, and you are using the terminal. Terminal commands are prefixed with the dollar symbol ($) but is not typed into the command line itself.
Choosing which mergetool to use(this has to be done for each Git repository you initialise!)
$ git config merge.tool vimdiff
By default git displays the two files to be merged. It is usually a good idea to use diff3 to set the editor to display the common ancestor BASE while examining the changes in LOCAL and REMOTE through
$ git config merge.conflictstyle diff3
Setting up a Git repository
In a new folder
Through the command line we set up a git repository
$ git init
Add a file of animals
$ vim animals.txt
then within vim add the following animals (entering insert mode in vim first):
and of course save the file through vim with :wq (remembering to exit insert mode first)
Add to git, commit
$ git add .
$ git commit -m “initial commit”
Once we have committed files, we will switch to a new branch. The idea behind this is to make some changes in both branches so they are out of sync and then use the mergetool to make everything fit together.
Create a new branch
$ git branch animalsbranch
Switch to the new branch
$ git checkout animalsbranch
Alter the text file on the new branch
$ vim animals.txt
using vim, remove “dave” since (obviously) Dave is not an animal. Save as before (:wq)
Commit the changes to the branch (the a switch stages the file for you here)
$ git commit -am “removed Dave”
Switch to the master branch
$ git checkout master
We are now going to update the main branch to make all the animals plurals
Alter the text file on the main branch
$ vim animals.txt
Commit the file (again the am switch is wise to use)
$ git commit -am “Updated the animals to plurals”
When we merge we merge from the branch, into the master.
Commit the file (remember we are on the animals branch. This can be confirmed with a git status command, or view the branches through git branch)
$ git merge animalsbranch
You will see an error as there is a merge conflict in animals.txt.
As instructed, we will fix the conflicts (here using merge tool), and then commit the result on the master banch.
Merge into the master branch
$ git mergetool
If you have selected the right mergetool, vim will open
Now you will need to navigate vim to choose which window to change. The merged split is shown at the bottom (and by default this is where you change the file).
The cursor keys (shown above) care combined with control to select the appropriate window. However you should know that
Control w then h
Control w then j
Control w then k
Control w then l
You can choose to use vim shortcuts for the REMOTE, BASE or LOCAL (respectively, as below)
To close all of the windows at once, you use the command
Comitting your merge
$ git commit -am “merged”
Confirming your commits
Check the commits made through git log
$ git log — — oneline
Sometimes you want to switch branches, not losing your work but equally not committing half-completed work. The solution is stashing.
$ git stash save mystash
this can be followed by listing all stashes (since they are stored in a stack)
$ git stash list
meaning that stashes simply work like a stack
It often makes sense to create a branch from a stash, so then it can be merged afterwards.
$ git stash branch testchanges
which puts you on the branch testchanges. As before, you can switch to master and then merge back to master with any changes.
Tips and tricks
Reverting your merge
To revert you can abort
$ git merge — abort
but you will be left with a series of backup and orig files:
Removing *.orig files
These can be removed through the command
$git clean -f *.orig
or remove all untracked files (use with care! The first option gives you a list, the second deletes)
$git clean -n
$git clean -f
Resolving conflicts from a git pull
You resolve the merge conflicts using merge tool, and then
Want to get in contact? Use Twitter: