Skip to content

Archive

Tag: Git

Git is a very useful versioning system. It can also be a very difficult system to use effectively, since it relies on a lot of arcane commands to work well, and unless you use Git often, most of these get forgotten.

However, there are a few commands that I use on a regular basis, which I cannot live without, so here is a brief rundown of these commands.

The first command you need to know is how to clone a remote repository. This is done with the clone command, and will then create a local repository cloned from the remote repository.

    git clone [email protected]:projectname/remoterepo.git localrepo

Now, the freshly created repository is normally set to master, which might not necessarily be what you want. For example, we tend to work at work on the edge branch of our remote repository, keeping the master branch for stable code, so now we need to switch to the new branch (in the example, the edge branch), using the checkout command. We need to track the remote branch too, so have added that parameter in. Origin in the command below refers to the remote repository which your local repository was cloned from.

    git checkout –track -b origin/edge edge

Now, to keep the local repository up to date, we need to do a pull from the remote repository when necessary, and we need for that is

    git pull

After you have edited the files you need to, you can call commit, which with the -a flag set will commit all changes you have made to files being tracked by the repository. I must admit that I tend to use TortoiseGit to do commits, as the GUI makes it much easier to make sure all the files I want to commit are correct, and that I have added a commit message, although if you do want to do it manually, you can use the command below.

    git commit -a

You may also want to add or remove files from the repository, which you can do using the commands

    git add newfile.js
    git rm oldfile.js

Once you have done a commit, you need to push the changes to the remote repository using the push command.

    git push

Every now and again, you make a commit that is incorrect, so to fix this, you can undo a commit. To undo the last commit you can use reset. The –soft flag means that you don’t lose all your changes though, so is often a better idea than using –hard which clears everything about the last commit. This command can also be used to undo a series of commits by changing the HEAD^ parameter, but that is out of the scope of this article, and, frankly, have never had to undo more than one commit at a time.

    git reset –soft HEAD^.

Those are the basic commands to use git, although there are many, many more for you to explore.

Share


Git is a rather complicated version control application that I still, after 5 months of working with, still do not fully know how to use. I keep forgetting all the different commands, which at times can seem quite arcane.

This is where this book comes in. It is one of the most comprehensive guides to using git, and covers just about everything you would want to know in how to use it.

The nice thing too, is that it explains what everything is, so for example, what a branch is, in a clear and easy to follow way. Most other guides I have found have been rather lacking here where either they are too brief, or else go so over your head that it sounds like gibberish.

The other nice thing about this book is that it is fully available online for free at www.progit.org, although there is also a print version available.

Share