Git Commands and GitHub
Learn essential Git commands and how to use GitHub effectively for version control and collaboration in software development projects.
Table of Contents
Git is a version control system that is used locally and can track and store different versions of a project in development. Its commonly used counterpart, GitHub, is a remote storage for hosting and sharing the code. This article will explain some commonly used commands for operating git and communicating code between local Git repositories and remote GitHub repositories.
Installing Git and Cloning a GitHub Repo
First choose the appropriate package management system, and install git locally, e.g.:
Fedora/RedHat/Centos:
sudo dnf install git-all
Debian/Ubuntu:
sudo apt install git-all
Next import (clone) the remote repository from GitHub to your local folder in order to work with it:
- Find the repository page online on GitHub
- Click on the green button that says “Code” and copy the repo's URL
- In your command line navigate to the desired location for your local repository
- Clone the remote repository to the local location:
git clone <url>
The folder structure from the GitHub repo will be copied.
Making Changes and Committing Them
In order to create a “version” the changes made need to be staged and then commited:
1. Check the status of your local repo. The git status command:
git status
will list all files that have been updated and/or staged.
2. Select the files (changes) that you want to be committed. Use:
git add <file>
to stage a changed file. If you have a staged file that you don't want to commit use
git restore <file>
3. Run:
git status
again to make sure those (and only those) files that you want to be committed are staged.
4. Run:
git commit
to commit your staged changes. You will be directed to add a commit message. Alternatively you can use:
git commit -m “<message>”
as a shortcut.
5. You can check your commit history by running:
git log
Pushing and pulling changes between local and remote repositories
To push your local commit onto the remote GitHub repository run git push
command. You will need to specify the remote name - typically “origin”, and the branch name - by default “main” or “master”.
git push origin master
To pull changes made on the remote GitHub to your local repository use git pull
For more information including information about branching please check the official documentation on https://git-scm.com/docs