# Git Basics Cheat Sheet

 ## What is Git ?

Git is free and open source software for distributed version control: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.

## Why it is used 

Git is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source.

# 1 . GIT BASICS

```
git init <directory>
```
1 . Create empty Git repo in specified directory. Run with no
arguments to initialize the current directory as a git repository.

```
git clone <repo>
```
2 . Clone repo located at < repo > onto local machine. Original repo can be
located on the local filesystem or on a remote machine via HTTP or SSH

```
git config user.name <name>
```
3 . Define author name to be used for all commits in current repo. Devs
commonly use --global flag to set config options for current user.

```
git add <directory>
```
4 . Stage all changes in <directory> for the next commit.
Replace <directory> with a <file> to change a specific file.

```
git commit -m "<message>"
```
5 . Commit the staged snapshot, but instead of launching
a text editor, use <message> as the commit message.

```
git status
```
6 . List which files are staged, unstaged, and untracked.

```
git log
```
7 . Display the entire commit history using the default format.
For customization see additional options.

```
git diff
```
8 . Show unstaged changes between your index and
working directory.

# 2. UNDOING CHANGES

```
git revert <commit>
```
1 . Create new commit that undoes all of the changes made in
< commit >, then apply it to the current branch.

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

```
git clean -n 
```
3 . Shows which files would be removed from working directory.
Use the -f flag in place of the -n flag to execute the clean.

# 3. REWRITING GIT HISTORY 

```
git commit --amend
```
1 . Replace the last commit with the staged changes and last commit
combined. Use with nothing staged to edit the last commit’s message.

```
git rebase <base>
```
2 . Rebase the current branch onto < base >. < base > can be a commit ID,
branch name, a tag, or a relative reference to HEAD.

```
git reflog
```
3 . Show a log of changes to the local repository’s HEAD.
Add --relative-date flag to show date info or --all to show all refs.

# 4. GIT BRANCHES

```
git branch
```
1 . List all of the branches in your repo. Add a < branch > argument to
create a new branch with the name < branch >

```
git checkout -b <branch>
```
2 . Create and check out a new branch named < branch >.
Drop the -b flag to checkout an existing branch.

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

# 5 . REMOTE REPOSITORIES

```
git remote add <name> <url>
```
1 . Create a new connection to a remote repo. After adding a remote,
you can use < name > as a shortcut for < url > in other commands.

```
git fetch <remote> <branch>
```
2 . Fetches a specific < branch >, from the repo. Leave off < branch >
to fetch all remote refs.

```
git pull <remote>
```
3 . Fetch the specified remote’s copy of current branch and
immediately merge it into the local copy

```
git push <remote> <branch>
```
4 . Push the branch to <remote>, along with necessary commits and
objects. Creates named branch in the remote repo if it doesn’t exist.







