DEV Community

Rishi Ezhava
Rishi Ezhava

Posted on

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

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For detailed info: git add -help

6) How to commit staged files

git commit -m [commit message] 
Enter fullscreen mode Exit fullscreen mode

For detailed info: git commit -help

7) How to push changes to repo

git push
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
rebaiahmed profile image
Ahmed Rebai

I thinks this link is helpful:github.com/joshnh/Git-Commands