Git : gitignore , Tags, Aliases

gitignore file

From time to time, there are files you don’t want Git to check in, like target folder created by runnnig maven, or .classpath/.settings files created by your IDE on your project. If you create a file in your repository named .gitignore, Git uses it to determine which files and directories to ignore, before you make a commit. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

If you’re using Windows it will not let you create a file without a filename in Windows Explorer. You can use command promt, git bash etc to create ths file., and then commiting it.

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

$git rm -r --cached .
$git add .
$git commit -m "fixed untracked files"

 Tags

Tag are specific points in history marked as being important. Typically people use this functionality to mark release points (v1.0, and so on).

To list down all available tags that are there in your repo simply type:

$git tag

Git uses two main types of tags: lightweight and annotated.

A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.

Lets create a annotated tag on our previous repository ‘myproject’ which we created last time. You can also use any of your exsisting repo to test this.  To create a annotated tag we use:

$git tag -a <tagname> -m <tag message>

You can provide many more details also here.

git_tag1

Lets now create a light weight tag

$git tag <tag name>

git_tag2

Notice the diff between two tag. Later one has no tagger, date of tag info etc info.

You can delete a tag by usig following command.

$git tag -d <tagname>

You can also tag commits after you’ve moved past them, by specifying the commit id at the end.

git_tag3

You can also search for tags with a particular pattern. You can use -l like below

$git tag -l ‘<pattern>’

Suppose you only wanted to see tags which are of version 1

git_tag4

Now, to push your tags to your remote.  The following command should push all your tags to remote:

$git push --tags

or if you are looking to push a single tag:

$git push origin <tag_name>

You can’t really check out a tag in Git, since they can’t be moved around. If you want to put a version of your repository in your working directory that looks like a specific tag, you can create a new branch at a specific tag:

$ git checkout -b version1 v1.0.0

If you do this and do a commit, your version1 branch will be different than your v1.0.0 tag since it will move forward with your new changes, so do be careful.

Aliases

Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config.

$ git config --global alias.st status
$ git config --global alias.ci 'commit -v'

Now you can user git st and git ci instead.
You can also add these by adding lines to ~/.gitconfig

[alias]
    st = status
    ci = commit -v
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: