100===Dev Ops/Git

Git Introduced

블로글러 2024. 5. 26. 10:57

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

  1. Repository (Repo): The project’s storage. It contains all the project files and the history of changes.
  2. 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.
  3. 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.
  4. Merge: Combining changes from different branches. It's like taking your friends' separate canvases and combining their best parts into one.
  5. Clone: Making a copy of a repository. It's like each friend getting their own copy of the initial canvas to work on.
  6. 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

  1. 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"
  2. 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
  3. 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"
  4. 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 or master).
      git checkout main
      git merge new-branch
  5. 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

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

  1. What is a commit in Git?
  2. How do you create a new branch and switch to it?
  3. What is the purpose of the git merge command?
  4. 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

728x90