Practice 1: First steps with Git
By the end of this practice session, you should have:
- Registered a Github account
- Downloaded and installed git
- Created your first repository
- Added a file to it
Github
Github is a development platform and Git repository hosting service that is very popular with both open-source and enterprise projects worldwide. It allows users to host their source code, collaborate with others, manage issues and wikis and much more.
In this course, we will be using Github for hosting our repositories. Go to http://github.com, look around and register an account.
Note: registering with a university e-mail address lets you create unlimited private repositories (usually a privilege reserved for paying users).
Installing Git
Next, we need to install a Git client on our system. As discussed in the lecture, while GUI clients are available, we will be using a command-line approach in this course. Depending on your operating system, steps may vary.
Windows
The easiest way to start using Git on Windows is to get the client from the official Git website.
- Go to https://git-scm.com/download/win and download the installer
- Follow the instructions to install Git
- When prompted, choose a code editor of your liking as the default editor. This application will be used to show you diffs, to let you edit commit messages, etc.
- When prompted, choose to use Git from Git Bash only. This way, your PATH will not be modified and this will not affect the other development software you may have running on your system.
- Leave the other settings as recommended by the installer.
Linux
Many distributions of Linux already include Git. To test if yours does, open the terminal, and enter the command git --version
.
If command could not be found, you need to install it yourself. Typically it is done through a package manager and depends on what distribution you're using. For a list of possible ways to install Git on various Linux distributions, see https://git-scm.com/download/linux
Mac OS
First, check if Git isn't already installed on your machine: open Terminal and enter the command git --version
.
If it's not, Mac OS will actually prompt you to install Xcode Command Line Tools. Follow the instructions of the install wizard to get Git running on your system.
NOTE!
In all operating systems, you must specify your name and e-mail address after installation.
- Open your Git client
- Type
git config --global user.email "your@email.address"
, substituting "your@email.address" to the address you used in Github - Type
git config --global user.name "Your Name"
, substituting "Your Name" with yout actual name or a nickname, as you prefer
Linking your computer to your Github account
In order to communicate with your Github account through your Git client, you need to generate a cryptographic keypair and add the public key to your account.
This makes it safer and easier to communicate with Github - you will not need to provide your name and password every time you attempt to retrieve information from the server or send information to it.
Follow the guide for your operating system on the Github website
First repository
Now we will create your first Git repository and add a file to it.
- Log into your Github account and go to the Github home page
- Click "New" under "Repositories"
- Choose a repository name, optionally write a description and tick the box to initialize the repository with a readme file
- Click "Clone or download" on your repository page. Copy the address in the pop-up window that appears.
- Open your Git client (Git Bash on windows or Terminal on Linux/Mac OS)
- Go to the repository where you want to check out the repository (use the unix
cd
command) - Type
git clone
followed by the address of your repository to check out your repository. - Check that folder with the name of your repository has been created and that it contains a README.md file
- Open the README.md file, write your name and save
- In your Git client, navigate to the folder of the repository (
cd repository-name
) - Type
git commit -a -m "My first commit"
- Type
git push
- Go to your repository on the Github website and check that the information made it there: The number of commits should be 2; commit message next to README.md file should be "My first commit" (or whatever you chose) and the readme section below should show your name
When everything is done, send a link to the Github page of your repository to the instructor in Slack.
Bonus
As a bonus, go to the Github Explore page, find an open-source repository and clone it to your computer. Add the address of the repository to the README.md file of your own repository like you did with the name before.