Managing software development projects with Git

Where to Next?

Git is now managing the project, but what is it actually doing? Lets look at the history first, which you can see with git log. The excerpt from Listing 6 shows a project with two commits, which corresponds to two versions.

Listing 6

Two Commits

$ git log
commit 9d71c8dd00db5bfb7e21ac8884356d0af284b1b8 (HEAD -> master)
Author: John Doe <john.doe@muster.de>
Date:   Fri May 11 15:22:13 2018 +0200
    new line inserted
commit e29f38d1bc7625090
Author: John Doe <john.doe@muster.de>
Date:   Thu May 10 09:35:53 2018 +0200
    First commit

Each commit is identified by a 40-digit SHA1 hash, which I will simply refer to as the hash. The hash is used for unique identification and as a checksum. For some commands, it is possible to specify the hash as a parameter; the first 8 to 10 digits are often sufficient.

The git log 77558e4ac command will only output the log messages up to the specified commit. In the terminal, you can copy and paste the hash with the mouse by double-clicking the hash with the left mouse button and then pasting it again with a single click on the middle mouse button.

Table 3 contains some commands, including possible options for handling the versioned data. The commands include a multitude of options.

Table 3

Extended Commands

Command

Function

log

Display versions including hash for identification

diff

Display differences between working directory and staging area

diff --staged

Display differences between staging area and last commit

diff hash

Display differences between working directory and commit hash

diff hash1 hash2

Display differences between the specified commits

reset HEAD

Remove files from the staging area

reset --hard

Reset files in the working directory to checked-in state

checkout

Overwrite file in working directory with content from last commit

checkout hash

Check out all files of the specified version .(Note: you cannot modify versions that have already been checked in)

The git difftool command behaves like git diff but starts an external program (Figure 5). Use the command

Figure 5: The git difftool command calls an external program to view the differences between files, in this case, the Meld visual diff and merge tool.
git config --global diff.tool   Program_name

to define the external program if required.

Remote Repository

So far, the project consists only of a local repository in the project directory. However, a typical project usually comes from a remote repository.

To create a remote repository, first create a bare repository from the local data. Unlike the local repo, this does not contain a working directory.

You then have the option to move the bare repository to a corresponding directory, ~/gitrepo in Listing 7. You can then rename or delete the existing project directory. Simply clone the newly created remote repository, and Git creates the subdirectory.

Listing 7

Remote Repository

$ cd ~/mproject/../
$ git clone --bare mproject mproject.git
Clone in bare repository 'mproject.git' ...
$ mv mproject.git gitrepo
$ cd
$ mkdir gitrepo
$ mv mproject/ mproject_old
$ cd
$ git clone /home/john/gitrepo/mproject.git
Clone to 'mproject' ...
$ cd mproject
$ git remote show origin
* Remote repository origin
  URL for retrieval: /home/john/gitrepo/project.git
  URL for sending: /home/john/gitrepo/project.git
  Main branch: master
  Remote branch:
    master followed
  Local branch configured for 'git pull':
    master merges with remote branch master
  Local reference configured for 'git push':
    master sent to master (current)

From now on, you will be editing the project in the newly created mproject directory. The local repository is connected to the remote gitrepo/mproject repository. The git push command transfers the data from the local directory to the remote directory.

Conclusions

Using Git is quite simple, even without prior knowledge of version control systems. You can complete your daily work efficiently at the command line with just a few commands. And since Git usually saves the changes locally, commands execute quickly.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Git Ready

    If you develop open source software, you need to know how to use Git. Information on Git fills books, but this basic overview will get you started with a vital open source development tool.

  • Git Hooks

    The pre-commit framework lets you automatically manage and maintain your Git hook scripts to deliver better Git commits.

  • Workspace: Subversion

    Even if you are not a programmer, you’ve probably heard of Subversion, a powerful tool for managing changes to software projects. Although Subversion is designed primarily for software developers, it can be useful to mere mortals as well.

  • Tree View

    Complex Git projects sometimes require a better view of the dependencies and branches. Several tools offer GUI options for Git. We take a look at gitk, gitg, git-gui, and GitAhead.

  • Branch Management

    Git makes version control as simple as possible. To manage your Git branches successfully, you need to learn the ins and outs of git branch and git merge.

comments powered by Disqus