Git & GitHub Essentials: Understanding Version Control and Collaboration
Tags: github, git, beginners, learning
Published: 2024-05-23 12:05:48

## Version Control

Imagine Doctor Strange casting spells and constantly modifying reality (the code) to defeat a foe (a bug). Unlike the film's time loop, Strange (the developers) must keep track of the changes. That's a subtle way of introducing version control, so let's describe it as a system for tracking and managing changes to code or other collections of files. It enables many individuals to work on the same project without overwriting each other's modifications, and it includes a method for reverting to prior versions of the project if necessary.

There are some tools that provide version control.

- **Git:** It’s distributed version control system that allows multiple people to work on a project at the same time without overwriting each other's changes. It is a popular choice among software developers.
- **Subversion (SVN)**: It’s a centralized version control system that keeps all the versioned files in a central repository. Often used for large-scale projects where centralized system is preferred.
- **Perforce**: It’s a version control system that is designed for large-scale software development projects. It offers features such as atomic commits, branching and merging, and access control.

Google uses its own version control system called Piper, which is a centralized version control tool. Piper is a renowned version control tool that Google uses as a vast repository, distributed over around ten Google data centers.

There are several hosting platforms available for version control systems. Here are a few popular ones:

- **GitHub**: GitHub is a web-based version control repository hosting service that is one of the most popular platforms for version control. It offers both public and private repositories, issue tracking, wikis, and collaboration features.
- **Bitbucket**: Bitbucket is another web-based version control repository hosting service that is owned by Atlassian. It supports both Git and Mercurial version control systems.
- **GitLab**: GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager. It offers features such as continuous integration, continuous deployment, and issue tracking.

Mostly we will be understanding version control system in term of git and GitHub.

### Install Git

Here is official guide to install git to your operating system: 

Official page: [Git - Installing Git (git-scm.com)](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

GitHub Guide: https://github.com/git-guides/install-git

## How to introduce version control into your project?

Introducing version control into your project, git as control system.

The **`git init`** command is used to initialize a new Git repository in your local system. It creates a hidden folder named **`.git`** in the current directory, which contains all the necessary metadata required for version control.

```bash
git init 
```

### Staging, Commits in Git

 In Git, a commit is a snapshot of your project at a particular point in time. When you commit, you're creating a new version of your project that you can refer back to later.

To commit changes in Git, you first need to stage them. Staging is the process of selecting which changes you want to include in your next commit. When you stage changes, you're essentially creating a "staging area" where you can group together changes that you want to commit.

To stage changes in Git, you can use the **`git add`** command. For example, if you've made changes to a file called `name.txt`, you can stage those changes with the following command:

```bash
# adding all file to stage
	git add .

# adding invidual file to stage  
	git add name.txt

# adding photograph to album
	git commit -m "name.txt file added"

# remove from stage 
	git restore --staged name.txt
```

### Branches in Git

In Git, a branch represents an independent line of development. It's essentially a movable pointer to a specific commit. When you create a new branch, Git creates a new pointer that moves independently of any other branches.

![Untitled](https://i.ibb.co/PzykZ4w/github.png)

Here's an example of how to create a new branch in Git:

```bash
git branch <new-branch-name>
```

This creates a new branch with the specified name, but it doesn't switch to that branch yet. To switch to the new branch, you can use the **`git checkout`** command:

```bash
git checkout <new-branch-name>
```

To create and switch into new branch in same command:

```bash
git checkout -b <new-branch-name>
```

Once you've switched to the new branch, you can make changes and commit them just like you would on the master branch.

Here's an example of how to merge a branch back into the main branch:

```bash
git checkout main
git merge <branch-to-merge>
```

Here's an example of how to delete a branch in Git:

```bash
git branch -d <branch-to-delete>
```

This deletes the specified branch. However, if the branch has not been merged yet, Git will prevent you from deleting it. In that case, you can use the **`-D`** flag to force the deletion

```bash
git branch -D <branch-to-delete>
```

## Git Workflow

A typical Git workflow involves several steps, including cloning a repository, making changes, staging those changes, committing them, and pushing them to a remote repository. Here's an example of what that might look like:

### Clone a repository:

```bash
git clone https://github.com/user/repo.git
```

### **Create a branch**

The main branch is usually called `main`. We want to work on another branch, so we can make a pull request and make changes safely. To get started, create a branch off of `main`.  Typically naming convention for branch after a feature or fix.

For feature `feat/<name-of-feature>` 

For fixing a bug `fix/<name-of-fix>`

### **Make changes to file (and make a commit)**

Once you've created a branch and moved the HEAD pointer to it by "checking out" to that branch, you're ready to make changes in repository.  Next, save your changes. You're ready to start the commit!

```bash
git add .
git commit -m "descriptive commit message"
```

### **Push your changes to the remote**

So far commit is made locally, you're the only one that can see it. When you're ready to push your changes to the remote repository, you can use **`git push origin <branch-name>`**. This will push your changes to the remote repository, allowing others to see and use your changes.
`origin` is the default name for the remote repository from which you cloned your local repository. It is a short name to be used when reading and writing to that remote.

```bash
git push origin <branch-name>
```

You can change or remove remote repository using 

```bash
# removing and re-adding remote:
git remote set-url origin git://<new-url-here>

#To remove remote use this:
git remote remove origin
```

### **Open a pull request**

Pushing a branch, or new commits, to a remote repository is enough if a pull request already exists, but if it's the first time you're pushing that branch, you should open a new pull request. Pull request is a comparison of two branches that allows users to propose changes to a repository and request that those changes be merged into the main branch.

### **Merge into `main`**

Once repository owner or team decide that the pull request looks good, they can merge it. By merging, you integrate the feature branch into the other branch (most typically the `main` branch). 

If you choose not to merge the pull request, you can also close pull requests with unmerged changes

```bash
git checkout main
git merge <branch-to-merge>
```

## Learning by doing

Learning by doing is the greatest way to learn anything.

**[LearnGitBranching](https://learngitbranching.js.org/) :**An interactive git visualization and tutorial. Aspiring students of git can use this app to educate and challenge themselves towards mastery of git!

## Conclusion

Overall, Git is a powerful version control system that allows you to track changes to your code over time, collaborate with others, and maintain a history of your code. By following a typical Git workflow, you can make the most of Git's features and ensure that your code is well-organized and easy to manage.

For additional articles, you can visit the [blog.viditkushwaha.com](https://blog.viditkushwaha.com).