How-tos of git: git commands you should know as a beginner

ezhurik profile image Rishi Ezhava ・2 min read

If you have heard about git and wished that you had a quick glimpse of cheat codes of it then here it is for you.

Here I am listing some commands which you might use daily when working with git.

1) How to change the current username and email

git config --global user.name [Your username]
git config --global user.email [Your email]

For detailed info: git config -help

2) How to clone an existing project

Go to the repo and click on 'clone or download' and copy the link of repo

Alt Text

Move into the directory in which you have to clone the repo

git init // Create an empty Git repository or reinitialize an existing one

git clone 'https://github.com/gitrepo/repo.git' //Clone a repository into a new directory

3) How to list all branches

git branch  // list all branches of the local repo. Branch with asterisk denotes current branch

git branch -a // list all branches of the local and remote repo

For detailed info: git branch -help

4) How to switch to another branch

git checkout [branch name]  //switches to the desired branch of your repository

For detailed info: git checkout -help

5) How to add files to staging state

git add .  //adds all changed files to the staging state

git add [file 1] [ file 2] ... [file n] //adds multiple files to staging

For detailed info: git add -help

6) How to commit staged files

git commit -m [commit message] 

For detailed info: git commit -help

7) How to push changes to repo

git push

For detailed info: git push -help

8) How to save changes temporarily and retrieve afterwards

git stash // all uncommitted local changes have been saved 

git stash pop // The "pop" flag will reapply the last saved state

For detailed info: git stash -help

9) How to create a new branch and push it to remote

git checkout master // checkout to the master branch

git pull // pulls the latest change from the master if any

git checkout -b [branch name] // creates a branch locally

git push -u origin [branch name] // pushes the branch to remote 

10) How to delete branch locally and remotely

Note:If you are in the branch which you want to delete then checkout to another branch

git branch -d [branch name] // deleted branch locally

git push origin --delete [branch name] deletes the branch of remote

11) How to rename branches locally and remotely

git branch -m [old-name] [new-name] // renames branch locally

git push origin: [old-name] [new-name] // renames the branch of remote

12) How to upload your local project to remote

Go to the root of the project folder

git init

git add .

git commit -m "commit message"

git remote add origin [url]

git push -u origin master

Please ignore any typo and suggestions are welcomed to make this a better post.

Posted on by:


markdown guide