https://git-scm.com/download/linux (Git & Dev Tools)
Learn https://git-scm.com/download/linux (Git & Dev Tools) step by step with clear examples and exercises.
Title: Git & Developer Tools: A full guide for Linux Users
Why This Matters
Git is an essential tool for developers, providing a robust version control system that enables efficient management of code, collaboration with others, and streamlined project workflows. In this lesson, we'll delve into the world of Git and other developer tools for Linux users, offering a comprehensive understanding of these powerful technologies.
By mastering Git and related developer tools, you will be able to:
- Collaborate effectively with other developers on projects
- Manage your codebase efficiently, making it easier to track changes and rollback if necessary
- Simplify the process of deploying updates and hotfixes
- Facilitate continuous integration and delivery pipelines
- Contribute to open-source projects more easily
Prerequisites
- Familiarity with Linux command line interface (CLI)
+ Understanding basic commands like ls, cd, cp, mv, and rm
+ Knowledge of file permissions and ownership in Linux
- Basic knowledge of text editors like nano, vim, or emacs
- Familiarity with SSH for secure remote access
+ Set up SSH keys for seamless authentication with GitHub or other services
Core Concept
Introduction to Git
Git is a distributed version control system that allows developers to manage their code, collaborate with others, and maintain project history. It's designed to handle everything from small personal projects to large-scale collaborations.
Git Basics
- Repository: A local directory containing all the files of a project under Git version control.
- Working Directory: The local directory where you make changes to your project files.
- Staging Area (Index): An intermediate area between the working directory and the repository, used to prepare changes for commit.
- Commit: A snapshot of the current state of the staged changes in the repository.
- Branch: A separate line of development within a repository. The master branch is the main branch where all development happens.
- Remote Repository: A Git repository hosted on a server or cloud service like GitHub, Bitbucket, or GitLab. Local repositories can be pushed to and pulled from remote repositories to collaborate with others.
Installing Git on Linux
To install Git on your Linux system, open a terminal and run:
sudo apt-get update
sudo apt-get install git
For other distributions like Fedora or CentOS, use the appropriate package manager to install Git.
Basic Git Commands
- git init: Initialize a new Git repository in the current directory.
- git add : Stage a file for commit.
- git commit -m "commit message": Commit staged changes with a message.
- git status: Check the current state of the repository, including untracked files and changes waiting to be committed.
- git log: Display the commit history of the repository.
- git checkout : Switch to another branch in the repository.
- git pull: Fetch and merge changes from a remote repository.
- git push: Send local commits to a remote repository.
- git clone: Create a copy of an existing Git repository on your local machine.
- git merge: Combine changes from two branches into one.
- git branch: List all branches in the current repository or create a new branch.
- git stash: Temporarily save uncommitted changes to be applied later.
- git diff: Compare working directory files with the last commit, staged changes, or a specific commit.
- git revert: Revert specific commits and apply their opposite changes.
Core Concept (Expanded)
Git Workflow
A typical Git workflow consists of four main stages:
- Working Directory: Making changes to the project files
- Staging Area (Index): Preparing changes for commit
- Commit: Saving a snapshot of staged changes in the repository
- Remote Repository: Sharing commits with other developers and collaborating on projects
Git Branches
Branches allow you to work on different features or tasks simultaneously without affecting the main codebase (master branch). Common branching strategies include:
- Feature branches: Create a new branch for each feature or task, merge it back into the master branch when complete.
- Pull requests: Request that changes from your feature branch be merged into the master branch by another developer.
Git Merge Conflicts
Merge conflicts occur when changes made in different branches overlap in the same file(s). Resolving these conflicts requires manually editing the affected files and choosing which changes to keep or merge appropriately.
Worked Example
Let's create a simple Git repository, make some changes, and commit them.
- Create a new directory for the project:
mkdir my-project && cd my-project
- Initialize the Git repository:
git init
- Create a file named
readme.mdwith some content:
echo "# My Project" > readme.md
- Stage and commit the changes:
git add readme.md
git commit -m "Initial commit"
- Make some changes to
readme.md, such as updating the title or adding more content. - Stage and commit the updated file:
git add readme.md
git commit -m "Updated project description"
- Create a new branch called
feature/new-feature:
git checkout -b feature/new-feature
- Make additional changes to the
readme.mdfile in this new branch. For example, add a new section about a new feature. - Stage and commit the updated file in the new branch:
git add readme.md
git commit -m "Added new feature"
- Merge the new branch back into the master branch:
git checkout master
git merge feature/new-feature
In this example, we created a new branch for a feature, made changes on that branch, and then merged those changes into the master branch. If there were any conflicts during the merge, you would need to resolve them manually before completing the merge.
Common Mistakes
1. Forgetting to add files before committing
Always remember to stage any new or modified files using git add before committing them.
Subheading: Handling Large Files
When dealing with large files, it's best practice to either exclude them from version control (by adding them to the .gitignore file) or use a tool like Git LFS (Large File Storage) to manage binary files efficiently.
2. Committing unstaged changes
Before committing, ensure that all desired changes are staged with git add. Unstaged changes will not be included in the commit.
Subheading: Ignoring Changes by Accident
Accidentally ignoring changes can happen when a file is added to the .gitignore file or when using the --assume-unchanged option on a file. To unstage a file with this option, use git update-index --no-assume-unchanged.
3. Ignoring file types
By default, Git ignores certain file types like executables and binary files. To include these files in your repository, create a file named .gitignore and list the file extensions you want to include.
Subheading: Handling Binary Files with Git LFS
For large binary files, consider using Git LFS (Large File Storage) to manage them efficiently. This tool allows developers to store large files in a separate storage system while keeping small pointers in the Git repository.
4. Deleting files without checking their status
Before deleting a file, ensure that it is not staged for commit using git status. If the file is staged, unstage it first with git reset or git restore.
FAQ
Q1: What is Git used for?
A1: Git is a version control system that helps developers manage their code, collaborate with others, and maintain project history.
Q2: How do I create a new branch in Git?
A2: To create a new branch, navigate to the repository directory and run git checkout -b .
Q3: What is the difference between staging and committing in Git?
A3: Staging prepares changes for commit by adding them to the index. Committing saves a snapshot of the staged changes in the repository.
Q4: How do I resolve merge conflicts in Git?
A4: To resolve merge conflicts, manually edit the affected files and choose which changes to keep or merge appropriately. Then, use git add to stage the resolved file(s) and git commit to save the changes.
Practice Questions
- You've made some changes to your project files and want to commit them. What command would you use?
git add . && git commit -m "Your message"
- You need to create a new branch for a feature called "new-feature". How would you do that?
git checkout -b new-feature
- Suppose you've made some changes in the
feature/new-featurebranch and want to merge those changes into the master branch. What command would you use?
git checkout master && git merge feature/new-feature
- You accidentally committed a large binary file that is causing issues with Git's performance. How can you handle this using Git LFS?
- Install Git LFS, add the binary files to the .gitignore file, and then use Git LFS commands to manage these files efficiently.
- What command would you use to view the differences between two commits or branches in Git?
git diff ..for comparing commits orgit diff master..feature/new-featurefor comparing branches.