GIT CHEATSHEET: ALL THE COMMANDS YOU NEED TO FAMILIARIZE WITH GIT
Tags: git, github, programming, codenewbie
Published: 2021-09-01 07:23:26

## INTRODUCTION
Install git, check (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)  for easy and clear installation.

After installation, ensure you configure your *username* and *email* before making any commit. It allows commits to have right author name and email associated to them. However, this has nothing to do with authentication when pushing to a remote repo.

## SETTING YOUR USER NAME AND EMAIL
`Git config --global user.name "my_username"`
`Git config --global user.email my_mail@example.com`

The above commands declares identity for all repositories, to declare an identity for a single repository, use git config inside a repo

Example:
```git
cd my_repository
git config user.name "my_work_login"
git config user.email my_work_mail@example.com
```

Remove a global identity
```git
git config --global --remove-section user.name
git config --global --remove-section user.email
```
However, to force git to look for your identity only within a repository's settings, not in the global config
```
git config --global user.useConfigOnly true
```
**Tip:** Settings stored in a repository's config file will take precedence over the global config when you use that repository

## CREATING, ADDING AND COMMITING FILES
`git --version` -  verifies if git is installed

### Configuring user information, initializing and cloning repositories (starting a project)

`git init` – initiates or creates an empty repository. It creates a hidden folder(.git) necessary for git to work

`git clone<project_name>` - copies an existing Git repository from a server to the local machine. It is followed by a url for the repository that is being cloned.

Example: git clone https://github.com/my_username/my_project.git
NB: You can also use the ssh version of the command:
git clone git@github.com:my_username/my_project.git
However, GitHub recommends using https rather than ssh.

### Working with snapshots and the Git staging area
`git status` - checks what files Git will add to your new repository

`git add <file>` - tells Git which of the files to place into version control

`git add .` - tell Git place everything in the directories and the sub-directories into version control.

`git reset <file>` - unstage a file while retaining the changes in working directory

`git diff` - difference of what is changed but not staged

`git diff --staged` - difference of what is staged but not yet committed

**NB:** create and populate a file named .gitignore before running the add command for files that you want to exclude from being tracked by Git

`git commit -m "Initial commit"` – commits all the added files
A commit creates a snapshot of your entire project. After a commit, the project is ready to upload to a remote repository

`git remote add origin <https://<your-git-service-address>/owner/repository.git>`

NB: You have to create a repository in your git service before adding the created remote.

## BRANCHING AND MERGING
`git branch` - list your branches. a * will appear next to the currently active branch

`git branch [-a]` - List all local branches in repository.With -a: show all branches(with remote).

`git branch -d [name]` - Remove selected branch, if it is already merged into any other. -D instead of -d forces deletion.

`git checkout [-b][branch_name]` - Switch working directory to the specified branch. With -b: Git will create the specified branch if it does not exist.

`git branch <branch-name>` - create a new branch at the current commit

`git checkout` - switch to another branch and check it out into your working directory

`git merge <branch>` - merge the specified branch’s history into the current one

`git log branchB..branchA` - show the commits on branchA that are not on branchB



## SETTING UP THE UPSTREAM REMOTE
if you clone a fork, most probably you will not be able to push access to the upstream repo, so you need both your fork but be able to fetch the upstream repository.

`git remote -v` - check the remote names

example: 
```git
git remote -v 
origin https://github.com/myusername/repo.git (fetch)
origin https://github.com/myusername/repo.git (push)
upstream # this line may or may not be here
```
If upstream is there already, set the URL
```git
git remote set-url upstream https://github.com/projectusername/repo.git
```
if it is not there, or you want to add a friend’s fork
```git
git remote add upstream https://github.com/projectusername/repo.git
git remote add emma_donery https://github.com/emma_donery/repo.git
```

## FINDING INFORMATION ABOUT A COMMAND
The help command gives details about what the command does, available options and
other documentation.
Syntax:
git command_name --help
example
`git push --help`

## GETTING UPDATES FROM OTHER REPOS AND UPDATING THE LOCAL REPOSITORY

`git remote add [alias] [url]` - add a git URL as an alias

`git fetch [alias]` - Fetch down all the branches from that Git remote

`git merge [alias]/[branch]` - merge a remote branch into your current branch to bring it up to date

`git push [alias] [branch]` - Transmit local branch commits to the remote repository branch

`git pull` - fetch and merge any commits from the tracking remote branch