Add a global ignore file to git

Had enough of adding your IDE’s files to the .gitignore file? Time to add a global ignore file for git.

Just execute the following commands.

On *nix or with Windows git bash:

git config --global core.excludesfile '~/.gitignore-global'

With Windows cmd:

git config --global core.excludesfile "%USERPROFILE%\.gitignore-global"

Then you need to create the file:

notepad %USERPROFILE%\.gitignore-global

And add for example the following contents:

data/
.idea/
*.log

That’s it, now you only have to do this one time and not for each project individually.

Installing a module from a git repo with npm

I recently needed to add internationalization (i18n) functionality to an existing GitHub repository. I made the changes and put in a pull request. That pull request has not been processed yet. In the mean time I wanted to use my updated version. After some googling around I found out it is possible to manage modules directly from GitHub (git) with npm.

Such a cool feature. It means I can use my updated version until the main repository is updated. It is really easy, just change the line in the package.json file if you already have the module imported.

  ...
  "dependencies": {
    "@babel/polyfill": "^7.0.0-rc.1",
    "@feathersjs/feathers": "^3.2.3",
    "@feathersjs/rest-client": "^1.4.5",
    "axios": "^0.18.0",
    "vue": "^2.5.17",
    "vue-analytics": "^5.16.0",
    "vue-i18n": "^8.2.1",
    "vue-meta": "^1.5.5",
    "vue-recaptcha": "^1.1.1",
    "vue-router": "^3.0.1",
    "vue2-flip-countdown": "https://github.com/nidkil/vue2-flip-countdown",
    "vuelidate": "^0.7.4",
    "vuetify": "^1.3.1"
  },
  ...

And then run npm install command. The module will be replaced with the git version.

If you have not installed the module yet just execute the following command.

npm install --save <module name> <git repo> 

Example:

npm install --save vue2-flip-countdown https://github.com/nidkil/vue2-flip-countdown

Is this cool or what?

Using Git with SSH key on Windows

Just a quick write up as reminder how to generate a SSH key on Windows and use it with Git. Git comes with OpenSSH which includes ssh-keygen. Of course you can use Putty to generate SSH keys, but why not do it the quick and easy way with Git? If you use Putty you need to convert the generated key from Putty format to the standard SSH format.

Okay lets get started. Follow along with the following steps.

1. Make Open SSH utilities accessible

Add the Git directory containing the OpenSSH command line utilities to the Windows Path. They are installed in the following location.

C:\Program Files\Git\usr\bin

You can do this the traditional way using Windows Control Panel executing the following steps.

  • Pressing the Windows key to open up the Start Menu
  • Search for “advanced system settings” (just start typing)

Alternatively you can browse through the Control Panel.

  • Select System and Security
  • System
  • Click on the Advanced system settings hyperlink in the left hand pane

Or do it the easy way if you have Rapid Environment Editor (RapidEE) installed. Use the following command line from a shell with administrative privileges to add it to the system wide path (for all users) or leave out the -M flag to add the variable to the user path.

rapidee -A -M Path "C:\Program Files\Git\usr\bin"

2. Generate the SSH key pair

NOTE: If you want Git to work with the generated SSH key pair without any further configuration then accept the default location and name of the SSH key pair.

An SSH key consists of a private and public key. The private key should be stored safely, the public key can be shared with others. Don’t forget to set a passphrase for your private key. The passphrase prevents unauthorised usage of the private key by protecting the key itself with a password. Although the directory holding the private keys should be inaccessible to other users, the root user of the system, or anyone who can access the private key can copy and use it if not protected by a passphrase.To add a passphrase to a key just type it when prompted during the key generation process. Keep in mind that the password must be at least 5 characters long. A good passphrase should be at least 10 characters long, and consist of random upper and lower case letters, numbers and symbols.

Generate the SSH key pair with the following command.

ssh-keygen -t rsa -b 4096 -C "nidkil-git-key"

The -t is the key type, -b is the key length (the higher the more secure) and -C is a comment that is added to the key which makes it easier to identify. The comment is added to the end of the key. Don’t believe me? Open the public key file and you will see your comment. Handy isn’t it?

3. Use the SSH public key

If you did not change the default location and name, the SSH key pair can be found in the directory .ssh in the users home directory.

That’s all fooks. Have fun.

Git quickstart

