debian-mirror-gitlab/doc/topics/git/cherry_picking.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
2.5 KiB
Markdown
Raw Normal View History

2021-04-17 20:07:23 +05:30
---
stage: Create
group: Source Code
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-04-17 20:07:23 +05:30
---
2021-11-11 11:23:49 +05:30
# Cherry-pick a Git commit **(FREE)**
2021-04-17 20:07:23 +05:30
2021-11-11 11:23:49 +05:30
In Git, you can *cherry-pick* a commit (a set of changes) from an existing branch,
and apply those changes to another branch. Cherry-picks can help you:
2021-04-17 20:07:23 +05:30
2021-11-11 11:23:49 +05:30
- Backport bug fixes from the default branch to previous release branches.
- Copy changes from a fork
[to the upstream repository](../../user/project/merge_requests/cherry_pick_changes.md#cherry-pick-into-a-project).
2021-04-17 20:07:23 +05:30
2021-11-11 11:23:49 +05:30
You can cherry-pick commits from the command line. In the GitLab user interface,
you can also:
2021-04-17 20:07:23 +05:30
2022-10-11 01:57:18 +05:30
- Cherry-pick [all changes from a merge request](../../user/project/merge_requests/cherry_pick_changes.md#cherry-pick-all-changes-from-a-merge-request).
- Cherry-pick [a single commit](../../user/project/merge_requests/cherry_pick_changes.md#cherry-pick-a-single-commit).
2021-11-11 11:23:49 +05:30
- Cherry-pick [from a fork to the upstream repository](../../user/project/merge_requests/cherry_pick_changes.md#cherry-pick-into-a-project).
## Cherry-pick from the command line
These instructions explain how to cherry-pick a commit from the default branch (`main`)
into a different branch (`stable`):
1. Check out the default branch, then check out a new `stable` branch based on it:
2021-04-17 20:07:23 +05:30
```shell
2021-11-11 11:23:49 +05:30
git checkout main
2021-04-17 20:07:23 +05:30
git checkout -b stable
```
1. Change back to the default branch:
```shell
2021-11-11 11:23:49 +05:30
git checkout main
2021-04-17 20:07:23 +05:30
```
2021-11-11 11:23:49 +05:30
1. Make your changes, then commit them:
2021-04-17 20:07:23 +05:30
```shell
git add changed_file.rb
git commit -m 'Fix bugs in changed_file.rb'
```
2021-11-11 11:23:49 +05:30
1. Display the commit log:
2021-04-17 20:07:23 +05:30
```shell
2021-11-11 11:23:49 +05:30
$ git log
commit 0000011111222223333344444555556666677777
Merge: 88888999999 aaaaabbbbbb
Author: user@example.com
Date: Tue Aug 31 21:19:41 2021 +0000
2021-04-17 20:07:23 +05:30
```
2021-11-11 11:23:49 +05:30
1. Identify the `commit` line, and copy the string of letters and numbers on that line.
This information is the SHA (Secure Hash Algorithm) of the commit. The SHA is
a unique identifier for this commit, and you need it in a future step.
1. Now that you know the SHA, check out the `stable` branch again:
2021-04-17 20:07:23 +05:30
```shell
git checkout stable
```
2021-11-11 11:23:49 +05:30
1. Cherry-pick the commit into the `stable` branch, and change `SHA` to your commit
SHA:
2021-04-17 20:07:23 +05:30
```shell
2021-11-11 11:23:49 +05:30
git cherry-pick <SHA>
2021-04-17 20:07:23 +05:30
```
2021-11-11 11:23:49 +05:30
2022-01-26 12:08:38 +05:30
## Related topics
2021-11-11 11:23:49 +05:30
- Cherry-pick commits with [the Commits API](../../api/commits.md#cherry-pick-a-commit)
- Git documentation [for cherry-picks](https://git-scm.com/docs/git-cherry-pick)