-
Notifications
You must be signed in to change notification settings - Fork 0
Introduction to Git
Git is a version control system and a popular way of managing software development by ensuring code is backed up and easily retrievable. The Git project itself was designed by none other than Linus Torvalds himself who needed a way to manage development of the Linux kernel.
Use of Git is pretty much mandatory for anyone using GitHub and other source code hosting sites such as GitLab and BitBucket. So for these reasons, I recommend using Git for e-book development (and software development in general).
This chapter will give a basic outline of how Git works and how to use it. However because Git is very popular and there’s plenty of documentation available for it, including an official book (which is available for free on the official Git website) – I won't make this guide completely definitive.
Using Git at first may seem intimidating as there are lot of jargon words to learn. However I will explain each core feature and by the end you should be able to start setting up your first repository with some confidence.
To get started I recommend using a Git client. There are a lot of Git clients, and official Git website keeps a list of them. Alternatively many Git users like to interact with Git exclusively through the command-line.
My personal choice of client is GitHub Desktop. GitHub Desktop is free and open-source with ports for Windows and macOS (and also albeit unofficially Debian, Fedora and AppImage). As its name heavily implies the client is owned by GitHub (now Microsoft) and connects with GitHub seamlessly, however it also supports connecting to other Git hosting services such as GitLab and Bitbucket along with self-hosted Git servers.
GitKraken is also a superb client with a beautiful user interface and has official builds for Windows, macOS and Ubuntu. However for commercial use users must pay $50 per year.
The easiest metaphor to describe Git is to think of a filing cabinet. The repository is the cabinet itself and a branch is each drawer. You pull out a filing cabinet draw to access its contents and then you push it back when you are finished.
Repository – A repository is an instance of Git that covers an entire folder and its files. This is where a project will be placed and all the source files that make it.
Commit – A commit is an alteration made to the repository, this includes any changes made to the files and folders including editing the text files, adding files, deleting files, deleting files, etc. Most clients will show the differences between the files using colour-coding. A commit requires a short title for the Git log and optionally a longer description which can be handy for keeping track of development and writing notes.
Push – A push is the process of sending the commit from the development machine to the Git server.
Pull – A pull is the process of retrieving previously pushed data to the Git server back onto the development machine. Through a combination of pushing and pulling a developer can work on the same code on multiple machines.
Branch – A branch is a particular version of a repository. By default a Git repository uses the master branch and for basic Git use by an individual developer this shouldn’t be a concern. For more complicated development, a branch can be merged bringing those changes to another branch or the branch can be split off. The way Git handles branching is the reason Git is referred to as a distributed source code management system.
As mentioned earlier setting up GitHub Desktop with GitHub is a snap. First and foremost you need to decide if the repository is already on GitHub or you want to add a repository to GitHub.
If the repository is already on GitHub, you can make a local copy of that repository on your development machine’s hard drive:
- Go to File → Clone repository… (Ctrl-Shift-O).
- Select the “GitHub.com” tab (the default) and click the “Sign in” button.
- Sign in to GitHub using your account.
- Choose a repository to clone and the preferred folder for the repository on your development machine’s storage (by default, it’s placed in Documents/GitHub).
If the repository is already on GitLab it requires more involvement to make a copy on the development machine’s hard drive:
- Login to GitLab.com in your preferred web browser, then select your preferred repository and copy the HTTPS link under the repository's name and description.
-
Create a SSH key to authenticate GitLab with GitHub desktop. This is done by opening up the Git Bash (Windows/macOS/GNU-Linux) or Terminal (macOS/GNU-Linux) then running this command (replace the e-mail in double-quotes with the GitLab account e-mail address):
ssh-keygen -t rsa -C "[email protected]" -b 4096
then save the public-private key as a .pub file. - Authenticate the generated key by going to GitLab.com in your preferred browser and to your account’s settings and select “SSH Keys”. Add a new key by copying and pasting the public key half from the generated .pub file which begins with “ssh-rsa” into the text field then give it a name.
- Go to File → Clone repository… (Ctrl-Shift-O).
- Select the “URL” tab and paste the HTTPS link from earlier. Login to GitLab and then choose which repository to clone and where on the development machine’s hard drive to clone it to (the default is Documents/GitHub, but I rename it to Documents/GitLab).
Once changes have been made to the development machine’s copy of the repository, they can be committed to the repository on the Git server. GitHub Desktop makes committing a repository easy to do.
- Select the files to commit and the branch to commit to (leave it as the Master branch if unsure).
- Write a summary for the changes and if necessary a longer description.
- Select “Submit” and then “Push origin”.
- Select the “History” tab to ensure it has been committed properly.
- Wikipedia page for Git
- Git-SCM.com – the official website for the Git source code management tool with documentation on Git, a list of Git clients and a download for the current version.
- Pro Git – the official and freely available book (and e-book) for Git which is available in multiple languages.
- Learn Git – GitHub’s official documentation on Git with many learning resources.
- Git Cheat Sheet – a quick point of reference provided by GitLab showing a wide-range of common Git commands and what they do.
- Git vs SVN – an insightful through rather advanced comparison of Git and SVN that suggests that one isn't necessarily superior to the other.