Sitemap

Get started with GIT Commands

7 min readJun 26, 2023

--

Want to know what is Git and how to use it? This is the best article suit for you. Let’s take a look and learn what is Git? the difference between Git vs GitHub & the most important Git commands to use in your projects.

Press enter or click to view image in full size

What is Git?

Git is an open-source, distributed version control system introduced in 2005. Simply Git allows one to manage small to large projects on the computer and keep track of the source codes’ history. It enables multiple developers to work together in a nonlinear method.

Users can add the files of the project created on the computer to Git by initializing it as a Git project.

To initialize a project as a Git project and track its changes, you must have installed the Git version on your computer.

Install Git on Windows

Click here to download the latest Git version

  • Select the suitable version for your computer and download it.
Press enter or click to view image in full size
download the latest git version from this site
  • Run the downloaded .exe file to install git.
  • After installation open the command prompt and type git to check the status of the installation.
  • If it shows as bellow. Then you have successfully installed Git on your computer.
Press enter or click to view image in full size

What is GitHub?

Git Hub is a cloud service that let users manage their Git repositories. (git repository is a container that manages the files and tracks the history of the changes in the files of the project)

Users can create a Git project in Git Hub and clone it to the local computer or push the Git project in the local computer to the Git Hub.

Create a Git Hub account

  • Click here and go to Git Hub.
  • Click the sign-up button and create a new account.

Git vs Git Hub

Press enter or click to view image in full size
Git vs GitHub

Git commands

You can manage your Git repositories using Git GUI or Git Bash on your computer. Here we discuss how to manage Git repos using Git Bash. You can use the following Git commands on Git Bash or Windows Powershell to manage local Git repositories.

git init- used to initialize the existing project as a git repository (Should move to the relevant directory in the shell)

Press enter or click to view image in full size

git init file_name — Create and initialize the file or the folder as a git repository.

After initializing a project as a git repository you can see the .git folder as a hidden file.

Press enter or click to view image in full size

git config — First of all you should define the username and email, It helps to identify the users who did changes at each commit or push.

git config — global user.email youremail@example.com

git config — global user.name yourname

–global — use the defined user name & email to use in all the git repos.

–local — use the defined user name & email to use only in that repo.

git status — used to check the status of the git repo.

git add — When creating a new file/folder in a git repo you should add that file as a tracked file. If not It does not track the changes done in the files. This command makes the untracked files tracked.

After adding the files as tracked they are in the staging environment.

git add filename — make the mentioned file into a track file.

git add . / git add –all / git add -A — make all the untracked files into a track file.

Gitignore — If you want to ignore some files/ folders from committing to the repository, you can add a .gitigore file and add the files that should ignore.

Create a new file in the local repository -> name the file .gitignore

OR

Create the file using the command prompt

touch .gitignore

Then open the .gitignore file using the text editor and add the files that should ignore according to the given format.

git commit — used to commit (take the snapshot of the changes) the changes in the tracked files to the repository.

git commit -m “First release of Hello World!” — by this, the staging environment has been committed to the repo with the message. -m “commit message” used to add a message to the commit.

Get Shadhani rushanthi’s stories in your inbox

Join Medium for free to get updates from this writer.

git commit -a -m “Updated index.html with a new line” — commit the changes by skipping the staging environment.

Warning: Skipping the Staging Environment is not generally recommended. Skipping the stage step can sometimes make you include unwanted changes.

git log- used to view all the git commits done Each commit has a unique id.

git log : view logs in a more descriptive way

git log –oneline : view logs by log id in one line

Press enter or click to view image in full size

git log -p : view logs with changes

Press enter or click to view image in full size

git diff — used to check the difference between the current changes in the files with the last commit of that file.

Press enter or click to view image in full size

Remove tracked or commit file/folder

git rm filename.txt

Restore deleted tracked file/folder

git restore filename.txt

Rename git file/folder

git mv oldfilename.txt newfilename.txt

git branch — Used to check the available branches in the local git repository

With * it represents that currently you are in that branch

Create a new git branch

git branch branch_name

git switch- Used to change the current branch into another branch

git switch branch_name

git merge- Merge changes in other branches to the main branch.

git merge -m “merge message” branchNameToMerge

Clone the cloud GitHub repository to the local repository.

git remote- Used to clone the cloud GitHub repository to local repository

git remote add origin repository_url

You can get the repository URL below.

Press enter or click to view image in full size
Press enter or click to view image in full size

git push- Push all the committed changes in the local repository to the remote git repository.

git push -f — Forcly push the changes to the remote branch by overwriting and deleting the existing files/folders.

git push -u origin branchName — Useful when pushing a new branch, this creates an upstream tracking branch with a lasting relationship to your local branch

git push — all — Push all branches available in the local to the remote repo.

Press enter or click to view image in full size

Getting the changes in the remote repo to local & merge them to the local branch

git fetch

git merge

Instead of these two commands, you can use the command called git pull

git pull origin branch_name

Remove remote Git repository from the local repository

git remote remove origin

--

--

Shadhani rushanthi

Written by Shadhani rushanthi

Associate Software Engineer, Undergraduate with 2 years experience in developing cutting-edge software solutions. Background of working with REACT, MERN, .NET