Git Cheat Sheet
Tags: github, git, versioncontrol, devops
Published: 2021-07-29 08:55:31

**Important Commands of  Git.** 
1)What is Git?
2)Why should we use git and GitHub?
3)How to Download?
4)Git Operations?
  * How to create a git Directory?
  * How to Stage?
  * How to do your first Commit?
  
5)Important Git commands?

**1. Git-** Git is a version control system (VCS) that lets you manage and keep track of your changes that you make to files. 

**2. Why we use:-** GitHub is a website for hosting projects that use git.
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.

**3. [Download] (https://git-scm.com/)-** Click the Blue mark download this will take you to the download page of git.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cm7w2f2hkr6k0ufrbwvh.png)
**4. Git Operations -**There are main three operations that every programmer have to do in Git.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqa3lll5s5x84igfhikd.png)
**6. Git commands -** The  most used commands are 

####i) CREATE 
`Create a new local repository`
```
git init
``` 
`Clone an existing Repository`
```
git clone ssh://user@domain.com/repo.git
``` 
####ii)LOCAL CHANGES
`Change files in your working directory`
```
git status
```
`Changes to tracked files`
```
git diff
```
`Add all current changes to the next commit`
```
git add .
```
`Add some changes in <file> to the next commit`
```
git add -p <file>
```
`Commit all local changes in tracked files`
```
git commit -a
```
`Commit previously staged changes`
```
git  commit
```
`Change the last commit`
_Don't amend published comments_
```
git commit --amend
```

####iii)COMMIT HISTORY

`Show all commits , starting with newest`
```
git log
```
`Show changes over time for a specific file`
```
git log -p <file>
```
`Who changed what and when in <file>`
```
git blame <file>
```
####iv) BRANCHES & TAGS

`List of all existing branches`
```
git branch -av
```
`Switch HEAD branch`
```
git checkout <branch name>
```
`Create a new branch based on your current HEAD`
```
git branch <new-branch>
```
`Create a new tracking branch based on a remote branch`
```
git checkout --track <remote/branch>
```
`Delete a local branch`
```
git branch -d <branch name>
```
`Mark the current commit with a tag`
```
git tag <tag-name>
```
####v) Update & Publish
`List of all Currently configured remotes`
```
git remote -v
```
`Show information about a remote`
```
git remote show <remote>
```
`Add new remote repository ,named <remote>`
```
git remote add <shortname> <url>
```
`Download all changes from <remote>, but don't integrate into HEAD`
```
git fetch <remote>
```
`Pull all changes from remote, merge/intregrate into HEAD`
```
git pull <remote> <branch name>
```
`Publish local changes on a remote`
```
git push <remote> <branch name>
```
`Delete a branch on the remote`
```
git branch -dr <remote/branch name>
```
`Publish your tags`
```
git push --tags
```
####vi) MERGE & REBASE
`MERGE <BRANCH> INTO YOUR CURRENT HEAD`
```
git merge <branch name> 
```
`Rebase your current HEAD onto <branch>`
_Don't rebase publish commits _
```
git rebase <branch>
```
`Abort a rebase`
```
git rebase --abort
```
`Continue a rebase after resolving conflicts`
```
git rebase --continue
```
`Use your configured mege tool to solve conflicts`
```
git mergetool
```
####vi)UNDO
`Discard all local changes in your working directory`
```
git reset --hard HEAD
```
`Discard local changes in a specific file `
```
git checkout HEAD <file>
```
`Revert a commit `
```
git revert <commit>
```
`Reset your HEAD pointer to a previous commit `
```
git reset --hard <commit>
```
`Preserve all changes as unstaged changes`
```
git reset <commit>
```
`Preserve uncommitted local changes`
```
git reset --keep <commit>
```