728x90

If you've been using Git and running git add and git commit frequently, you might wonder:
"How can I check all the versions I've created?"

The answer is simple: Use git log!

 

 

1️⃣ What is git log?

The git log command allows you to see all the commit history in your repository.
Simply type the following command:

 

git log

 

This will display details about each commit, including:

Commit hash → A unique ID for each commit
HEAD info → Shows which branch you’re currently on
Author information → Useful for collaboration
Commit message → A short description of the changes

📌 Why is the commit hash important?
Each commit has a long hash (e.g., a1b2c3d4...).This is useful when you need to switch to a specific version later.


2️⃣ Making git log More Readable

By default, git log displays a lot of information, which can be overwhelming.
Here are two useful options to make it more readable.

🔹 1. --graph (Visualizing commit relationships)

 
git log --graph

This option draws a graph to show how commits are connected.
It becomes extremely useful when working with branches.

🔹 2. --oneline (Displaying commits in a single line)

git log --oneline

 

This command simplifies the output by showing only the commit hash (7 characters) and message.

🔹 3. --graph + --oneline (Best combination)

git log --graph --oneline

 

This is the most commonly used combination to keep logs simple and structured.


🎯 Summary

✔ git log helps you review your commit history.
✔ Use --graph and --oneline to make it easier to read.

Git logs are essential when you need to track changes, revert to previous versions, or collaborate with a team.

728x90
728x90

Understanding the Basics

Yesterday, we learned how git add and git commit work to update version information when changes occur. Today, let's explore how to revert to a previous version using Git.

1. Restoring a Previous Version

(1) Git Structure

In Git, when changes occur, they are added to the staging area with git add and then stored in the repository with git commit.
To revert changes, we use different commands depending on the situation:

  • git reset: Moves back to a previous commit.
  • git restore --staged <file>: Unstages a file from the staging area.
  • git restore <file>: Discards changes made in the working directory.

(2) git reset

The git reset command is used to revert back to a previous commit. Here’s how:
1️⃣ Check the commit hash of the version you want to return to

2️⃣ Reset to a specific commit (removes all changes after it)

3️⃣ Reset to the previous commit (soft reset, keeps changes unstaged)


(3) git restore --staged <file>

If you have staged a file but want to unstage it, use:
1️⃣ Check the Git status

2️⃣ Unstage the file

3️⃣ Verify the file has been unstaged


(4) git restore <file>

To discard changes made to a file in the working directory, use:
1️⃣ Check modified files

 

💡 You’ll see something like this:

2️⃣ Restore the file to its previous state

3️⃣ Confirm that the file is no longer modified


📌 Summary

  • Use git reset to move back to an older commit.
  • Use git restore --staged to unstage a file.
  • Use git restore to discard changes in the working directory.

Hope this guide helps! 🚀


🔖 References

  • "Git & GitHub Beginner's Guide" – Aegis Publishing
728x90
728x90

🔹 Git 브랜치 정리, 왜 필요할까?

Git을 사용하다 보면 브랜치가 많아지고, 커밋이 중복되거나 지저분해지는 경우가 많아요.
이럴 때 git rebase와 git squash를 활용하면 브랜치를 깔끔하게 정리할 수 있습니다 :)
오늘은 Git Rebase와 Squash를 사용해 브랜치를 정리하는 방법을 알아볼게요. 🚀


1️⃣ Git Rebase란?

🔹 Rebase vs Merge 차이

  • git merge는 브랜치를 합칠 때 새로운 커밋을 생성해요. (병합 이력이 남음)
  • git rebase는 브랜치를 다른 브랜치의 최신 상태에 맞춰 변경하는 방식이에요.

💡 Rebase를 사용하면 깔끔한 Git 히스토리를 유지할 수 있어요!

🔹 Git Rebase 기본 사용법

예를 들어, feature 브랜치를 main 브랜치에 맞춰 업데이트하려면?

✅ feature 브랜치의 커밋이 main 브랜치의 최신 상태를 기준으로 다시 정렬돼요!


2️⃣ Git Squash란?

🔹 여러 개의 커밋을 하나로 합치기

