<div class="page">
<div class="cover text-center">
<img class="mx-auto" src=/itb/images/logo_mislata.png alt="logo">
# Cherry Pick
<div class="text-end fit-content ms-auto my-3 mt-auto pt-3">
<p><strong>Autor:</strong> Joan Puigcerver Ibáñez</p>
<p><strong>Correu electrònic:</strong> j.puigcerveribanez@edu.gva.es</p>
<p><strong>Curs:</strong> 2024/2025</p>
</div>
<div>
<p class="fw-bold mb-0">Llicència: BY-NC-SA</p>
<p class="d-none d-md-block">(Reconeixement - No Comercial - Compartir Igual)</p>
<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.ca" target="_blank">
<img class="mx-auto" src="/itb/images/license.png" alt="Licence"/>
</a>
</div><!--license-->
</div><!--cover-->
</div><!--page-->
{:toc}
## Introducció
L'eina `cherry-pick` consisteix en "copiar" un _commit_ de la història del repositori
al nostre estat actual (`HEAD`). Per exemple, pot servir per incloure les modificacions realitzades
en un _commit_ d'una branca a una altra branca.
La sintaxi és:
```bash
git cherry-pick <hash>
```
::: docs
Documentació oficial sobre `cherry-pick`:
- https://git-scm.com/docs/git-cherry-pick
:::
```mermaid
---
title: Revert
---
%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%%
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A"
commit id:"THREE"
checkout develop
commit id:"C"
```
## Repositori d'exemple
::: tip
Podeu copiar i pegar les ordres següents a __Git Bash__
per inicialitzar un repositori amb l'estructura dels exemples.
:::
Tots els exemples es realitzaran a partir d'un repositori amb la següent estructura:
```bash
mkdir ~/git_cherry_pick
cd ~/git_cherry_pick
git init
echo "# Git cherry pick" >> README.md
git add README.md
git commit -m "Added README.md"
git branch -m main
git branch develop
echo "- Primer canvi" >> README.md
git commit -a -m "1. Primer canvi"
echo "- Segon canvi" >> README.md
git commit -a -m "2. Segon canvi"
git checkout develop
echo "- Canvi A" >> README.md
git commit -a -m "A. Canvi A"
echo "- Canvi B" >> README.md
git commit -a -m "B. Canvi B"
echo "- Canvi C" >> README.md
git commit -a -m "C. Canvi C"
```
```shell
jpuigcerver@FP:~/git_cherry_pick (main) $ git lga
* 8453c8c - (2 seconds ago) C. Canvi C - Joan Puigcerver (HEAD -> develop)
* c28155d - (2 seconds ago) B. Canvi B - Joan Puigcerver
* 7dbdc25 - (2 seconds ago) A. Canvi A - Joan Puigcerver
| * 6582d61 - (2 seconds ago) 2. Segon canvi - Joan Puigcerver (main)
| * 06ce5a6 - (3 seconds ago) 1. Primer canvi - Joan Puigcerver
|/
* 529847a - (3 seconds ago) Added README.md - Joan Puigcerver
jpuigcerver@FP:~/git_cherry_pick (main) $ git status
On branch main
nothing to commit, working tree clean
jpuigcerver@FP:~/git_cherry_pick (main) $ cat README.md
# Git reset
- Primer canvi
- Segon canvi
- Tercer canvi
```
## Exemple
A partir del repositori d'exemple:
```mermaid
---
title: Revert
---
%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%%
gitGraph
commit id: "ZERO"
branch develop
commit id:"A"
checkout main
commit id:"ONE"
checkout develop
commit id:"B"
checkout main
commit id:"TWO"
cherry-pick id:"A"
commit id:"THREE"
checkout develop
commit id:"C"
```
Incorporem els canvis del _commit_ __A__ a la branca principal utilitzant `cherry-pick`:
```shell
jpuigcerver@FP:~/git_cherry_pick (main) $ git checkout main
Already on 'main'
jpuigcerver@FP:~/git_cherry_pick (main) $ git cherry-pick 7dbdc25
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 7dbdc25... A. Canvi A
.yellow{hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".}
jpuigcerver@FP:~/git_cherry_pick (main|CHERRY-PICKING) $ git status
On branch main
You are currently cherry-picking commit 7dbdc25.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add <file>..." to mark resolution)
.red{both modified: README.md}
no changes added to commit (use "git add" and/or "git commit -a")
jpuigcerver@FP:~/git_cherry_pick (main|CHERRY-PICKING) $ vi README.md # Solucionem els conflictes
jpuigcerver@FP:~/git_cherry_pick (main|CHERRY-PICKING) $ git add README.md
jpuigcerver@FP:~/git_cherry_pick (main|CHERRY-PICKING) $ git add status
On branch main
You are currently cherry-picking commit 7dbdc25.
(all conflicts fixed: run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Changes to be committed:
.green{modified: README.md}
jpuigcerver@FP:~/git_cherry_pick (main|CHERRY-PICKING) $ git cherry-pick --continue
[main e579732] A. Canvi A
Date: Mon Dec 18 18:03:26 2023 +0100
1 file changed, 1 insertion(+)
jpuigcerver@FP:~/git_cherry_pick (main) $ git lga
* e579732 - (5 minutes ago) A. Canvi A - Joan Puigcerver (HEAD -> main)
* 6582d61 - (5 minutes ago) 2. Segon canvi - Joan Puigcerver
* 06ce5a6 - (5 minutes ago) 1. Primer canvi - Joan Puigcerver
| * 8453c8c - (5 minutes ago) C. Canvi C - Joan Puigcerver (develop)
| * c28155d - (5 minutes ago) B. Canvi B - Joan Puigcerver
| * 7dbdc25 - (5 minutes ago) A. Canvi A - Joan Puigcerver
|/
* 529847a - (5 minutes ago) Added README.md - Joan Puigcerver
jpuigcerver@FP:~/git_cherry_pick (main) $ cat README.md
# Git cherry pick
- Primer canvi
- Segon canvi
- Canvi A
```
Aquest lloc web utilitza galetes per millorar l'experiència de l'usuari