Skip to content

Commit

Permalink
Adding post using Git rebase and amend
Browse files Browse the repository at this point in the history
  • Loading branch information
ehvs committed Jul 23, 2024
1 parent fcf825c commit dea2047
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/blog/posts/git-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: Git rebase and amend
summary: Using commands git rebase and amend
authors:
- Hevellyn
date: 2024-07-23
categories:
- Cheatsheet
slug: git-rebase-amend
tags:
- git, branches, rebase, amend
---

When handling dozens of different branches in a Git repo, doing a rebase and all the right commands to make a clean push can be a bit challenging.
Here are my notes on how rebase and push it with more confidence.

<!-- more -->


# Git real life operations
## Scenario
- Main as default branch
- Branch named `feature1` and `feature2` , both created from the same head of `main`.
- Branch `feature2` is already **merged** and I am still working on `feature-1`.
- Branch `feature1` already has a **saved** commit. Eg: `1a2bc3d4`

### Purpose
The purpose here, is to add **new** changes to an existing commit from my branch `feature1`.
The outcome of it, would be to not have 2 commits, in the same PR (Pull Request).

### What to do?

1. Pull latest changes to my local `main` branch.
```
git checkout main
git pull
```

2. Rebase the new branch (`feature2`) from `main`.
Assuming that the latest changes does not affect my own changes, rebasing now from `main` should **Succeed**.
```
git checkout feature1
git rebase main
```
???+ note
Running `git rebase` modifies the commit SHA. Meaning, that SHA `1a2bc3d4` will change to another hash.
3. Do the additional necessary changes. Once done, stage it them:
```
git add .
```

4. Let's now ADD these new changes to our current existing commit. The flag `--no-edit` means that I will not change my original commit message.
```
git commit --amend --no-edit
```

5. Push the changes of my local branch (origin) to my remote branch. The flag `-f` is because I am changing the history of commits.
```
git push origin feature2 -f
```

Voila! Once this is done, I should be able to see the new commit SHA, with my new changes in my PR that is already open.

0 comments on commit dea2047

Please sign in to comment.