Chapter 27: Using git

 

In this chapter, we’ll explore Git, a distributed version control system that helps you track changes in your code, collaborate with others, and manage projects efficiently. By the end of this chapter, you’ll be able to create repositories, commit changes, work with branches, and collaborate using Git.


1. Why Learn Git?

Git is an essential tool for: 
- Version Control: Track changes in your code over time.
- Collaboration: Work with others on the same project without conflicts. 
- Backup: Safely store your code in remote repositories. 
- Branching: Experiment with new features without affecting the main codebase.


2. Getting Started with Git

Installing Git

Most Linux distributions include Git in their package repositories. Install it using your package manager: - 
Debian/Ubuntu:$ sudo apt install git

Red Hat/Fedora: $ sudo dnf install git

Configuring Git

Set your username and email for commits:

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"

Checking Git Version

Verify the installation and check the Git version:

$ git --version

3. Creating and Cloning Repositories

Creating a New Repository

Initialize a new Git repository in your project directory:

$ git init

Cloning a Repository

Clone an existing repository from a remote server:

$ git clone https://github.com/username/repository.git

4. Basic Git Workflow

Tracking Changes

  • Check Repository Status
    $ git status
  • Add Files to Staging Area
    $ git add file.txt
  • Commit Changes
    $ git commit -m "Your commit message"

Viewing Commit History

View the commit history of your repository:

$ git log

5. Working with Branches

Creating a New Branch

Create a new branch to work on a feature or fix:

$ git branch new_feature

Switching Branches

Switch to an existing branch:

$ git checkout branch_name

Merging Branches

Merge changes from one branch into another:

$ git checkout main
$ git merge new_feature

Deleting a Branch

Delete a branch after merging:

$ git branch -d branch_name

6. Remote Repositories

Adding a Remote

Add a remote repository:

$ git remote add origin https://github.com/username/repository.git

Pushing Changes

Push your local changes to the remote repository:

$ git push origin main

Pulling Changes

Pull the latest changes from the remote repository:

$ git pull origin main

7. Resolving Conflicts

Identifying Conflicts

When merging branches, conflicts may occur. Git will mark the conflicting lines in the affected files.

Resolving Conflicts

  1. Open the conflicting file and edit it to resolve the conflict.
  2. Add the resolved file to the staging area: $ git add file.txt
  3. Complete the merge: $ git commit

8. Practical Examples

Creating a Feature Branch

  1. Create a new branch for a feature: 
    $ git branch feature_branch
  2. Make changes and commit them: 
    $ git add . $ git commit -m "Added new feature"
  3. Merge the feature branch into main
    $ git checkout main $ git merge feature_branch

Collaborating on a Project

  1. Clone a remote repository: 
    $ git clone https://github.com/username/repository.git
  2. Make changes and push them: 
    $ git add . $ git commit -m "Updated project" $ git push origin main

Interactive Git tools like lazygit and tig provide visual interfaces for managing commits, branches, and merges, making Git more accessible for beginners. Git hooks automate tasks like linting and testing before commits or pushes, ensuring consistent code quality. 

By leveraging these modern tools and hooks, you can streamline your version control workflow, reducing the complexity of managing repositories and fostering good version control practices from the start.

$ lazygit


9. Practice Time!

Let’s put your new skills to the test: 
1. Create a new Git repository and add a file to it. 
2. Commit the changes and view the commit history. 
3. Create a new branch, make changes, and merge it into the main branch. 
4. Clone a remote repository, make changes, and push them to the remote.


10. Conclusion

Git is an indispensable tool for version control and collaboration. By mastering Git, you can efficiently manage your projects, collaborate with others, and maintain a history of your work. Whether you’re working on a small personal project or a large team effort, Git will help you stay organized and productive.


Prev: Chapter 28 | Next: Conclusion