Whether or not you use version control, I think you'd agree that it's important to track your changes and keep thorough records of your work on a project. Your future self will thank you whenever you need access to a previous state of it. Trust me, this happens more often than you think.
You can do it manually but it gets unmanageable very quickly. Unless you're one of those persons who needs to experience pain before seeing the light then I'd suggest you listen to my advice and start using version control from the very start.
These days there are many great free and open source tools to choose from:
- Bazaar: It is a distributed version control system sponsored by Canonical. It is written in Python and Ubuntu is one of the many projects that uses it.
- Git: It is a distributed version control system that efficiently handles large projects and has strong support for non-linear development. It is written in C. Many popular Python projects use it, including requests, SQLAlchemy and Django.
- Mercurial: It is a distributed version control system that efficiently handles projects of any size and offers an easy and intuitive interface. It is mainly written in Python. If you ever want to contribute to Python then you'd have to learn it. (Update: The cpython codebase is in the process of moving to git, see https://www.python.org/dev/peps/pep-0512/. Thanks to James Bradshaw for pointing this out.)
- Subversion: It is a centralized version control system that is being used less and less. Most projects are switching from this to the others mentioned above.
Here's a great article on the advantages and disadvantages of centralized vs. distributed version control systems.
And here's the current trend among these tools.
I use Git at work and for my personal projects so that's what I'm going to show you how to install, configure and use in this post.
Let's get started.
N.B. The instructions that follow were verified on a 64-bit Ubuntu 14.04 LTS system. You can find the latest Ubuntu release here.
$ sudo apt-get update $ apt-cache policy git git: Installed: (none) Candidate: 1:1.9.1-1ubuntu0.3 ...
$ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update
Now, if we check the version of Git that will be installed we see that it is the latest version.
$ apt-cache policy git git: Installed: (none) Candidate: 1:2.10.2-3~ppa1~ubuntu14.04.1 ...
This is what we want, so let's install it.
$ sudo apt-get install git
Let's check that it was installed correctly.
$ git --version git version 2.10.2
And, that's it.
Some basic configuration is necessary to get started using Git.
The first thing you need to do is set your full name and email address. This information will be recorded in the commits you make.
$ git config --global user.name "Dwayne Crooks" $ git config --global user.email "email@example.com"
$ git config --global core.editor "atom -nw" # or $ git config --global core.editor "nano -iT 2"
Aliases are not necessary when you're now starting out. But, as you continue to use Git on a daily basis you'd find yourself using a couple commands over and over again. Aliases can help you to reduce the amount of typing you need to do to achieve the same results and it frees your mind from having to remember the details of complex commands. Here are a couple aliases to get you started:
$ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.co checkout $ git config --global alias.st status $ git config --global alias.hist "log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short"
As an example, here's how you'd use the
hist alias. Instead of typing:
$ git log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
You can just type:
$ git hist
And get the same results.
Finally, we can tell Git about all the files we never ever want it to track. It's recommended that you only put system specific files that shouldn't be allowed to sneak into your repositories or your
$ wget https://raw.githubusercontent.com/github/gitignore/master/Global/Linux.gitignore -O ~/.gitignore_global $ git config --global core.excludesFile "$HOME/.gitignore_global"
Here's a simple example to illustrate how you'd use Git.
Create a directory and add a Python module to it.
$ cd /tmp && mkdir hello-world && cd hello-world $ echo "print('Hello, world')" > hello.py # Test that it works $ python hello.py Hello, world
Initialize a Git repository and start tracking the file.
$ git init $ git add hello.py $ git ci -m "My first Python program" $ git log commit fedce34e7fe53a582e47741810bbce90110de377 Author: Dwayne Crooks <firstname.lastname@example.org> Date: Fri Nov 18 08:33:39 2016 +0000 My first Python program $ git hist * fedce34 2016-11-18 | My first Python program (HEAD -> master) [Dwayne Crooks]
Great! You're set to begin using version control in your future projects.
To learn more, I'd recommend you read Pro Git.
- What is Git?
- Learn Git in your browser for free with Try Git
- How to Write a Git Commit Message
- git-config Documentation
- Pro Git is a highly recommended book to read
- Atlassian Git Tutorials and Training
- Installing software from a PPA?
P.S. Here's my challenge to you. Install Git or any other version control system and start using it in your current or next project. Are you already using Git? Feel free to share your experiences in the comments below.