Helpful Git Operations

Git Cherry-Pick: Using Specific Commits

Cherry-picking lets you apply one commit from one branch to another. This is useful when you want to use a particular change without merging whole branches. It’s like picking a single cherry from a tree instead of taking the whole branch. (Guess what, it’s not a git specific term either!)

To cherry-pick a commit:

git cherry-pick

For example, to use commit abc123 on your current branch:

git cherry-pick abc123

This makes a new commit on your branch with the changes from abc123. Be ready to fix conflicts if the changes clash with existing work.

Git Squash: Joining Commits

Squashing joins multiple commits into one. This keeps your commit history tidy and makes it easier to understand the overall changes. It’s often used before merging a feature branch to main, to present a cleaner history.

To squash the last 3 commits:

git rebase -i HEAD~3

This opens a text editor where you can mark commits to squash:

pick f7f3f6d First commit message
squash 310154e Second commit message
squash a5f4a0d Third commit message

Change “pick” to “squash” (or “s”) for the commits you want to join. Save and close the file, then edit the new commit message when asked.

Git Bisect: Finding Problem Commits

Bisect helps find the commit that caused a bug. It uses a binary search method, which is much faster than checking each commit one by one. This is especially useful in large projects with many commits between known good and bad states.

Start a bisect session:

git bisect start
git bisect bad  # Current version is bad
git bisect good v1.0  # v1.0 is known to be good

Git will check out a commit in the middle. Test your code and tell Git the result:

git bisect good  # or git bisect bad

Keep doing this until Git finds the first bad commit. End the session with:

git bisect reset

Git Stash: Saving Changes Temporarily

Stash saves your unsaved changes and gives you a clean working directory. It’s helpful when you need to switch tasks quickly but aren’t ready to commit your current work. You can later reapply these changes when you’re ready to continue working on them.

To stash changes:

git stash save "Work in progress on feature X"

To use the most recent stash:

git stash pop

You can also use a stash without removing it from the list:

git stash apply

Git Reflog: Recovering Lost Work

Reflog keeps a record of all your recent Git actions. It can help you recover lost commits or branches, even if you’ve deleted them. This acts as a safety net, allowing you to undo mistakes that might otherwise seem permanent.

View the reflog:

git reflog

To bring back a lost commit or branch, find its hash in the reflog and check it out:

git checkout -b recovered-branch abc123

Remember, reflog is only on your computer and isn’t shared with others.

Git Rebase: Cleaning Up Branch History

Rebasing is a way to add changes from one branch to another, making a cleaner project history. It’s like replaying your changes on top of the latest version of the main branch. This can make the history easier to follow, but it changes commit hashes.

To rebase your feature branch onto the latest main:

git checkout feature-branch
git rebase main

This moves your feature branch commits to the end of main, one by one. It’s like you started your feature branch from the current main.

Interactive rebasing gives you more control:

git rebase -i main

This lets you reorder commits, join them, or skip some. It’s great for tidying up your branch before merging.

Be careful when rebasing branches that others are using, as it changes commit history.

Git Worktree: Using Multiple Working Directories

Git worktree lets you check out multiple branches of the same repository into different folders. This is great for working on different features at the same time or running long tests while still coding. It’s like having multiple copies of your project, but without duplicating the entire repository.

To make a new worktree:

git worktree add ../new-directory branch-name

This checks out the branch into a new folder. You can now work on this branch separately, without affecting your main work.

To see your worktrees:

git worktree list

To remove a worktree:

git worktree remove path-to-worktree

Worktrees share the same Git folder, so they don’t take up much extra space.

These Git commands can help you work more efficiently. With practice, you’ll find them very useful for managing your code.

関連記事

カテゴリー:

ブログ

情シス求人

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

ページ上部へ戻る