Sitemap
SAMEER MISTRY

SAMEER MISTRY

Passionate Software Engineer crafting innovative solutions through clean code and modern technologies. Lifelong learner driving impactful digital transformation

About Git and its Commands

4 min readApr 26, 2023

--

Version Control System:

Version control systems are a category of software tools that helps in recording changes made to files by keeping track of modifications done in the code. In a distributed version control system, every developer has a full copy of the project and project history. Unlike once popular centralized version control systems, Git is commonly used for both open-source and commercial software development, with significant benefits for individuals, teams, and businesses.

Press enter or click to view image in full size

About Git:

Git is an open-source distributed version control system. It is developed to coordinate the work with the developers. Git Foundation has many services like GitHub, GitLab, Bitbucket and etc. Git was created by Linus Torvalds in 2005 for Linux Kernal.

Basic Git commands:

git init

  • Initializes a brand new Git repository and begins tracking an existing directory.

git clone

  • It creates a copy of the repository from an existing URL. The clone includes all the project’s files, history, and branches.

git status

  • It shows the status of changes as untracked, modified, or staged.

git branch

  • It shows the branches being worked on locally.

git add

  • It’s used to add one or more files to the staging area. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work.

git remote add

  • This command is used to connect your local repository to the remote server. This command allows you to create, view, and delete connections to other repositories.

git commit

  • It saves the snapshot of the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that’s been staged git add will become a part of the snapshot with git commit.

git merge

  • It’s typically used to combine changes made on two different branches. For Example, a developer wants to merge a local branch with a staging branch.

git pull

  • It fetches and merges changes on the remote server to your working directory.

git fetch

  • It downloads branches and tags on the remote server to your working directory.

git log

  • It is the most useful command for Git. Every time you need to check the history, you have to use the git log command. The basic git log command will display the most recent commits and the status of the head.

git push

  • It is used to upload local repository content to a remote repository.

git stash

  • This command resets current branch changes. You can also stash specific branch

Here are examples of basic git commands.

Get SAMEER MISTRY’s stories in your inbox

Join Medium for free to get updates from this writer.

Example-Make a new repository and publish:

# create a new directory, and initialize it with git-specific functions
git init react-crud-app

# change into the react-crud-app directory
cd react-crud-app

# create the first file in the project
touch README.md

# git isn't aware of the file, stage it
git add README.md

# take a snapshot of the staging area
git commit -m "add ReadMe andinitial commit"

# provide the path for the repository you created on github
git remote add origin https://github.com/sameermistryh1533/react-crud-app.git

# push changes to specific branch
git push origin master

# pushes all the branches to the server repository.
git push --all

# push changes to github
git push --set-upstream origin maingit

Example-Setup to an existing repository:

#Replace with your clone url
git clone https://github.com/sameermistryh1533/dev-api.git

# change into the `repo` directory
cd repo

# create a new branch to store any new changes
git branch my-branch

# switch to that branch (line of development)
git checkout my-branch

# Take pull from remote branch
git pull origin branch-name

# take a snapshot of the staging area
git commit -m "first commit"

# push changes to specific branch
git push origin master

# pushes all the branches to the server repository.
git push --all

# push changes to github
git push --set-upstream origin maingit

Ignore files using .gitignore:

The purpose of .gitignore files is to ensure that certain files not tracked by Git remain untracked.

.gitignore

Git Configuration Commands:

git config credential.helper store

This command is used to configure Git to cache your credentials (i.e., your username and password) in memory for a period of time so that you don’t have to re-enter them every time you interact with a remote Git repository.

git config credential.helper store

#config username and password
git config --global user.name abc_@1533
git config --global user.email abc@123

About git rebase

git rebase

This command allows you to modify the history of a Git repository by moving, removing, or combining existing commits. The basic idea behind rebasing is to change the base of a branch from one point to another.

It’s essential for everyone involved to understand that although the branch appears the same, it’s made up of entirely new commits. When you perform a Git rebase, you are, in effect, rewriting history.

Example:

you have a development branch called development that you want to integrate into your main branch called master. You want to make sure that the changes in your feature branch are integrated into your main branch in a clean, linear way.

#Checkout to master branch
git checkout master

#pull changes from remote branch
git pull

#run rebase command to get changes from development branch
git rebase development

#Git will pause the rebase process and prompt you to resolve the conflicts manually.
#Once all conflicts have been resolved push into master branch
git push origin master

Ref:

https://git-scm.com/docs/git

--

--

SAMEER MISTRY
SAMEER MISTRY

Passionate Software Engineer crafting innovative solutions through clean code and modern technologies. Lifelong learner driving impactful digital transformation