# GIT - Version Control

**Version control** is a system that records changes to a file or set of files over time so that you can recall specific versions later. Versioning of files/projects.

Curious on your own, go through [this git handbook](https://guides.github.com/introduction/git-handbook/).

There are different [types of version control systems](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control). **Distributed Version control system** remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer.

![file versioning](https://4037691551-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LbyiXaZSs-vDRSOJfOk%2Fuploads%2Fgit-blob-e44763de766083151d1f677237b1ff3fb6442208%2FScreenshot%202019-04-19%20at%2007.23.37.png?alt=media)

### Do I Really need Version Control?

Yes, you do. Real time projects worked on in parallel teams will always require you revert back and forth to an older version of your code. Also it independence through branching, hence you can comfortably work on your task in a team without much conflicts. `Don't worry you will understand with time`

### What is Git?

Git is the most commonly used version control. Git is an example of DVCS - `Distributed Version Control System` . Experienced software developers understand how to use git well, it's a well represented field.

Git features performance, security and flexibility.

### Installing Git

It's good to use git while you have an account at [Github](https://github.com) (remember the "server"). Go ahead and signup on [github.com](https://github.com)

#### Windows installation

Download <https://git-scm.com/download/win> and install it. Read about [git for windows.](https://gitforwindows.org)

After installation, open git. Also you can create a folder for your projects, right click on it, click on `git bash` to execute git. Configure according to your credentials below.

#### Linux Installation(Debian e.g. Ubuntu)

On your terminal, run this&#x20;

```
sudo apt install git-all
```

Configure according to your credentials below.

#### Mac-OS installation

The easiest way to install is to install xcode. Check if you have git, if you don't it will prompt you to install xcode.

```
git --version
```

Configure according to your credentials below.

### Setting up git

You have to identify yourself to git. Remember your github credentials, time to use. On git bash, type the following with your credentials.

```
git config --global user.name "Your username"
git config --global user.email Your-email-address
```

To check your credentials

```
git config --list
```

{% hint style="info" %}
Git uses linux commands.
{% endhint %}

### Where to start

Two ways:

1. **Create a project(repo) on Github**

A **repository**(repo) is like a folder for your project. Login to your Github account and follow the steps below:

* [ ] In the upper-right corner of any page, click +, and then click **New repository**.
* [ ] In the Owner drop-down, select the account you wish to create the repository on.
* [ ] Type a name for your repository, and an optional description.
* [ ] Choose to make the repository either public or private. **Public repositories** are visible to the public, while **private repositories** are only accessible to you, and people you share them with.&#x20;
* [ ] Optionally choose to add README(instructions doc for your project), license and a gitignore (what should not be sent to remote server)
* [ ] When you're finished, click **Create repository**.
* [ ] To get started locally, click on **clone/download** on your created repo. Copy the link.

![](https://4037691551-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LbyiXaZSs-vDRSOJfOk%2Fuploads%2Fgit-blob-ed9de84bf7f7dca92d145d45689cce17b2d12f93%2FScreenshot%202019-04-19%20at%2008.28.03.png?alt=media)

* [ ] Paste the link after `git clone ...`&#x20;
* [ ] check into the cloned repo `cd cloned-repo-name` . You can do `ls` to see the name of the cloned repo.
* [ ] Open your text editor here e.g. `code .` to open VSCode while in that project.
* [ ] `Happy Coding`&#x20;
* [ ] After you satisfy that you have done enough coding, then it's time to send your codes to Github.
* [ ] `git status` to know what files you have edited
* [ ] `git add .` to add all files or `git add filename.extension` to add single file
* [ ] `git commit -m "editing of files done` to add a snapshot for Github recognition
* [ ] `git push origin master` to send codes to Github. **Note** `master` can be replaced with your branch name.
* [ ] `emocionado (es) - Exicited (en)`&#x20;

&#x20;    2\. **Initialise a project locally**

Let's say you had an existing project locally and want on Github for some reason. Steps:

* [ ] First create a new repo on Github, in the upper-right corner of any page, click +, and then click **New repository**.
* [ ] Open terminal(linux/mac), (windows) right click on project and open gitbash.
* [ ] Navigate to your local project (cd .....)
* [ ] Initialize the local directory as a Git repository. `git init`&#x20;
* [ ] Add the files in your new local repository. This stages them for the first commit. `git add .`&#x20;
* [ ] Commit the files that you've staged in your local repository. `git commit -m "Initial Commit"`&#x20;
* [ ] On github repo, at the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL.
* [ ] In Terminal, add the URL for the remote repo where your local repository will be pushed.

  ```
  git remote add origin remote repository URL
  # e.g. git remote add origin remote https://github.com/Nicanor008/Mentorship-101.git
  ```
* [ ] Push the changes in your local repository to GitHub.

  ```
  git push -u origin master
  ```

### Common Git commands used

```
cd - change directory e.g. (cd .. -moves back one directory)
ls - list all files and directories
pwd - print working directory (path)

git init - initialise a local project for version control
git clone - Taking a remote project to your local computer so you can
                start editing.
git status - to know status of your files after making changes
git add - add files on staging i.e. sending files from local server to 
                remote server but not yet in remote
git commit - appends a message to files to be sent to the remote server
                e.g. git commit -m "message";
git push - Sends the staged files to the remote server
git pull - brings remote(server) changes to local(your project)
```

{% hint style="info" %}
There are a lot of git commands. For beginners, don't hurry to know all of them. but if you much curious, check out [here](https://github.com/joshnh/Git-Commands) and [this](https://readwrite.com/2013/09/30/understanding-github-a-journey-for-beginners-part-1/) explains meaning of all git commands
{% endhint %}

### Time For More

Now that you've understood all the above, it's now time to understand key concepts in Version Control. These entails:

* Branching
* Merging and merge conflicts
* Commit History
* Github Pages
* Rebase
* Squash

### Git Branching

This is an important aspect when it comes to code collaboration. It allows you to test out what your working on independently while in a team. Create your own branch and later that branch will be combined with the major team branch

`Master` branch is always created by default by github.&#x20;

`Base branch` is the default branch.

To create your own branch , make sure you are in a base branch, then in terminal/gitbash

```
git checkout -b my-new-branch-name-here
```

Make changes to your code and then push them to github. There will be no effect on the base branch until merging is done.

### Curious

Want to learn more, go through [this git handbook](https://guides.github.com/introduction/git-handbook/).

### A Little Exercise

I believe you have learnt something about project collaboration. Let's see what you have got.

{% hint style="info" %}
Create your own branch
{% endhint %}

* [ ] Go to [Mentorship 101 github repo](https://github.com/Nicanor008/Mentorship-101)
* [ ] Clone it
* [ ] Create a some new files, you can write anything :grin: in the files
* [ ] Push them to github

{% hint style="info" %}
You made it. Good Job. Do more practices. Happy git-ting
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mentorship101.gitbook.io/mentorship-101/git-version-control.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
