Useful Git Commands
Tags: git, github, githunt, webdev
Published: 2021-06-28 16:57:42

Hi there! I have made this blog because I forgot some of the git commands every time while using git or GitHub. So I am going to put all the useful git command in this blog so that anyone who is going to get started with open-source get some help and whenever I need I can quickly find those at one place. 

> Git is so powerful. There are a lot of really cool features. Some other basic functions you'll want to learn is how to `git pull`, `git clone`, `git merge`, `git rebase`, `git stash`, `git checkout`, and `git branch`. Those are the ones that are needed for base level proficiency in git.

## Git Basics
**1.** To Create `empty git repo` in specified directory. Run with no arguments
```yml
git init
```
**2.** For `cloning` the repo onto your local machine
```yml
git clone <repo--HTTP or SSH>
```
**3.** To define `author name` to be used for all commits in current repo
```yml
git config user.name <name>
```
---
**4.** To `stage` all changes in `<directory>` or `<file>` for the next commit
```yml
git add <directory or file>
```
you can also use 
```shell
git add .
```
---
**5.** to `List` which files are staged, unstaged, and untracked 
```yml
git status
```

**6.** for `commit` the staged snapshot with a commit message
```shell
git commit -m "<message>"
```

**7.** to display entire commit history
```yml
git log
```

**8.** to show unstaged changes b/w your index and working directory or  to analyze the current state of the repo
```yml
git diff
```
---
## Git Branch
**1.** List all of the branches in your repo. Add a <branch> argument to create a new branch.
```git
git branch
```

**2.** Create and checkout a new branch named <branch>.
```yml
git checkout -b <branch>
```
`-b` flag is for checking out an existing branch

**3.** Merge <branch> into the current branch
```yml
git merge <branch>
```
---

## Remote Repositories

**1.** Create a new connection to a `remote repo`.
```yml
git remote add <name> <url>
```
after doing this you can use `<name>` as a shortcut for `<url>` in other commands.

**2.** Fetches a specific <branch>, from the repo. Leave off `<branch>` to fetch all remote refs
```yml
git fetch <remote> <branch>
```
**3.** Fetch the specified remote's copy of the current branch and immediately merge it into the local copy.
```yml
git pull <remote>
```
**4.** `Push` the branch to <remote>, along with necessary commit objects. Creates named branch in the remote repo if it doesn't exist
```yml
git push <remote> <branch>
```
---

## Git Rebase
**1.** Interactively rebase current branch onto `<base>`.Launches editor to enter commands for how each commit will be transferred to the new base
```yml
git rebase -i <base>
```

## Git Pull
**1.** Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches
```yml
git pull --rebase <remote>
```
## Git Push
**1.** to link a local branch with the remote branch. The -u flag is used to set origin as the upstream remote in your git config
```yml
git push -u <remote> <branch>
```

**2.** Forces the git push even if it results in a non-fast-forward merge. Do not use the `--force` or `-f` flag unless you're absolutely sure you know what you're doing
```yml
git push <remote> --force
```
or 
```yml
git push -f <remote> <branch>
```


## Undoing Changes

**1.** Create new commit that undoes all the changes made in <commit>, then apply it to the current branch. here we need to a commit reference id
```yml
git revert <commitId>
```
or `git revert Head` to create a new commit with the reverted changes which had been done in the last commit.

**2.** To remove file from staging area but leave the working directory unchanged. This unstages a file without overwriting any changes
```yml
git reset <file>
```

**3.** used to remove unwanted files from your working directory. `-n` flag is used for double checking before changing, to execute use `-f` in place of `-n`
```yml
git clean -n
```
---

## Rewriting History
**1.** Replace the last commit with the staged changed and last commit combined
```yml
git commit --amend
```

**2.** Rebase the current branch onto <base>. `<base>` can be a commit ID, branch name, a tag, or a relative reference to HEAD.
```yml
git rebase <base>
``` 
**3.** Show a log of changes to the local repository's HEAD. Add `--relative-date` flag to show date time info or `--all` to show all refs.
```yml
git reflog
```
---
That's all! I think this is more than enough git commands for getting started with open-source and in the last just look at the image below cuz, it is super helpful.

![for up and going](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irivrh1lv307qwmdqlv3.png)
  
## Thank You