Being a programmer, one should also focus on script/ code management. It includes timely backup of the code among other activities. In an industry where you have peers which also contributes towards the same project, managing the code is a routine exercise. This git tutorial for beginners will teach you enough to work with git.
Git is a version control system which tracks the changes in the source code. This is a distributed system and any authorized team member can access it. A user can always pull the code from git to local machine, make changes and re-upload/push it to git.
Similarly, if some other team members update the code on git, you can pull the changes and update the code in your local system.
Start With Git
Git is available in both GUI and CLI (Command Line Interface). This git tutorial will make use of CLI as it is convenient and extensively used. Git is available for Windows, Mac, and Linux.
This tutorial is created on Linux OS but the steps will be similar for all OS.
Install Git – On Linux
Since this tutorial is based on CLI, open the terminal and execute the following command:
sudo apt install git
Registration On Git Website
Head over to GIT and register using an email. Once registered successfully, login to the github.com.
Add Repository in Git
A repository or a repo is the folder which contains source files or a project. Let’s create the first repo in GIT and upload the first project. The assumption is that you already have an existing java project saved in your local machine.
Creating First Git Repo
We will upload the following existing SeleniumTest project to our git repository.
While logged into your git account, click on the + icon at the top right corner and select New Repository. The following page should get displayed:
Provide any suitable Repository Name, Description and click on Create Repository button, keeping the default values. A URL pointing to this git repo should get generated, note down this URL.
Configuring the SSH Key
Git is an online secure repository which restricts unauthorized access. SSH key should be created to ensure only the authorized user(s) has access to git.
On git webpage, click on your Profile -> Settings and select SSH and GPG Keys. Click New SSH Key button below SSH Keys section on the right. Enter any value for name.
Generating the SSH Key
Open your terminal and execute below code:
ssh-keygen -t rsa -b 4096 -C "email"
Replace the email with your git registered email id. Hit ENTER if asked for file name or passphrase. A .pub file should get created. Open the file, copy its content and paste in the Keys text area on git Settings page below the name.
Pushing The Code For First Time
Go into the project folder to be pushed into git. Open the command prompt and execute the following code one by one.
1. git config
git config --global user.email "email" git config --global user.name "username"
Update email and username with your git’s email and user name. This authentication is required to access the git.
2. Initialize git
This command is used to initialize the git. After executing below code a hidden .git folder should get created inside your main project folder.
3. Connecting to the git url
Recall the URL created after creating the first git repo. Execute git remote add origin <<git url>> and replace <<git url>> with your own.
git remote add origin https://github.com/codeshut/SeleniumTest.git
This will create a connection between your local folder and the online git repo. To check the status execute the following code.
git status will print the file/folder names that are updated and still not synced. In the above image, all the folders are displayed as they are not yet been pushed to the git repo.
4. Adding the first project
Execute the following code to add a complete folder to the git repo in one go.
git add .
The . indicate the current folder. To add a specific file, use the absolute path like /path/filename.java
You may again use the git status command to check the repo status. It should display the files waiting to be synced.
5. Committing the changes
This command is used to confirm the changes made by the user. You should also provide comments against your changes.
git commit -m 'comment here'
6. Pushing the code
Code/changes are still not pushed to the git. If you go to your repo’s git URL, you won’t see any code. Use below command to push the changes to git.
git push origin master
Enter your git user name and password upon prompt. It should push all the changes and code to the git repo.
Go to the git URL and you should see the code is now available in git.
Pulling The Code From Git
In case the code in git repo has been updated since you last commit your changes, use the following code.
git pull origin master
This should sync all the updated files from git repo to your local machine.
These are the basics which will get you through working with the git. Create a repo of your own and make a habit to upload all your code to it.