Mastering Git Basics

  • 2024/12/14
  • Mastering Git Basics はコメントを受け付けていません

Mastering Git Basics

By now, you’re probably heard about Git. Whether you’re a solo developer or part of a massive dev team, Git is at the heart of modern version control. Let’s go beyond the basics and focus on some practical concepts and commands you’ll use every day.


Understanding Branches

Branches are Git’s way of letting you work on different versions of your project simultaneously. Think of main (or master) as the primary branch where production-ready code lives.

  • A local branch exists on your machine.
  • An origin branch is your Git client’s reference to the branch on the remote repository (often on origin).
  • The remote branch exists on the Git server (e.g., GitHub, GitLab).

For example, after cloning a repository, you’ll have local branches and references to the remote ones:

git branch          # Lists local branches
git branch -r       # Lists remote branches
git branch -a       # Lists all (local + remote) branches

Example Output:

* main
  feature/login
  remotes/origin/main
  remotes/origin/feature/login

Fetching Remote Branches

When you run git fetch, you’re updating the references to the remote repository’s state without touching your local branches. This is where the origin branches come in.

git fetch

This updates the origin/main reference to match the state of main on the remote server. Your main branch remains untouched until you explicitly merge or rebase changes.


Avoid Confusion: Local vs. Remote Branch References

One common mistake is merging a local branch that hasn’t been updated with its remote counterpart. For example, running:

git merge main

When you mean to merge the latest changes from origin/main, this merges your local main branch, not the remote one. Instead, always ensure you’re merging the origin branch:

git merge origin/main

This way, you’re incorporating the latest changes from the actual remote branch rather than relying on outdated local data.


Pulling Updates: What’s Happening?

When you use git pull, you’re effectively doing this in one step:

  1. Fetching changes from the remote repository.
  2. Merging those changes into your current branch.
git pull origin main

This command ensures your local branch is in sync with the latest changes on origin/main.


Merging Changes from Another Branch Without Switching

You don’t always have to switch branches to bring in someone else’s changes. For example, if you’re on main and need updates from feature/login, you can:

git merge origin/feature/login

This fetches the changes from feature/login (if you’ve run git fetch) and merges them into your current branch (main).


Tip: Use git switch for Simpler Workflow

The git switch command simplifies branch management. It’s a modern alternative to git checkout for working with branches.

    • Switch to an existing branch:
git switch feature/login
    • Create and switch to a new branch:
git switch -c feature/signup

This makes your workflow cleaner and avoids the overloaded syntax of checkout.


Git Merge vs. Rebase: Quick Demo

Merging combines two branches by creating a merge commit.

git merge feature/login

Rebasing replays your branch’s commits on top of another branch for a linear history.

git rebase main

Merge vs. Rebase Visualization

Before merge:

main:  A---B---C
feature:       D---E

After git merge:

main:  A---B---C---M
feature:       D---E

After git rebase:

main:  A---B---C---D---E

Handy Tips for Everyday Use

  • Stash Changes: Need a clean working directory but don’t want to lose progress? Use stash.
    git stash
    git stash apply
    

    This is perfect for when you need to switch branches but aren’t quite ready to commit.

  • Undo the Last Commit (Without Losing Changes):
    git reset --soft HEAD~1
    

    This keeps your changes in the staging area while removing the last commit. Great for quick fixes.

  • See Who Changed What Line:
    git blame filename
    

    Use this to understand who made changes to specific lines in a file. It’s handy for debugging or tracking history.

  • Clean Up Old Branches:
    git branch -d feature/old-branch
    git push origin --delete feature/old-branch
    

    Useful for cleaning up unused branches — keeps your repo organized.

  • Interactive Rebase for Clean History:
    git rebase -i HEAD~3
    

    Use this to edit, squash, or reorder commits in your branch history for a cleaner and more meaningful commit log.

Interactive rebasing is particularly useful before merging a branch to ensure your commits tell a clear story.


Wrapping Up

Git is more than just a version control system; it’s a tool for collaboration in complex projects. By understanding the difference between local and origin branches, mastering fetch vs. pull, and leveraging tools like merge and rebase, you can work more effectively and avoid common pitfalls.

関連記事

カテゴリー:

ブログ

情シス求人

  1. チームメンバーで作字やってみた#1

ページ上部へ戻る