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.
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.
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 --versionCreating a New Repository
Initialize a new Git repository in your project directory:
$ git initCloning a Repository
Clone an existing repository from a remote server:
$ git clone https://github.com/username/repository.gitTracking Changes
$ git status$ git add file.txt$ git commit -m "Your commit message"Viewing Commit History
View the commit history of your repository:
$ git logCreating a New Branch
Create a new branch to work on a feature or fix:
$ git branch new_featureSwitching Branches
Switch to an existing branch:
$ git checkout branch_nameMerging Branches
Merge changes from one branch into another:
$ git checkout main
$ git merge new_featureDeleting a Branch
Delete a branch after merging:
$ git branch -d branch_nameAdding a Remote
Add a remote repository:
$ git remote add origin https://github.com/username/repository.gitPushing Changes
Push your local changes to the remote repository:
$ git push origin mainPulling Changes
Pull the latest changes from the remote repository:
$ git pull origin mainIdentifying Conflicts
When merging branches, conflicts may occur. Git will mark the conflicting lines in the affected files.
Resolving Conflicts
$ git add file.txt$ git commitCreating a Feature Branch
$ git branch feature_branch$ git add . $ git commit -m "Added new feature"main: $ git checkout main $ git merge feature_branchCollaborating on a Project
$ git clone https://github.com/username/repository.git$ git add . $ git commit -m "Updated project" $ git push origin mainInteractive 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.
$ lazygitLet’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.
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.