Git/Github Quick Start Reference Document
Tags: github, git, beginners, tutorial
Published: 2023-06-02 12:35:53

#Git and GitHub

**GitHub**- A cloud platform used for collaborating that makes use of `git`.

Features of GitHub: `Issues`, `Discussions`, `Pull requests,` `Notifications`, `Labels`, `Actions`, `Forks`, `Projects`.

**Git**- Allows people to contribute to projects with the use of a distributed version control system (DVCS).

> In its most basic form git is a content tracker

#Basic Git Commands

- **`git init`** - Initialized an existing git repository or creates an empty git repository.

![Git Init Screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhhiev9vkav72lu2rjp4.png)

- **`git Status`** - Displays the status of the working tree. It could inform changes to be committed, changes not staged for commit, untracked files or if the tree is clean.

![Git Status Screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y7r0nl8gfdukvb0xn4y.png)

- **`git add`** - Add file contents to the index/working tree.

![Git Add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wp1c8dc7wzzpicey422p.png)

![Git Status Screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44azffwv3d2u87zdep0g.png)

- **`git commit`** - Record changes to the repository

![Git Commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxbw6zn5kg5shkpqgeae.png)

- **`git log`** -  Displays commit logs history for the current repository.

![Git log Screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7np81p6sa4yyxel4wi1.png)

#Branches

**Branch**- Git branches are a reference to a snapshot of your changes. By default git initializes with the branch name of `main` or `master`.

- **`git branch`** command can also show the list of existing branches for a repository, create new branches or delete existing ones.

###List
- The current selected branch in the screenshot bellow is `master ` that's why it has an asterisk by its name and its color is green.
![Git Branch List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1gy7nsq6n43hmwvpdvc.png)

###Create
- Creates a new branch based of the selected branch named `b1`.
![Git Branch Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuedjp42evbz6v4dn5m3.png)

###List
- Lists two branches from the repo.
![Git Branch List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9tg2k4y01limaxycvnk.png)

###HEAD
- The HEAD of the current project is a reference to the selected branch that is `refs/heads/master`.

- **`git switch`** - Allows one to switch the selected branch to an existing branch.

![Git Swtich](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz8sdnaba4rmq162kmc6.png)

- Now the HEAD of the branch is `refs/heads/b1`.

- **`git checkout`** - Also allows one to switch the selected branch to an existing branch o create new branches.

![Git Checkout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9v1pvceyz644tj7db01.png)

- Now the HEAD of the branch is back to `refs/heads/master`.


#Basic Git Knowledge

**Repository**- A project that may contain files and folders with versioning.

**Commit**- represents a change with an assigned a unique ID to 1 or many files.

**Pull Request**- Responsible for alerting that a commit is ready for merging between branches.

**Pull**- The `git pull` command retrieves all base branch commits that hasn't been applied to the current branch.

#Cloning and forking

**Cloning a Repository**- Represents a copy of the repository and its history to a machine.

**Forking a Repository**- Represents a copy of a repository in a GitHub account.

#Merging

**Merge**- Allows one to adds the changes in the pull request and branch into the `target` branch.

- Lets say you have two branches master and b1 with the same ReadMe.txt file and different content on them. Lets assume you want the content in the `b1` branch to merge into the `master` branch. To start the merge you can switch to the master then run the following command.

- **`git merge b1`**

![git Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ui3sonsmc3oew6f9tou9.png)

- As you can see from the screenshot above since the content differs there is a merge conflict that needs to be resolved.

- Now you can resolve the merge conflict in the ReadMe.txt file then add and commit the merged file into the `master` branch.

![Git Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5vc2j2semjq7qnhm2wa.png)

**Merge Conflict**- Occurs when a merge is attempted and changes to the same line of code from parallels changes.

**Resolving Merge Conflicts**- Git can generate a temporary file that contains differences from each branch. One can manually select what should or shouldn't be merged.

**Preventing Merge Conflicts**- pull updates frequently and  keep branches close to base.

#Rebasing

- Lets say you have two branches master and b1 with the same ReadMe.txt file and different content on them as seen in the figure bellow.

![2 branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gydcuntljimbjzvsfi95.png)

- The rebase command such as `rebase master` changes the base of the command to move b1 to the end of master as displayed in the image bellow:

![Rebase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrbfs9m0fyb4qjkbyw5w.png)