Table of contents
No headings in the article.
Git is a distributed version control system (DVCS) that is widely used in software development to track changes in source code and collaborate on projects. It was created by Linus Torvalds in 2005 and has since become the de facto standard for version control in the software development industry. Git is known for its speed, flexibility, and robust branching and merging capabilities
Step 1: Install Git
Start by ensuring your system is up to date and then install Git:
sudo apt update
sudo apt install git
Step 2: Configure Git
Set your name and email address. This information will be associated with your Git commits.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
You can also configure your default text editor (if necessary):
git config --global core.editor "your_editor_of_choice"
Step 3: Create Your First Repository
Navigate to the directory where you want to create your Git repository, and initialize it:
cd /path/to/your/project
git init
Add your files to the repository:
git add filename # To add a specific file
or
git add . # To add all files in the directory
Commit your changes with a meaningful message:
git commit -m "Initial commit"
Step 4: Connect to a Remote Repository
If you don't have a Git hosting service account (e.g., GitHub, GitLab, Bitbucket), create one.
Generate an SSH key for secure authentication. Replace "your_email@example.com" with your email:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This generates a public and private key pair.
Step 5: Add Your SSH Key to the Hosting Service
Copy your public key to your clipboard:
cat ~/.ssh/id_rsa.pub
Then, go to your Git hosting service (e.g., GitHub) and add the public key in your account settings.
Step 6: Test Your SSH Connection
Test the SSH connection to your Git hosting service. Replace "git@github.com" with your hosting provider.
ssh -T git@github.com
Step 7: Clone and Pull Repositories
To clone a remote repository to your local machine:
git clone <repository_url>
To fetch the latest changes from the remote repository:
git pull
Step 8: Push Your Changes
Push your local changes to the remote repository. Replace "origin" and "master" with your remote and branch names.
git push origin master
Step 9: Branching and Merging
Create a new branch:
git checkout -b new-branch-name
Switch between branches:
git checkout branch-name
Merge branches:
git merge branch-name
Step 10: Resolving Conflicts
When conflicts arise, use git status
to identify them.
Edit the conflicted files to resolve the issues.
Commit the resolved changes and continue the merge:
git add conflicted-file
git commit -m "Resolved conflicts"
git merge --continue
Step 11: Tagging Releases
Create a tag to mark a specific point in your project's history:
git tag -a v1.0 -m "Version 1.0"
Push tags to the remote repository:
git push origin --tags