Linux Format

Tackle Git

Join Shashank Sharma as he explores one of the most popular version control systems and puts it to everyday use.

- LXF

Join Shashank Sharma as he explores one of the most popular version control systems and puts it to everyday use.

Git is a popular versioning control system (VCS) that’s designed to serve two essential tasks. First, it allows users to track changes to a file, which makes it possible to identify and correct mistakes. Second, its distribute­d nature means that a large number of users can edit these files at the same time. What’s more, the system can keep track of all the changes made by the myriad users independen­tly. In the Linux ecosystem, Git repositori­es (repos) are frequently used by developers to allow everyday users access to the latest release of their software while simultaneo­usly allowing developers to collaborat­e on extensive projects.

For software developers, the added advantage of using VCS is its ability to merge the many different independen­tly developed branches into a single coherent piece of software. But Git isn’t limited to only serving developers. Since a VCS is designed to work best with plain text files, you can easily use it to keep track of your essential documents. This provides for a quick rollback mechanism to revert changes to a file.

Before embarking on the more interestin­g everyday uses for Git, let’s take a quick look at how you can use it to fetch software. The most popular software repo is GitHub which serves over 14 million project collaborat­ors hosting more than 35 million repos.

Your Linux distributi­on (distro) most likely ships with the Git client, but you can easily install it using your distro’s package manager. Once installed, you can replicate the files for any project hosted on a remote server such as GitHib by running the following command: $ git clone project-url .

The command will copy all the files into a new local folder with the same name as the repos in the given URL. You can then build the software, explore the code and even make changes without having to access the server again.

If you wish to add Git support to your own project, you need to first create a git repo of your project. You can do so from the terminal by running the git init command from within the project directory. This creates a hidden .git folder inside your project directory which it’ll store all the pertinent informatio­n about the files as well as compressed copies of past versions.

Git going

After creating a repos you still need to populate it with files and the next step is to identify the files you wish to track with git. You can use regular expression­s and wildcards with the git add <filename> command. For instance, if you wish to track all files starting with letter ‘n’ in your repo, you can do so with the git add n* command. This command adds the specified files to the staging area.

With your git repo in place, you can create timely snapshots of your project before each significan­t change. If you’re dissatisfi­ed with the newer modificati­on, you can revert back to a previous saved state, and start again.

You must run the git commit -m 'Add meaningful comment' to create a snapshot of all the files you’ve already added to the staging area. But if you only wish to commit changes made to a single file you must specify it by name: git commit -m 'Meaningful comment' filename

You must choose your commit message carefully as it’ll be used to identify at a glance the changes made to the file at the time of each commit when you view the file history. Ideally, it should be curt yet provide sufficient details. After each successful commit, git will output a unique ID.

For each significan­tly modified file, you must either run the git add and git commit command or you can run the git commit -m 'meaningful and informativ­e comment' -a

command. The -a command option instructs git to commit all the files already in the index.

The git log and git show <commit ID> commands can be used to quickly ascertain the various commit operations you’ve run on your project from time to time. If at any point you decide that you’re unhappy with the recent commits, you can revert back to the previous saved state by running the git

checkout <commit ID> command. This is why it’s important to identify the various commits with appropriat­e comments.

When collaborat­ing with other users, the mere safety of past commits may seem inadequate. Thankfully, git allows you to fork your projects into different branches. Each branch is a copy of your main repo, called the master branch, as it exists at the time any particular commit. You can decide to either dump the branches in entirety or incorporat­e the changes made in a branch into your master repos. To create a new branch, you must run the git branch

branchname . When run without a branchname, the command will provide a list of all branches. You can now start work on the newly created branch, without any risk of affecting your master branch, by running the git checkout

branchname command. From this point forward, you work with the files in the regular fashion, making changes using your favourite editor and regularly adding the files and committing the changes. If you decide to merge the branch into your master repo, switch back to it with the git checkout command and then run the git merge branchname command. In fact, you can merge two or more different branches and merge the finished branch into the master repo.

Git ships with its own set of graphical utilities to aid users not comfortabl­e on the command-line. You can use the Gitk utility to view all the different commits made to your repository, the various files in each tree etc. The git-gui tool can similarly be used to make changes to the repository by adding new commits, editing existing ones, creating branches, performing merges etc.

Graphical git

Apart from these, there are several free, open source as well as commercial applicatio­ns that allow users to perform all of these functions. However, due to the complex nature of git, the tools may not appear to be intuitive or straight forward.

Although originally intended to be used for source code management, you can use git to track any file, be it the first drafts of chapters for a book, edited images from a family vacation, PDFs and ODTs etc. The only disadvanta­ge to working with files that aren’t plain text is that you can’t use the diff utility to view the changes made to the files.

If it feels like we’ve rushed through important Git concepts, that’s because we have. The Git project offers a wide range of features and we’ve only scratched the surface of what it can do. For more resources and informatio­n, head over to https://git-scm.com/doc for the official reference manual, book as well as video and other tutorials explaining its many features and functional­ity.

 ??  ?? Shashank Sharma is a trial lawyer in Delhi and avid Arch Linux user. He’s always on the hunt for geeky memorabili­a. Our expert
Shashank Sharma is a trial lawyer in Delhi and avid Arch Linux user. He’s always on the hunt for geeky memorabili­a. Our expert
 ??  ?? Gitg provides different context menu options depending on where you click.
Gitg provides different context menu options depending on where you click.

Newspapers in English

Newspapers from Australia