작업하다 보면 의미 없는 커밋이 많아질 수 있어요.
이럴 때 git squash를 사용하면 여러 개의 커밋을 하나로 합칠 수 있어요.

🔹 Git Squash 기본 사용법

1️⃣ 리베이스 모드로 진입 (최근 3개의 커밋을 합치려면)

2️⃣ pick → squash 변경

3️⃣ 새로운 커밋 메시지 작성 후 저장

💡 이제 3개의 커밋이 1개의 커밋으로 정리돼요! 🎉


3️⃣ Rebase & Squash를 활용한 브랜치 정리

🔹 원하는 브랜치로 최신화 & 커밋 정리하기

1️⃣ main 브랜치 최신 상태 반영

2️⃣ 의미 없는 커밋 정리 (최근 5개 커밋 합치기)

 

3️⃣ 충돌 발생 시 해결 후 진행

4️⃣ 리모트 브랜치 강제 푸시

💡 Rebase & Squash를 활용하면 브랜치가 깔끔해지고 협업이 쉬워져요!


📌 정리

✔️ git rebase → 브랜치를 최신 상태로 정렬
✔️ git squash → 여러 개의 커밋을 하나로 합치기
✔️ git rebase -i HEAD~N → 최근 N개의 커밋을 정리

이제 지저분한 Git 히스토리는 끝! 🎯
Rebase & Squash를 적극 활용해보세요! 😆

728x90
728x90

Background

Git is a system designed to efficiently manage source code. Its core functions include version control, backup, and collaboration. In this post, we will focus on the version control functionality of Git.

Managing versions with Git involves three key steps:

  1. Creating a repository.
  2. Generating new versions when changes occur.
  3. Reverting to previous states when necessary.

This post will cover repository creation and version management.


1. Creating a Git Repository

When starting a new project, we typically create a folder to store files. To manage the files within this folder using Git, we need to initialize a Git repository. Follow these steps:

mkdir project_test  # Create a new folder named 'project_test'
cd ./project_test   # Navigate to the 'project_test' directory

git init  # Initialize a Git repository in the 'project_test' folder

If successful, you will see the following message:

Initialized empty Git repository in C:/Users/project_test/.git/

At this point, the directory is ready for Git version control.


2. Managing Versions with Git

2.1 Understanding Git's Structure

Before creating versions, it's essential to understand Git's structure. Git consists of three areas:

  • Working Directory: The folder where files are edited and stored.
  • Staging Area: A temporary space where files are prepared before committing them to the repository.
  • Repository: The final storage area where committed versions are permanently saved.

Git uses the following commands to manage these areas:

 

  • git add moves files from the working directory to the staging area.
  • git commit moves files from the staging area to the repository.
  • git restore and git reset help revert changes if necessary.

2.2 Creating a New Version in Git

The process of creating a new version in Git involves checking the status, adding files to the staging area, and committing them to the repository.

Step 1: Check Git Status

git status  # Check the current status of the working directory

This command helps determine whether there are new or modified files that need to be staged.

Step 2: Create a New File and Add It to Staging

vi test.txt  # Create a new file named 'test.txt'
# Enter some content, then save and exit the editor

git status  # Check the status again

Expected output:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

Since this file is new, Git does not track it yet.

Next, add the file to the staging area:

git add test.txt  # Stage the new file
git status  # Check the status again

Expected output:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

Step 3: Commit the File to the Repository

git commit -m "Added test.txt"  # Commit the file with a message

After committing, check the log to confirm the new version:

git log  # View commit history

Expected output:

commit 2a90767a070d85bebdbaa5f54b72b8e5d7043b45 (HEAD -> master)
Author: -----
Date:   Sun Jun 26 14:01:04 2022 +0900

    Added test.txt

Conclusion

  • Git repositories allow efficient tracking of file changes.
  • Git version control involves adding changes to the staging area and committing them to the repository.
  • Key commands: git status, git add, git commit, and git log are essential for managing versions.

By mastering these basic Git operations, you can effectively manage your source code and collaborate with others.


References

  • "Git & GitHub Guide" by Easy Publishing

Thank you for reading! 🚀

728x90

+ Recent posts