How-tos of git: git commands you should know as a beginner
Tags: git, tutorial, beginners, cheatsheet
Published: 2019-12-25 11:00:18

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
 
```javascript
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](https://thepracticaldev.s3.amazonaws.com/i/0816px6djbnephsm7wlm.png)

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

```javascript
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

```javascript
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

```javascript
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

```javascript
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

```javascript
git commit -m [commit message] 
```
For detailed info: __git commit -help__

__7)__ How to push changes to repo

```javascript
git push
```
For detailed info: __git push -help__

__8)__ How to save changes temporarily and retrieve afterwards

```javascript
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
```javascript
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

```javascript
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

```javascript
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

```javascript
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.