top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Git Basics for Beginners

When you are working with a team Git is an important part of daily programming.Since there are many things you can explore in Git,I explained few basic things to get started with Git.

What is Git?

Git is a Version Control System (VCS), allows us to keep track of when and who did what changes to our files. Those can be code, config,images or whatever else we need to track.

What is Git Hub?

Git hub is a Git repository hosting service that provides a web-based graphical interface (GUI). It helps every team member work together on a project from anywhere, making it easy to collaborate.

Create Account

To get started with Git create a Git hub account using email id.


Git installation

In your machine download the latest version of Git 32/64bit based on your system specification. Once the file downloaded install it in your local system. Once installed,select Launch the git bash and select finish. Now the Git Bash is launched.




Check the git version using the command

  git --version

For any help use the command

 git help

To set the user name and email address to be used with your commits use

 git config –global user.name “[name]”
 git config --global user.email "email@address.com"

The email address should be the one you used to create the account in Git hub

To initialize the new repository use the command

 git init <repository name>

This will create a new Git repository in your local system.

Once the initial set up is done you can work with your local and remote repository!


The basic Git Commands

Git Clone:

Git clone is a command for downloading existing source code from a remote repository.

If you want to download a project from Github, click on the green button called code or clone and copy the URL in the box, then go to the folder where you want to clone the repository (repo in short) right click -->Show more options -->Git bash here,the CLI will be opened and paste the URL after the git clone command.



$ git clone <url of the repository>

This will make a copy of project in your local work space.


Git Status:

The git status command tells us all the information about the current branch

 git status

This command provides the current working branch. If the files are in the staging area, but not committed, it will be shown by the git status. Also, if there are no changes, it will show the message no changes to commit, working directory clean.


Git add:

The git add command is used to add all the untracked files to the staging area. Git add command is used to add any new or modified files.

$ git add  <file name> -to add single files
$ git add  * - to add one or more files 

Git commit:

The git commit command makes sure that the changes are saved in local repository.

$ git commit -m  'Commit message'

Commit message helps everyone to understand what is added or modified in the project.After the commit you can check the status using git status command, it will show working tree clean.

Git Push:

After committing all your changes the next thing you want to do is push or transfer your local changes to remote repository.The git push command will update your remote repository.

$ git push origin <branch name>

However, if your branch is newly created, then you also need to upload the branch with the following command:

git push --set-upstream <remote> <name-of-your-branch>

or

 git push -u origin <branch name>

Git Branch:

A branch represents an independent line of development. The git branch command lets you create, list, rename, and delete branches.

By creating new branch,we can experiment without breaking what already works, several developers can work in parallel on the same project simultaneously.

To list all the existing branch we can use the command

git branch

To create the new branch the command is

git branch <branch name>

To switch to the corresponding branch

git checkout <branch name>

To create a new branch and switch to it we can use the command

git checkout -b <branch name>

In your specific branch you can do changes then you can add, commit and push

Git merge

When you completed your task in your branch if everything works fine the final step is to merge your branch with the parent branch (main/master/dev).

Before merging you need to switch to the corresponding branch in which you are going to merge. For example if you want to merge your test branch to main branch first you need to switch to the main branch using the command

git checkout main 

Then you need to update the local main branch using

git fetch

Finally you can merge the branch using

git merge <branch name>

Make sure your main branch has the latest version before merging to avoid any conflicts.


Git revert

Some times we want to undo the changes we have made, the safer way to do this is using 'git revert' command.But this to be used before pushing your commit to remote repository.

Sometimes we can use "git reset –hard [commit]" This command discards all history and goes back to the specified commit.


These are few of the commonly used Git commands, there is more to learn, I hope you found this useful to get started with your Git Journey.


Happy learning! Keep exploring!!

409 views1 comment

Recent Posts

See All

Setting Up Jenkins on macOS: A Step-by-Step Guide

Introduction: Jenkins is a powerful automation server that is widely used for continuous integration and continuous delivery (CI/CD) pipelines. Installing Jenkins on macOS can be a straightforward pro

bottom of page