---
# System prepended metadata

title: Git cheat sheet ;)
tags: [git]

---

###### tags: `git`

# Git cheat sheet ;)

*Author: **Jérémy Habit***
*Created at: **26/02/2021***
*Last updated : **26/02/2021***

## How to rebase

#### Step 1 : get the last version of the main branch

1) **`git checkout my-main-branch`**
2) **`git pull`**

#### Step 2 : get the last version of the sub branch

3) **`git checkout my-sub-branch`**
4) **`git pull`**

#### Step 3 : rebase the sub branch from the main one

5) **`git rebase my-main-branch`**
6) check & resolve potentials conflicts then ==> **`git rebase --continue`**
7) **`git push -f`**

## Commit naming rule

1) Based on [conventional commits : check it, it's easy to understand ;)](https://www.conventionalcommits.org/en/v1.0.0/#summary) : **`feat(login) :`**
2) Write the release's number the feature is destinated for : **`@2.35 /`**
3) Write the sprint's number the feature has been developped : **`SP-58 /`**
4) If it exists, write the jira's ticket's number of the feature :  : **`MP_5467 /`**
5) Write a description : **`login screen done`**

> Exemples :
* **`feat(login) : @2.35 / SP-58 / MP_5467 / login screen done`**
* **`fix(postOffer) : @2.34 / SP-57 / MP_5434 / weight & capacity max value`**
* **`ci(gha) : @2.35 / SP-58 / MP_5467 / git hub actions updated to github flow`**
* **`docs(git) : @2.35 / SP-58 / docs for git commands and rules`**

## How to merge several commits into ONE only

In order to keep a clean and 'easy to use' historical of commits, it's better to merge several commits into one commit BEFORE to merge it on **`main`**(quality) or on **`feature-branch`**.

> For the exemple :

* the current feature branch : **`my-feature-branch`**
* the parent branch : **`main`**
* number of commits: 3

#### Step 1 : identify the last commit of the main branch

1) **`git log`** 
> you should see something like that. The 3 commits are inside the blue frame.

![](https://i.imgur.com/6rNmmnW.png)

2) Copy the sha1 of the last commit of the **`main`** branch: **`4f55ad331e124b32c72f14b29911cd56d2ed05b6`** (inside the pink frame)

3) **`git rebase -i 4f55ad331e124b32c72f14b29911cd56d2ed05b6`** 
> You should see something like the following screen. 

![](https://i.imgur.com/JR4aZPu.png)

4) Press the **`i`** key to active the insert mode. Now, you have to make a choice for each commit by writting commandes like pick, reword, fixup etc before each commit : it's easy
    * **pick** or **reword** the first one.
    * **fixup** all others.
> Like follwing :
![](https://i.imgur.com/TfKZ2kF.png)
 

5) Then leave the insert mode by pressing **`esc`** key.
6) Type **`:wq`** and press **`enter`**
7) if you chose to reword a commit, then you will see a new screen to rename it. If you chose to reword 3 commits, then you will see three new screens, one after other. (Use insert mode and :wq to save as above)
8) check & resolve potentials conflicts then ==> **`git rebase --continue`**
9) **`git push -f`**
10) check the github repository, it should be done :+1: 



## Multiple lines

pour visual block : control + v

pour insert multiple line ensuite : maj +I, insérer des caractéères puis echap.

pour delete multiple lines ensuite : juste appuyer sur "d"