Introduction
Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.
- Tool
- Remote repo
Installation and configuration
It is an apt package and can be installed using:
sudo apt install git
Configure you user-name and email-address to identify your commit
git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "EMAIL-ID"
Initialising
To initialise a folder as a git folder Open terminal
cd path/to/directory
git init
Version control
To start, add file to git. Git will start tracking whatever files added. You can add files to git using:
git add FILENAME
git add FILE1 FILE2
To add all file inside the current directory
git add .
You can view the status of the files in a git enabled directory by using
git status
After changes are made, these changes are to be staged for a commit.
git stage FILENAME
After the changes are staged, it can be committed. A commit creates a snapshot of all the files which were staged at that point. A commit must also always have a commit message.
git commit -m "PREFERABLY A DESCRIPTIVE MESSAGE STATING ALL THE CHANGES MADE IN THE COMMIT"
Branches
You can create multiple branches to work on. Changes in the tracked files of a branch does not reflect in other branches. You can create a new branch using:
git branch BRANCH_NAME
This will create anew branch from the existing branch from the latest version of files
Switching of branches can be done using checkout command
git checkout BRANCH_NAME
Setting up remote
You can set up remote using ssh:
- For this first you need to generate ssh private key and public keys. This can be done using:
ssh-keygen
command
After this step, you will be able to see the keys generated in ~/.ssh folder
Open the id_rsa.pub, copy the contents of the file.
Go to your remote repository, click on the clone button. and select ssh option.
There you will find a link to Manage ssh keys. Click on it. It will direct you to a page where you can add you ssh key.
Paste the contents you have copies and give a suitable name for your device
This will enable your system to communicate with repo without password.
After adding the ssh key, copy the ssh link from the clone section.
In the git directory
git remote add origin SSH_LINK_TO_REPO
Git Pull and Git Push
All the commits are local to your system. To update the local commits in remote repository you need to push the changes to remote repository.
git push
Above command assumes that you have added the remote origin. If not, please proceed to setting the remote origin and then push.
Conversely, to fetch changes from the remote repository, you need to pull from the remote repository.
git pull
You need to add the remote origin to pull from remote repository