Git for beginners | with simple examples.
Tags: github, git, beginners, webdev
Published: 2023-05-02 17:34:15

Hello there! As an experienced developer with over 8 years of experience using Git, I'd like to share with you some insights on this powerful version control system.
Git is a must-know tool for any developer, whether you're working on your own project or collaborating with others. With Git, you can easily keep track of changes to your code, share your work with others, and collaborate on projects with ease.

![ some insights on Git- powerful version control system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zrlgpyg0v1fiicqp3bi.png)

Let's start with the basics. Git is a version control system that allows developers to keep track of changes to their code over time. It was created by Linus Torvalds in 2005 to manage the development of the Linux kernel.
So, what exactly is version control? Well, think of it like a time machine for your code. With Git, you can easily go back in time to any previous version of your code, see what changed, and even revert to an earlier version if necessary.

![what is version control](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0esisb4bt8mx0e6506cy.jpg)

Now, let's talk about Git's decentralized model. This means that each developer has their own copy of the repository on their local machine. This allows for greater flexibility and faster collaboration. For example, if you're working on a feature and your teammate is working on a bug fix, you can both work on your respective tasks without interfering with each other's work. When you're ready to share your changes, you can push them to a shared repository that your teammates can then pull from.

Another powerful feature of Git is its branching model. This allows developers to create new branches to work on specific features or bug fixes. This allows for parallel development and makes it easier to isolate changes. For example, let's say you're working on a new feature for your project. You can create a new branch for that feature, work on it without affecting the main branch, and then merge it back into the main branch when you're done.

![powerful feature of Git is its branching model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tkmbtam7x9rpqwwy1e7.png)
One thing to keep in mind with branching is that it's important to keep your branches up to date with the main branch. This is where Git's merge and rebase features come in handy. Merge combines two or more branches together, while rebase moves your changes to the tip of the main branch.

Finally, it's worth noting that Git is free and open-source software, which means that anyone can use it and contribute to its development. It's also widely used in the tech industry and is an essential tool for many developers.

To get started with Git, you'll need to install it on your local machine. Once you've done that, you can create a new repository, add your code to it, and start making changes. Here's a simple example:

### 1. Create a new repository on your local machine using the **git init** command.

- Create a new directory on your computer where you want to store 
  your project.
- Open a terminal window or command prompt and navigate to the new directory.
- Type the following command to initialize a new Git repository:

```
git init
```

This creates a new Git repository in the current directory.

### 2. Add your code to the repository using the `git add` command.
 Add new file to the repository using the following command:
	
```
git add <file name>
```
This tells Git to start tracking changes to the specified file.

### 3. Commit your changes using the `git commit` command. 
Be sure to include a meaningful commit message that  describes the changes you've made clearly to you and other developers. Like 
         
```
git commit -m "Updated the header section of index.html with new logo and navigation links" 

```

### 4. Create a new branch using the `git branch` command.

For example, if you want to create a branch called **"feature-branch"**, you would run 

```
git branch feature-branch
```

### 5. Switch to the new branch using the `git checkout` command.

Now you can switch to the branch created in 4 using below command 

```
git checkout feature-branch
```

### 6. Make changes to your code and commit them using the `git commit` command.

once you have made changes in the branch you can run 

```
git status
```
to see the status of the branch. The above command will tell you which files have been modified and need to be committed.

### 7. Switch back to the main branch using the `git checkout` command.

###8. Merge your changes from the new branch using the `git merge` command.

## Collaborating with Git

One of the great things about Git is that it allows developers to collaborate on projects. Here's an example of how that works:

- Create a new repository on a hosting service like GitHub or GitLab.

- Invite other developers to join your repository.

- Each developer clones the repository to their local machine using the following command:

```
git clone <repository URL>
```
- Contribute to repository.
Each developer can now make changes to the code and commit their changes to the repository.
To see the changes made by other developers, each developer can use the following command:

```
git pull
```
This downloads the latest version of the repository from the hosting service.

Of course, this is just the tip of the iceberg when it comes to Git. There's a lot more to learn, but hopefully, this gives you a good starting point. If you're looking for a graphical user interface to help you get started, I recommend checking out GitHub Desktop, GitKraken, or Sourcetree.

> In conclusion, Git is an essential tool for any developer, and I hope this blog post has given you a good introduction to its basic features. With Git