Edited 3 weeks ago by ExtremeHow Editorial Team
Version ControlGitDevelopmentCommand LineCollaborationSource Code ManagementProjectsSoftware DevelopmentOpen SourceUtilities
This content is available in 7 different language
Git is a popular version control system used to track changes to source code during software development. It is used by millions of developers and companies worldwide. If you are developing software on Linux, it is important to understand how to use Git to manage your code effectively. In this guide, we will take a deep dive into using Git on Linux.
Git is a distributed version control system, which means that the entire codebase and its history is available on each developer's computer. This allows for seamless collaboration between multiple developers working on the same project.
Linus Torvalds, the creator of the Linux kernel, developed Git in 2005. Since then, it has become an essential tool in the software development arsenal due to its robustness, ease of splitting and merging branches, and amazing performance.
Before you can use Git, you need to make sure it is installed on your Linux machine. You can install Git using your Linux distribution's package manager.
sudo apt update
sudo apt install git
Confirm the installation by running the following:
git --version
This should revert the installed Git version.
For CentOS, use the following command:
sudo yum update
sudo yum install git
Again, confirm the installation:
git --version
Before using Git, you should configure your username and email address, as these will be associated with your commits.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
These settings are stored in the .gitconfig
file in your home directory.
To view all your Git configurations, use the command:
git config --list
You should see your username and email address in the output.
To start working with Git, you need to create a new Git repository. You can do this by running the following command in your project's directory:
git init
This command creates a hidden directory called .git
, which contains all the necessary repository files.
Create a new file using the text editor of your choice. Save the file in the project directory, then add it to the Git staging area using the following:
git add filename
Replace filename
with the name of your file.
Once the file is added to the staging area, you need to commit it. This is done like this:
git commit -m "Add initial file"
The -m
flag allows you to write a commit message that describes the changes.
Branches in Git are a powerful way to isolate work in progress. Let's learn how to create and manage branches.
Use the following command to create a new branch:
git branch branchname
Replace branchname
with the descriptive name of your branch.
Use the following to switch to your new branch:
git checkout branchname
checkout
command changes the working directory to point to the desired branch.
Once you are satisfied with the changes made to your branch, you can merge them into another branch, usually the main branch. Switch to the branch you want to merge into and then run:
git merge branchname
This command merges the specified branch into the current branch.
Remote repositories allow you to share your code with others and contribute to others' projects. Let's see how to work with remote repositories.
To add a remote repository, use the command:
git remote add origin https://github.com/yourusername/repository.git
Replace the URL with the URL of your repository. The word origin
is an alias for the remote URL.
After committing your changes, push them to the remote repository using the following:
git push origin branchname
This command commits your changes to the remote repository.
If you want to work on an existing remote repository, you can clone it using the following command:
git clone https://github.com/username/repo.git
This creates a new directory on your local machine containing all the files and history of the remote repository.
To keep your local repository updated with changes in the remote repository, use:
git pull origin branchname
This fetches the changes from the remote branch and merges them into your current branch.
Conflicts occur when changes in branches are not compatible with each other. You must resolve these conflicts before you can merge the branches.
Conflicts usually arise during a merge, and Git will notify you of any conflicts. Conflicting files will include conflict markers to indicate where changes conflict.
To resolve a conflict, open the conflicting files in a text editor. Look for lines containing <<<<<<<, =======, and >>>>>>>>that indicate conflicting changes. Make the necessary changes to resolve these conflicts.
After you resolve the conflicts, add the resolved files to the staging area, then commit your changes:
git add resolved_file
git commit -m "Resolve merge conflicts"
Following best practices ensures that Git will serve you well in managing your projects.
Always use clear and descriptive commit messages that summarize the changes you made.
Make changes as often as possible. Implement small, logical changes regularly for better project management.
Adopt a consistent branch naming convention to help easily identify branches.
Regularly pull changes from the remote repository to keep your local branch up to date.
Before merging branches, perform code reviews to catch potential problems and ensure code quality.
Version control is a fundamental skill for developers. Git on Linux provides an efficient way to manage and collaborate on code. With the knowledge and skills provided in this guide, you will be well-equipped to handle Git on Linux effectively. Get into the habit of using Git regularly, follow best practices, and gradually uncover more advanced topics like rebasing, cherry-picking, and bisecting to become proficient in Git.
If you find anything wrong with the article content, you can