Software Development, Tip of the Day

Tip of the day: Forcing a file in to your Git repository

Normally, you’ll have a .gitignore file that defines what should not go into a git repository. If you find that there is a file or two that is ignored that you really need in the repository (e.g. a DLL dependency in a BIN folder for some legacy application that doesn’t do NuGet, or other package management) then you need a way to force that into Git.

The command you need is this:

git add --force <filename>

Replace <filename> with the file that you need to force in. This will force stage the file ready for your next commit.

You can also use . in place of a file name to force in an entire directory, or you can use wild cards to determine what is staged.

Afterwards you can use

git status

to see that it has been staged.

Software Development

Linking Perforce Merge to Git

Git’s built in Merge conflict resolution is awful. Although all the information is there it is difficult to use for all but the simplest of conflicts. Luckily, it is relatively easy to wire up a third party diff and merge tools to help.

Setting up as a diff tool.

You can download the Perforce Visual Merge Tool here. The only part of the installer that is needed is the “Visual Merge Tool (P4Merge)”

Perforce Installation Wizard - Feature Selection
Perforce Installation Wizard – Feature Selection

To configure Git to use the p4merge as the diff tool, the global config needs to be edited. The global config, on Windows 7 and 8 is found in c:\users\<username>\.gitconfig

The following needs to be added:

[diff]
    tool = P4Merge
[difftool "P4Merge"]
    cmd = p4merge "$LOCAL" "$REMOTE"

The [diff] section sets up the default tool to use, you can configure as many as you like. The [difftool "toolname"] section sets up the options for a specific tool.

Now, in Git Bash, you can type git difftool and it will show the diffs in the perforce merge tool between the current file and the previous commit.

If you have multiple files that have changes it will prompt one-by-one to view them in the diff tool.

If you’ve already staged the files (prior to a commit) then you’ll need to type git difftool --cached in order for them to show up.

If you wish to see just a specific file you can use git difftool name-of-file

Again, add the --cached option (just before the filename) if you’ve already staged the file prior to a commit.

Setting up as a Merge Tool

Open up the .gitconfig file, as above, and make some changes to it. Add the following sections to it which are similar to the diff tool.

[merge]
    tool = P4Merge
[mergetool "P4Merge"]
    cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
    keepTemporaries = false
    trustExitCode = false
    keepBackup = false

If you get a merge conflict when merging branches or pulling down from the remote repository you can now use git mergetool to merge the changes.

Software Development

Getting Tortoise Git to work with GitHub repositories

In this post I’ll walk you through installing Tortoise Git in a way that allows it to interact easily with GitHub repositories.

Download msysgit

First off download msysgit, a prerequisite for running Tortoise Git. (A the time of writing this was v1.8.3).

For the installation, I mostly accepted all the default options. The only change I made was to allow the system’s PATH environment variable to be updated. This will be required for a latter step.

I also left the default “Checkout windows-style, commit unix-style endings”, which is equivalent to the git option core.autocrlf being set to true. You probably also want to set this on if you don’t have it set already. GitHub also has an article on their site about file specific options that you might want to include in a .gitattributes file in your repository.

If you have any existing repositories on your system you can now use GitBash to work with them. At the moment each command, however, will require you to type your user name and password.

Download Tortoise Git

Then download Tortoise Git (v1.8.4 at the time of writing). If you have Windows 8 you should go for the 64-bit edition. Again, I just accepted all the default installation options.

As with GitBash in the msysgit installation, once this is set up you’ll be able to work with any existing repositories, and again each operation will require a user name and password to be allowed.

Download the git-credential-winstore

GitHub has an article on how to set up password caching (skip to “password caching” for download link) if you are using tools other than GitHub for Windows. The file requires that the path variable has the git bin folder in it. This will be the case if the option above was made when installing msysgit. I also found that a machine reboot was required before installing this as it didn’t immediately find git in the path after installing msysgit.

The git-credential-winstore install very quickly. It asks one slightly confusingly worded question, “Do you want to install git-credential-winstore to prompt for passwords?”. The correct answer is “yes”. It doesn’t mean that it will always prompt you instead of at the command line or the GUI tool, it will only prompt for a password if it does not know the credentials to use, after that it uses what’s in its credential store so you don’t get asked all the time.

When git-credentials-winstore is installed it will create a [credentials] section in your .gitconfig file which should be at C:\Users\<username>\.gitconfig

Be aware, however, that GitHub does have a nasty habit of removing the [credentials] section of the .gitconfig file. To get around this, copy the credential section to the gitconfig file in the msysgit directory (If you followed the installation defaults it will probably be in C:\Program Files (x86)\Git\etc.) You’ll have to run as administrator in order to edit that gitconfig file due to its location.

If you have multiple users on your machine you may also want to move the installed location of git-credentials-winstore as it installs in your AppData directory. However, I’ve not tried this as I’m the only user on my machine.

You can now use GitBash and Tortoise Git with your GitHub repository.

Software Development

Git cheat sheet

Here is my little cheat sheet for working with git at the command line.

Check the status of the current repository:

git status

This will return details of which files are modified and any files that are untracked, etc.

Add a file to the repository:

git add <filename>

You can also replace filename with a dot (.) to include all untracked files. Use git status before to find out what files are currently untracked.

Commit the changes to the local repository:

git commit -m "<commit message>"

This will only commit the changes as far as the local repository.

Send the changes back to the server:

git push origin master

origin is the name of the remote location of the repository. It is more-or-less, by convention, where you cloned your local repository from.

master is the name of the branch. By convention this is your default branch.

Revert a file to the version at the previous commit:

git checkout -f <filename>

Delete files from the repository

git rm <filename>

If you want to delete an entire directory you need to add the -r (recursive) flag.

git rm -r <folder-path>

You may also need to choose between keeping the files on the disk and removing them altogether. In which case you want either --cached, to remove them from the repository but keep them on disk, or -f to force the removal of the file from disk.