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