Useful commands for Git and GitHub

Sadat Jubayer

Sadat Jubayer

June 14, 2022

3 min read

First steps

Configure Git

  • After installing git, open Git Bash or any terminal and run the following commands.

  • Line started with # are comments

#set your name
git config --global user.name "firstname lastname"

#set email address
git config --global user.email "email-address"

#CRLF setup - for Windows
git config --global core.autocrlf true

#CRLF setup - for macOS
git config --global core.autocrlf input

#set automatic command line coloring for Git
git config --global color.ui auto

Most used commands

# Initialize git in current repository/folder
git init

# Adding a file to staging area
git add <filename>
#example- git add helloWorld.ts

# Add all files to the staging area
git add .

# show changed files in the working directory
git status

# Commit changes (creating snapshot of current files)
git commit -m "descriptive message"
#example- git commit -m "configaration file added"

# Adding a remote repository
git remote add <remote_name> <remote_url>
#example- git remote add origin https://github.com/username/rempository

# Push local branch to specified remote.
git push <remote_name> <branch_name>
#example- git push origin main

# Download new changes from the a remoote branch.
git pull <remote_name> <branch_name>
#example- git pull origin main

# Display commits history.
git log

Workflow 1

Setup a local repository and push it on Github (new project)

#staging all the files
git add .

#commit the statged files
git commit -m "first commit"

Now, you need to create a repository on GitHub - https://github.com/new

#add the remote url
git remote add origin https://github.com/username/repo_name

#if your branch is master on local, change it to main
git branch -m master main

#push the files on GitHub
git push origin main

Workflow 2

You already have a local repository; you want it to add a remote repository on it
#staging all the files
git add .

#commit the statged files
git commit -m "first commit"

#add the remote url
git remote add origin https://github.com/username/repo_name

#if your branch is master on local, change it to main
git branch -m master main

#push the files on GitHub
git push origin main

Workflow 3

You already have a repository on GitHub, and now you want to set it up on your local computer.

#clone the repository on your local computer
git clone https://github.com/username/repo_name

#After changing your files, you can stage files
git add .

#commit the staged files
git commit -m "new commit"

#push the files on GitHub
git push

I hope this article will help you to GITing better; happy Coding!