- vừa được xem lúc

Chapter 6: Advanced Git Techniques - Stashing, Rebasing, and Cherry-picking

0 0 18

Người đăng: Duc Nguyen

Theo Viblo Asia

This chapter belong to Getting Started with Git: A Beginner's Guide Series

In software development, it's common to have multiple branches of code that need to be merged into a main branch. Advanced Git techniques, such as stashing, rebasing, and cherry-picking, help to make these merges smoother and more efficient.

Stashing

Stashing allows developers to temporarily save changes in a branch that are not ready to be committed to the main branch. This can be useful when switching between branches or when pulling in changes from the main branch. To stash changes, use the command:

git stash

To get the stashed changes stack in Git, you can use the git stash list command. This command will show you a list of all the stashes you have stored in your repository. Each stash is identified by a stash ID, which is a hash of the changes you stashed away. To get more detailed information about a particular stash, you can use the "git stash show" command followed by the stash ID. This command will display the differences between the stash and your current branch.

To apply the changes from a stash, you can use the git stash apply command followed by the stash ID. This command will restore the changes to your current branch, but it will not remove the stash from your stash stack. If you want to remove the stash after you apply it, you can use the git stash drop command followed by the stash ID.

If you have multiple stashes, you can apply a specific stash by specifying its stash ID after the "git stash apply" command. For example, if you have 3 stashes and you want to apply the second stash, you can use the following command:

$ git stash apply stash@{1}

You can also use the "git stash pop" command to apply the latest stash and remove it from the stash stack in one command. For example:

$ git stash pop

Rebasing

Rebasing is the process of taking a branch and reapplying it on top of another branch. This can help to make the branch more current and make merging easier. To rebase a branch, use the command:

$ git rebase <branch>

The difference between merge and rebase

Merging involves creating a new merge commit that brings the changes from one branch into another. This results in a linear history, with all the changes being recorded in a single line. Merging is simple and straightforward, and is often used when working with multiple people on the same branch.

Rebasing, on the other hand, involves taking the changes from one branch and reapplying them on top of another branch. This results in a linear history, with the changes appearing as if they were made directly on the other branch. Rebasing is often used when working on a feature branch and you want to keep your code up-to-date with the latest changes in the main branch.

It's important to understand the differences between merging and rebasing, and to choose the appropriate method for your project and workflow. If you're working with multiple people on the same branch, it's generally recommended to use merging. However, if you're working on a feature branch and want to keep your code up-to-date, rebasing is often a better choice.

Cherry-picking

Cherry-picking is the process of taking a specific commit from one branch and applying it to another branch. The cherry-pick command allows you to apply a specific commit from one branch to another branch, without merging the entire branch. To cherry- pick a commit, use the command:

git cherry-pick <commit-hash>

These advanced Git techniques can help make your workflow more efficient and make it easier to collaborate with others on a project. However, it's important to understand the implications of each technique and use them carefully.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Đặt tên commit message sao cho "tình nghĩa anh em chắc chắn bền lâu"????

. Lời mở đầu. .

1 1 689

- vừa được xem lúc

Tập hợp những câu lệnh GIT hữu dụng

Dưới đây là một vài ví dụ về các câu lệnh Git mà tôi thường dùng. git config --global user.name "John Doe". git config --global user.

0 0 54

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 2): Trigger một work flow

Events trigger. Bạn có thể cấu hình cho workflows chạy khi có một sự kiện nào đó xảy ra trên GitHub, theo một lịch có sẵn hoặc cũng có thể là một sự kiện nào đó xảy ra ngoài GitHub.

0 0 69

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 1): Một ít lý thuyết

CI/CD là gì. Về mặt khái niệm là vậy nhưng về mặt triển khai thì CI/CD là quá trình tự động thực hiện các quá trình build, test, release, deploy khi có các trigger như commit/merge code lên một branch định sẵn hoặc có thể là tự động chạy theo một lịch cố định.

0 0 116

- vừa được xem lúc

Giới thiệu về Git LFS

. Git LFS là gì . Git LFS làm điều này bằng cách thay thế các tệp lớn trong repo của bạn bằng một con trỏ nhỏ.

0 0 28

- vừa được xem lúc

Git workflow được Google và Facebook sử dụng có gì hay ho

Với developer thì Git hẳn là công cụ rất quen thuộc và không thể thiếu rồi. Thế nhưng có mấy ai thực sự hiểu được Git.

0 0 65