Git is the most widely used distributed version control system today. It is known for its speed, flexibility and robust features.
For this tutorial, it is assumed you have basic knowledge of the terminal or Window Power Shell
- On Debian/Ubuntu:
sudo apt install git-all
- On Fedora/CentOS:
sudo dnf install git
- Arch Linux
sudo pacman -S git
- On Windows: Click here
- On Mac: Click here
If the command is not found, install git using onw of the methods outlined above.
bash git --version
After installing git, you need to do a few things to customize your git environment. There are three types of environments as indicated in the --<environment> flag below: The system environment (--system), the global [user scope] (--global) environment and the local [project scope] (--local) environment. --local is infact the default. If you use the local scope, you need to run the setup command for every project.
The basic settings to get working with git are:
-
Identity: Setting up your username and email address. The username is not neccessarily your GitHub username.
The following commands are issued to the user scope.
git config --global user.name "John Doe" git config --global user.email "someone@example.com"
-
Set your default branch: By default,
gitnames its default branch asmasterwhile GitHub usesmain. It is advisable to set your default branch tomain. Formgitversion2.28onwards, you can set your default branch.git config --global init.defaultBranch main
There are other commands such as setting up your default text editors. If you used the windows installer during installation, you would have noticed that some of these settings were made during the installation setup.
-
Preview the Current Settings: You can view your current settings using:
git config --list
- You can also check the value in a key by using the command below, for example, you want to see the current value stored in the
user.namekey.
git config user.name
- You can also check the value in a key by using the command below, for example, you want to see the current value stored in the
-
For every command line tool, there is a
helputility which explins how the tool is used. Anyone of the following commands can be used to quary the help utility ofgit.git help <verb>
or
git verb --help
For example, to get the manpage help for the
git addcommand, run:git help addor
git add -h
- To track your project with git, you need to make git know about your project. You can make your project a git repository by doing one of the following:
- Initializing a git repository in your project directory
- Cloning an existing repository
- In this section, we will discuss initializing a git repository in your root project directory.
- In your root project directory (
cdinto your project's root directory), use the commandto initialize a git repository. This will create a hidden directory namedgit init
.gityour project's root directory and this is the folder where everything that has to do withgitis stored.
- You can clone an existing repository using
git clone https://github.com/<username>/<repo.git>
- This will copy everything about the project including snapshots of versions a folder named in your current directory where you run the
git clonecommand. You can thencdinto the directory and start working. Be sure to edit the command to raname the username and the reponame. The angular brackets are not included. Examplegit clone https://github.com/mobadara/git-course.git
- Now that you have a repository (initialized or cloned),
cdinto the repository and start creating your project files and directory normally. - Imagine you have two files in your project directory
index.js,index.htmlwith an hidden.gitdirectory, you want to informgitabout the two files. Use the commandgit add index.js git add index.html
- You have added the two files to the staging area of the repository. At this point, you need to commit your changes. This can be achieved by the command
git commit -m "I just made a change to the index files" - The
"I just made a change to the indexfiles" is call a commit message and can be any string as long as it describes why you committed. - Finally, you push your changes to a remote repository. You need to inform your local repository (
git) about your remote repository. You can register many remote repository with one local repository as long as they have different name reference. You will need to create a repository on your remote repository provider at this point and name it according to your project. Common remote repositories are GitHub and Bitbucket. The name "origin" is commonly used to reference our first remote repository in our local repository. Register your first remote repository with the command:git remote add origin https://github.com/<remote-repo-username>/<remote-repo-name>.git
- With that done successfully, use the command below to send your project to the remote repository you registered above.
where
git push -u origin main
originandmainabove are the remote repository reference key and branch name respectively. Recall that you set your default branch name tomain. Now, on a web browser, visit your remote repository landing page and find a copy of your project.
Muyiwa J. Obadara