learn basic git commands

learn basic git commands

This post contains basic commands of git.

There are many things you can do with React Refs. Refs in React are a powerful tool but they can be tricky and can be misused easily which can lead to lots of bugs and unexpected behaviours. we will go through most of the ways we can utilize the Refs for good

You can read more about git commands in git-scm docs.


Initial Config

Set username and password.

# Setting user name
git config --global user.name { github_username }

# Setting user email
git config --global user.email { github_email }

List all config

git config --list

Git clone

Clone repository

git clone ${ repository_url }

Using this command you can download and initialize a repository from a remote url into your local machine.

Clone repository with different name

git clone { repository_url } { folder_name }

Clone to a specific directory

git clone { repository_url } { directory_path }

Clone a specific branch

git clone -b { branch_name } { repository_url }

Intializing a repository from folder

Using git init

git init

To initialize a repository, Git creates a hidden directory called .git . This .git directory stores all of the objects and refs that Git uses and creates as a part of your project's history.

Connect initialized repository to remote url

git remote add origin { repository_url }

Commiting changes to repository

To Check status of files

git status

Green lines indicate files are added to staging area.

Red lines indicate files are not added to staging area.

Add files to staging area

# Add particular file to staging area
git add ${ file1.txt } ${ file2.txt }

# To add all files in that directory
git add .

Commit changes

git commit -m "Commit message"

Push code

# Push to master branch
git push origin master

# Push to a particular branch
git push origin { branch_name }

# Push all branches to remote repository
git push --all origin

Pulling code from a branch

# Pull from master branch
git pull origin master

# Push to a particular branch
git pull origin { branch_name }

Commands on Git Branch

List all branches

git branch

Green text indicates you are currently on that branch.

Create a new branch

git branch { branch_name }

Creates a new branch and switches to newly created branch

git checkout -b { branch_name }

Switch to another branch

git checkout { branch_name }

Delete a branch

git branch -d {branch_name}