Git is a version control system that helps track changes to files, enabling collaboration and maintaining a history of changes in a project, similar to a time machine for your code.
The Big Picture
Imagine you're working on a big art project with friends. Everyone adds their own touches to a shared canvas. Without a system to track who did what, and when, it's easy to make mistakes or lose important changes. Git is like a digital logbook that records every change made to the canvas, so you can always go back to previous versions and see who contributed what.
Core Concepts
- Repository (Repo): The project’s storage. It contains all the project files and the history of changes.
- Commit: A snapshot of your repository at a specific point in time. It’s like taking a photo of your canvas after every significant change.
- Branch: A separate line of development. Imagine each friend working on their own version of the canvas. Branches allow you to try new ideas without affecting the main project.
- Merge: Combining changes from different branches. It's like taking your friends' separate canvases and combining their best parts into one.
- Clone: Making a copy of a repository. It's like each friend getting their own copy of the initial canvas to work on.
- Push/Pull: Syncing changes with a remote repository. Push is sending your changes to the main canvas, and pull is getting the latest changes from the main canvas.
Detailed Walkthrough
Setting Up Git:
- Install Git: Download and install Git from git-scm.com.
- Configuration: Set up your name and email:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
Creating a Repository:
- Initialize a Repository: Start a new repository in an existing directory.
git init
- Clone a Repository: Make a copy of an existing repository.
git clone https://github.com/username/repo.git
- Initialize a Repository: Start a new repository in an existing directory.
Making Changes:
- Stage Changes: Add changes to the staging area, preparing them for a commit.
git add filename
- Commit Changes: Save your changes in the repository’s history.
git commit -m "Describe your changes"
- Stage Changes: Add changes to the staging area, preparing them for a commit.
Branching and Merging:
- Create a Branch: Start a new branch for your work.
git branch new-branch git checkout new-branch
- Merge a Branch: Combine your branch with the main branch (usually
main
ormaster
).git checkout main git merge new-branch
- Create a Branch: Start a new branch for your work.
Collaborating:
- Push Changes: Send your commits to a remote repository.
git push origin branch-name
- Pull Changes: Fetch and merge changes from the remote repository.
git pull origin main
- Push Changes: Send your commits to a remote repository.
Understanding Through an Example
Let's say you're working on a website with a friend. You start by creating a repository for the project:
git init my-website
cd my-website
You create a simple HTML file and stage it:
echo "<h1>Hello World</h1>" > index.html
git add index.html
You commit your change:
git commit -m "Add homepage"
Your friend wants to add a contact page, so they create a new branch:
git branch contact-page
git checkout contact-page
They add their changes and commit:
echo "<h1>Contact Us</h1>" > contact.html
git add contact.html
git commit -m "Add contact page"
After finishing, they merge their changes back into the main branch:
git checkout main
git merge contact-page
Finally, they push the changes to a remote repository for everyone to see:
git push origin main
Conclusion and Summary
Git helps manage and track changes in your project. It allows multiple people to work together without overwriting each other’s work. By understanding key concepts like repositories, commits, branches, and merging, you can effectively use Git to maintain and collaborate on projects.
Test Your Understanding
- What is a commit in Git?
- How do you create a new branch and switch to it?
- What is the purpose of the
git merge
command? - How do you sync your local repository with a remote one?
Feel free to ask more about any of these concepts if you need further clarification!
Reference
'100===Dev Ops > Git' 카테고리의 다른 글
Git 제대로 이해하기: 시간여행자의 코드 관리 비법 🚀 (2) | 2024.11.17 |
---|---|
Gitlab Introduced (0) | 2024.06.11 |
How do you sync your local repository with a remote one with git? (0) | 2024.06.09 |