Git is a powerful tool used to track changes in code and collaborate on software projects. It helps developers save versions of their work and makes it easy to share and merge code with others. Git is open-source and widely used in programming, especially in teamwork and open-source projects.
Why Use Git?
-
Version Control:
Keeps a history of your code changes, so you can go back to earlier versions if needed. -
Collaboration:
Multiple people can work on the same project without overwriting each other’s changes. -
Backup:
Your code is safe, even if something happens to your computer, as it can be stored in remote repositories like GitHub. -
Branching:
Create separate branches to test new features without affecting the main code.
How Git Works
-
Repository (Repo):
A folder where Git tracks your code. -
Stages:
- Working Directory: Where you write and edit code.
- Staging Area: Where you prepare changes before saving them.
- Commit: A snapshot of your changes saved to the repo.
-
Remote Repository:
A version of your project stored online, like on GitHub, GitLab, or Bitbucket, for sharing and backup.
Basic Git Commands
-
Setup Git:
git config --global user.name "Your Name" git config --global user.email "you@example.com" -
Initialize a Repo:
git initTurns a folder into a Git-tracked repository.
-
Check Status:
git statusShows changes and the current state of the repo.
-
Add Changes to Staging:
git add file.txtAdds specific changes, or use
git add .to add all changes. -
Commit Changes:
git commit -m "Describe your changes"Saves the changes in the repo with a message.
-
View History:
git logShows all past commits.
-
Connect to a Remote Repo (GitHub):
git remote add origin https://github.com/your-username/repo-name.git -
Push Changes to Remote Repo:
git push origin mainSends your changes online.
-
Pull Changes:
git pull origin mainUpdates your local repo with changes from the remote repo.
-
Create and Switch to a Branch:
git branch new-feature git checkout new-feature
How Git Fits Into Daily Life
-
Solo Development:
Keep track of your projects and revert mistakes easily. -
Teamwork:
Collaborate with others on the same project without conflicts. -
Open Source:
Contribute to public projects by forking repos, making changes, and submitting pull requests. -
Backup:
Save your code online to ensure you never lose it.
Popular Platforms for Git
- GitHub: The most popular platform for hosting Git repositories.
- GitLab: Offers similar features with better CI/CD tools.
- Bitbucket: Often used with teams that use Jira.