Advanced Tools: Rebase, Stash, Cherry-pick and Tags
Moving on to the tools that distinguish a confident Git user from someone who only knows the basics. We'll explore rebase as an alternative to merge - showing when it makes history cleaner and when it turns it into a nightmare. We'll add stash for quickly 'shelving' unfinished work, cherry-pick for precisely transferring commits, and tags for marking releases.
- You'll be able to use git stash for context switching between tasks without losing changes.
- You'll understand the difference between rebase and merge and will be able to make an informed choice between them depending on the context.
- You'll learn to edit commit history using interactive rebase and mark release versions with tags.
Phần đầu bài giảng
The Problem: How to Shelve Unfinished Work
- A sudden bugfix interrupts your work
- You can't commit incomplete code
- Git stash — temporary storage for changes
- Tracks both staged and unstaged files
- Returns the working directory to a clean state
Imagine: you're in the middle of working on a new feature when a critical bugfix comes in. You don't want to leave your work half-finished, but you can't commit incomplete code either. This is exactly what git stash is for — your digital temporary shelf. It's like pausing a game: your current progress is saved, and you can attend to other matters. When you return, everything will be exactly as you left it, including untracked files if you didn't forget the -u flag. This saves you from having to keep multiple tasks in mind at once.
Saving and Viewing Changes
- git stash save — saves changes to the stash
- git stash list — shows the list of saved changes
- Each stash is assigned a unique ID
- stash@{0} — most recently saved change
- The stash preserves the working directory state
Let's explore how to work with git stash in practice. The command git stash save will save all changes in the working directory and staged files, returning you to a clean state. Your work won't be lost — it will go into a special storage area. To verify that your changes have been saved, run git stash list. You'll see a list of all stashed tasks with unique identifiers like stash at zero. This is particularly useful when you need to quickly switch between tasks without losing progress.
# Save current changes
git stash save "WIP: New feature login form"
# Check the list of saved changes
git stash list
# View the contents of the latest stash
git stash show -p
Recovering Stashed Changes
- git stash pop — applies and removes the most recent stash
- git stash apply — applies without removing
- Code conflicts may occur
- Conflicts must be resolved manually
- stash@{n} — refers to a specific stash
When you're ready to return to your stashed work, you have two options. The git stash pop command will apply the most recent stashed changes and remove them from the stash list. If you want to review the changes or apply them multiple times, use git stash apply — the changes will remain in the stash. Important: if the code has changed since you created the stash, conflicts may occur. In this case, Git will alert you to the problem, and you'll need to resolve the conflicts manually.
# Restore the most recent stashed changes
git stash pop
# OR apply without removing
git stash apply
# Apply a specific stash by index
git stash apply stash@{1}
Bài giảng đầy đủ dành cho thuê bao
Còn 25 slide nữa trong phiên bản đầy đủ.
Video, toàn bộ slide, ghi chú, bài kiểm tra, gia sư AI và phỏng vấn sẽ mở sau khi đăng nhập và đăng ký thuê bao.
Đăng nhập hoặc đăng ký Đăng ký thuê baoNội dung bài giảng
- The Problem: How to Shelve Unfinished Work
- Saving and Viewing Changes
- Recovering Stashed Changes
- When Stash Can Be Harmful
- How Branch Merging Works
- Rebase: Rewriting Commit History
- When to Use What: Practical Scenarios
- The Dangers of Rebase: What Could Go Wrong
- What is Rebase and Why Do We Need It
- Performing Rebase: Step-by-Step Guide
- Resolving Rebase Conflicts
- Interactive Rebase: Your Commit History Editor
- Key Commands in Interactive Rebase
- Practice: Creating the Perfect History
- Warning: Changing Published History
- Why You Shouldn't Rebase Shared Branches
- Recovering from a Failed Rebase
- Safe History Modification
- Why Use Cherry-Pick in Git
- How to Use git cherry-pick
- When to Choose: cherry-pick vs merge vs rebase
- Git Tags: Bookmarks for Important Commits
- Creating Annotated and Lightweight Tags
- Publishing Tags to a Remote Repository
- Scenario: Hotfix in the Middle of Development
- Integrating Changes Using Rebase and Cherry-Pick
- Common Pitfalls and How to Avoid Them
- Summary: Your New Git Mastery Toolkit
Bài viết liên quan đến bài học này
- git rebase: cách hoạt động, chế độ tương tác và gộp commit
- Lệnh Git: bảng tra cứu với ví dụ dùng hằng ngày
- Git merge và rebase: nên chọn cái nào cho nhóm
- git stash: cách cất tạm thay đổi và lấy lại
- Câu hỏi về Git khi phỏng vấn: 25 câu hỏi và đáp án
Tất cả bài giảng của khóa học
- Introduction: Git from Scratch to Confident Developer
- Why Git is Essential - And Why It's a Job Requirement
- Installation and Setup: Getting Git Ready for First Use
- First Repository: init, add, commit, and the Magic of the Index
- Reading History: log, diff, show and Navigating the Past
- Branches: The Parallel Reality of Your Code
- Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
- GitHub and Remote Repositories: Your Code in the Cloud
- Team Collaboration: Forks, Feature Branches, and Pull Requests
- Merge Conflicts: Don't Panic - Understanding and Resolving Them
- Git Workflows in Real Teams: Choosing the Right Approach
- Advanced Tools: Rebase, Stash, Cherry-pick and Tags
- Final Project: Simulating Real Team Development