Just some short notes to remember how to get started with Git.

Check version

$ git --version

Setting-up

Set the following configuration items if using Git for the first time, as these values are used for each commit made.

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

Initializing a new Git repository

Make sure you are in the top-level folder of your project and then run the following command.

$ git init

Create the .gitignore file

Git has a special file called ‘.gitignore’ that allows you to specify which files to NOT include in your repository.  The following file is based on the recommended .gitignore file for Python from GitHub.

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
 
# PyCharm files
.idea/
 
# Instance Folder - used for sensitive configuration parameters
/instance

Setting remote repository
Make sure you are in the top-level folder of your project and set up the remote repository.

$ git remote add origin [email protected]:nidkil/name-of-repository.git

Check the remote repository has been set correctly.

$ git remote -v

Staging files
The standard flow for adding files to your git repository is to create/edit the files, add them to the staging area, and then commit them to your repository.

$ git status
$ git add .
$ git status

Initial commit
The following command makes the first commit to the repository, including a message describing the commit.

git commit -m "Initial version"

You can confirm that the commit was successful by checking the log of the repository.

git log

Push to the remote repository
To push the local Git repository to the remote Git repository.

$ git push -u origin master

Working with a feature branch
A common method of developing features is to create a feature branch where you implement specific functionality and then merge the changes back into the master or development branch. Follow the process: create a new feature branch, make changes and merge changes back into the ‘master’ branch.

Create a new feature branch.

$ git checkout -b name_of_feature_branch

Check new feature branch was created (the star next to ‘name_of_feature_branch’ branch indicates it is the current branch that we are working in).

$ git branch

The following commands stage, commit and merge the changes into the local repository.

$ git add .
$ git status
$ git commit -m "Added use of templates and Bootstrap"
$ git checkout master
$ git merge add_templates

Delete the branch.

$ git branch -d add_templates

Add a version tag to your update.

$ git tag -a v0.1 -m "version 0.1"

You can view the changes of a version tag.

$ git show v0.1

You can list tags.

$ git tag

Push the changes into the remote repository.

$ git push -u origin master

Check difference between local and remote repository.

$ git diff master origin/master

Overwrite local master with remote master.

$ git fetch --all
$ git reset --hard origin/master

Create a branch of the local master before overwriting it with the remote master.

$ git checkout master
$ git branch 
$ git fetch origin master
$ git reset --hard origin/master

After this, all of the old commits will be kept in the . However, uncommitted changes (even staged) will be lost. Make sure to stash and commit anything you need.

Oops working in master instead of a branch

Happens to me a lot I start working on a change and after a while realize I am working on my local master instead of a feature branch. Continue working or through away your changes and start again in a new feature branch. Neh, just checkout the new feature branch and the changes will automatically move to it.

$ git checkout -b name_of_feature_branch

Just do a git branch and git status to see that it actually worked 🙂

Cool isn’t it? Who doesn’t love git?

Deleting a remote tag in Git

Today I deleted a tag in Eclipse using EGit that was already pushed to the origin. I then discovered that once the tag is deleted in the local repository there is no way in EGit to delete it from the origin. I am using Github as remote repository. So I checked if the tag could be deleted there. Unfortunately I could not find a delete option there. Bummer what now? Command line git to the rescue. Continue reading

Yes you can: Diff with EGit in Eclipse!

In my previous post on how to quickly view all the changes between two branches in Git I ended with the statement “Is there away to do the same in Eclipse with EGit?“. I Googled around for the answer a bit, but with no success. By accident I just discovered how to compare the working version of a file to the commited version. Not entirely the same, but getting closer 🙂 For the purpose of being able to quickly check what changes I have made to write meaningful commit messages this is sufficient for me.

Anyway, these are the steps:

  • Open Git Staging (Window > Show view > Git > Git Staging)
  • Double click on a file in “Unstaged Changes” or “Staged Changes”

And voilà! There is a beautiful window with a side to side diff of the working version and commited version. Nice! I hope this is useful.

Easy way to quickly view all the changes between two branches in Git

I was commiting some changes in Git and needed to view all the changes between, so that I could write a meaningful commit message. I used the following command to view the changes in each changed file:

git difftool -v development

This command is viewing the active branch (checked out branch) with the development branch. It will open the changed files one by one.

Is there away to do the same in Eclipse with EGit?