Table of contents
One of the prerequisites of being a professional developer is learning and knowing how to use Git.
What is Git?
Git is a DevOps tool used for source code management. It is free to use and used to track changes in the source code. Git is not a programming language, it is however important for every developer no matter the language or tech stack the developer may be used to, to learn and make use of the Git version control tool.
Git as a version control
What is version control?
Version Control, also known as source control, is the practice of making, managing and tracking changes made to computer programs, websites, and a collection of other documents. Version control is made up of systems which comprise tools that help developers and software teams make and manage changes to source code over time. The systems, in turn, have software that keeps track of all changes and modifications made in the source code from time to time.
Git is a Distributed Version Control System, that is, rather than having a single system containing the history of changes made to a repository as with a centralised version control system, Git has a distributed system that enables software development teams to have multiple local copies of the project's codebase or repository independent of each other. Thus, if any server dies, and these systems were collaborating via that server, any of the client repositories can be copied back up to the server to restore it.
Git on Operating Systems
Git can be installed in operating systems ranging from Mac to Windows to Linux. Before using Git, you have to make sure it is already installed. If it is installed, you have to ensure you have the latest version. Git can be installed through an installer, as a package, or through a source code.
Git on Mac
Git comes preinstalled on most Mac computers. To check if you have Git on your Mac, click the Spotlight icon in the menu bar, and type "Terminal" into the search box. Once you have opened the Terminal application. Check the git version by typing git version
or git --version
into the terminal. The output will tell you the version of Git installed on your Mac
For example, my git version is 2.32.1. If you do not have git installed, it would return Git as an unknown command. If that is your case, You can check out how to install through homebrew or macOS Git installer as explained below.
Git on Linux
Just like Mac, the majority of Linux systems come with Git already installed. On your windows machine, type "git bash" in the command prompt. When your git bash terminal is opened, type the command prompt ``git --version
. If you do not have git installed, it would return Git as an unknown command. If that is your case, You can check out how to install it through homebrew or Guthe git for windows package.
Installing Git
Installing Git through Homebrew
Simply type brew install git
on your terminal. After the installation, close the Terminal and restart/open a new one, then type git --version
to get the version of git installed.
Note- You may get a path error or a previous version of git is in the path where the now downloaded version is supposed to be. In cases like this, you can run
brew link --overwrite git
to overwrite the former version of git in the system.
Installing Git on Mac
Go to the macOS Git Installer and download the latest version. Once the installation is done, go to the terminal and type git version
Installing Git on Windows
Go to Git for Windows package [insert link] and download the latest version. Once the installation is done, go to the git bash terminal and type git version
Installing Git on Linux
Git can be installed via the binary installer on Linux through the package management tool that comes with your distribution.
If you are on a Debian-based distribution like Ubuntu, you use apt
As always, it is essential to ensure that you are on the latest version by running the sudo apt-get update
then
sudo apt install git-all
Then check the git version by typing git version
If you’re on Fedora or an RPM-based distribution you can use dnf
. Check for the latest version of git by running dnf check-update
then
sudo dnf install git-all
Then check the git version by typing “git version”
Installing from the Source
Installing Git from the source gives you the latest version of git so you do not need to update or check if it’s the latest version you have installed.
If you are on Debian-based distribution like Ubuntu; you can check out the digital-ocean tutorial on steps to install and set up git from the source.
Git Commands
Git Commands were designed to coordinate and assist programmers using source codes during development. They are used for tracking changes in any set of files.
Here is a list of basic commands every developer should know:-
Git config: configures/sets the user's profile and default branch name. It changes the name and email address to be used in your commit.
Syntax:
git config --global user.name "Your Name"
git config --global user.email "YourEmailAddress@me.com"
When setting your git for the first time, it is advisable to use the
--global
option as every future commit uses whatever information is embedded in the config file. If you want a different name and email address for specific projects you can ignore the--global
optiongit config user.name
Because git creates the
master
branch by default when a new repository is created withgit init
. To set main as the default branch name, the git config file can be used.Syntax:
git config --global init.defaultBranch main
Git init: initializes or creates a new repository. For an existing project to become a git repository, run
git init
in the targeted root directory.Git clone: makes a copy of a repository from an existing URL in a new directory or location.
To clone a repository, navigate to the main page of the repository, click on Code, and copy the repository URL. Then open a terminal and change the location to where you want the copied repository then do
git clone URL
Git status: displays the state of the working directory and staging area. It shows all untracked files and changes that have been staged. Simply run
git status
Git add: Adds one or more files to the staging area to be committed.
To add a file -
git add FileName
To add more than one file or to add multiple changes you have made in your local repository without having to name each filename -
git add .
Git commit: After you run the
git add
to add files, you run thegit commit
to save the changes in your local repository. The git commit usually accompanies a message description of changes made to your repository.Syntax:
git commit -m "Your commit message"
Note that your commit messages must be enclosed in quotes to avoid throwing an error and your message should be conveyed in the present tense.
git commit -m "adding image folder to the root directory"
This command also commits any file added in the local repository with git add.
git commit -a -m "Adding image folder to the root directory"
A git commit can also be used to update the date or backdate the last commit to a specific date using the ```
date
`` variable followed by the git message.Syntax:
git commit --date="24-02-2022" -m "Your commit message"
orgit commit --date="10 days ago" -m "Your commit message"
Git push: uploads changes made to the local repository to a remote repository. The remote repository is the destination of the push operation.
This command can be used to organize the commits in the branches in the repository.
Git push origin master - sends changes made on the master branch to the remote repository.
Syntax:
git push origin master
Git push -all: pushes all commits of branches to the remote repository.
Syntax:
git push --all <remote repository>
Git Branch: lists all branches in the repository
Syntax:
git branch
Git Merge: merges the specified branch history to the current branch.
Syntax:
git merge branchName
Git checkout: switches from one branch to another.
Syntax:
git checkout [branchName]
It also creates a new branch and switches to it.
Syntax:
git checkout -b [branchName]
Git pull: fetches data from the remote server and merges changes to your working directory.
Syntax:
git pull URL
Git log: checks commit history.
Syntax:
git log
Git stash: temporarily stores all modified tracked files.
Syntax:
git stash save
This command also restores all recently stashed files.
Syntax:
git stash pop
Git remote: connects the local repository to the remote server.
Syntax:
git remote add [variable name] [Remote server URL]
Git Bash commands and Git commands
Bash is an acronym which stands for Bourne Again Shell. It is a terminal application used to interface with an operating system through written commands.
According to the Atlassian website, Git Bash is an application for Microsoft Windows environments which provides an emulation layer for a Git command line experience. It is available on Linux and macOS.
Git Bash has sometimes been confused with Git commands.
It is a terminal application with its own set of commands, Git Bash commands and additional commands like the Git commands. The Git Bash commands are entirely different from Git core commands.
Bash commands can be used to create, organize and separate files in the computer
Some Git Bash commands include:
"mkdir": to create a folder.
"cd": used to change directory.
"cd -": return to a previous directory/folder.
"cd ..": used to move up a directory. The root directory is the first and topmost directory
"ls": used to see or check the list of all available files and folders in the current directory.
"ls-l": gives additional information to files and folders listed
"ls-la": lists all directories including hidden files like the .git directory.
"touch": used to create a file/files
"rm-": used to delete files
"rm -r": used to delete a folder
"rm -rf": used to forcefully remove and delete a file entirely from the system. N.B-
rm -rf /
deletes the root directory, which is the directory that houses other directories of your system. In other words, when this command is run, all the directories in your system can be wiped off entirely. It is advisable never to run this command."pwd": view the working directory of your system
"cp": copy a file
"cp -r": copy a directory and its contents.
"code filename": opens a file directly from the terminal in the VScode