Introduction
Git is a distributed version control system widely used to manage software development projects. This guide will introduce you to the fundamental commands of Git, helping you get started with version control.
Prerequisites
- A system with Git installed.
- Basic command-line knowledge.
Step 1: Install Git
On Ubuntu/Debian:
sudo apt update sudo apt install git -y
Step 2: Configure Git
Set your username and email (required for commits):
git config --global user.name "Your Name" git config --global user.email "[email protected]"
Step 3: Create a New Repository
- Create a new directory for your project:
mkdir my_project cd my_project
- Initialize Git:
git init
Step 4: Add Files to the Repository
- Create a file:
echo "# Project Title" > README.md
- Add the file to Git tracking:
git add README.md
Step 5: Make a Commit
- Record the changes in the repository:
git commit -m "Add README.md file"
Step 6: Clone an Existing Repository
To clone a remote repository:
git clone https://github.com/user/repository.git
Step 7: View the Repository Status
Check which files have been modified or are waiting to be committed:
git status
Step 8: View Commit History
Display the commit history:
git log
Step 9: Work with Branches
- Create a new branch:
git branch new_feature
- Switch to the new branch:
git checkout new_feature
Step 10: Merge Branches
- Return to the main branch:
git checkout main
- Merge the changes:
git merge new_feature
Step 11: Manage a Remote Repository
- Add a remote repository:
git remote add origin https://github.com/user/repository.git
- Push changes to the remote:
git push -u origin main
Step 12: Update the Local Repository
To get the latest changes from the remote repository:
git pull
Conclusion
Now you know the fundamental commands to use Git in version control for your projects. Continue exploring advanced features to improve your workflow.