░░▓██                                                                        
           ▒▓███                                                                        
          ░▒▓█▓█                                                                        
         ░▒▓█▓▓█                                                                        
        ░░▓▓█▒▒▓█                                                        ░█             
        ░▒▓█░▒▒▓█                                                       ░▒█             
       ░▒▓██ ░▒▓██                                                      ░▒█ ░▒████████  
      ░▒▓██   ░▒▓█        ████████      ░████       ████      ▒██      ░▒▒█░▒▒█▒▒▒▒▒▒█  
     ░▒▒▓█     ░▓█ ████████▓█████    ░▒███▒▒█     ░██▒▒██    ░▒██      ░▒██░▒▒█░░░░ ██  
     ░▒▓██   ███████▓▓▓▓▓▒▒██▒▒▒█  ░░▒██▒▒░▒█   ░▒█▒▒▒░▒██   ░▒███     ░▒█ ░▒██         
     ░▒▓██████▓▓▒▓█▒▒▒ ░▒▒█░░░░    ░░▒█ ░░░    ░▒█░░░  ░▒█  ░▒██▒██   ░▒▒█ ░▒█          
     █████▓▓▓▒▒ ▒▓█     ░▒▒█       ░▒██       ░▒██     ░▒█  ░▒█▒▒▒█   ░▒█  ░▒█   ░▒█    
  ██████▓▒▓      ▒▓█     ░░▒██     ░▒█        ░▒█      ░▒█ ░▒██░░▒▒█ ░▒▒█  ▒▒███████    
 ██▓▓▓█ ▒        ▒▓█      ░▒▒▒███  ░▒█        ░▒█     ░▒██ ░▒█  ░░▒██░▒██ ░▒▒█▒▒▒▒██    
▒▓▒▒▓██          ▒▒▓█   ░█  ░▒▒▒██ ░▒█        ░▒█    ░░▒█ ░▒██    ░▒█░▒█  ░▒▒█░░░░▒     
░░▒▓██            ▒▓█  ░▒█    ░▒▒█ ░▒█    ░▒█ ░▒█    ░░██ ░▒█     ░▒████ ░░▒██          
 ░▒▓█▒            ▒▓█  ░▒█    ████ ░▒███ ░▒██ ░▒███ ░░██  ▒██      ░▒██  ░▒▒█      ▒█   
 ░▒██             ▒▒▓█  ░▒█████▒▒   ░▒▒▒███▒▒ ░▒▒▒████▒▒ ░▒██       ▒█  ░░▒▒██████░▒█   
░▒▓█▒             ▒▒▓█   ░▒▒▒▒▒      ░░▒░░▒░░  ░░░▒▒▒▒░  ░▒█         ▒    ░░▒▒▒▒▒▒███       /\_      
░▓██               ▒▒▓   ░░░░          ░░░       ░░░░░   ░▒█                 ░░░▒▒▒▒▒▒    (゚、 。`7 
░▒▒                  ▒                                  ░▒██                    ░░░░░░░    {`  ~ \   )
 ░                                                       ░▒▒                                ),),\_,)/

Git.md

Coding/Git.md
  • date:
  • tags:
  • meow: yes

Rebasing

Note: you can similar visualizations to used here using:

git log --oneline graph HEAD origin/main main

Say you have a branch my-feature which is based on top of the main branch:

* Y (my-feature)
* X
* A (main)
* B
* R
.
.
.

Then someone rebased the main branch onto a different branch, s.t. after running git fetch origin we get the following situation. An accent is used to indicate a commit with similar changes, but based on a different parent commit.

* B' (origin/main)
* A'
* P
| * Y (my-feature, origin/my-feature)
| * X
| * A (main)
| * B
|/
* R
.
.
.

What we want to do is to reapply the changes X and Y on top of origin/main and have that be the new my-feature branch. This is called rebasing.

In this specific case we want to do:

# Let A be the SHA of the last commit on main before that got force pushed!
git rebase A --onto origin/main --rebase-merges

The --interactive flag can be used to give a list of commits that will be rebased. This can be useful to check if your SHA is set properly.

After this we will have the following situation:

* Y' (my-feature)
* X'
* B' (origin/main)
* A'
* P
| * Y (origin/my-feature)
| * X
| * A (main)
| * B
|/
* R
.
.
.

We then update our local main branch and update the feature branch.

# you already are on the my-feature branch
git push -f
# need to update our main branch too
git checkout main
git pull

Which then clears out the stale branches

* Y' (my-feature, origin/my-feature)
* X'
* B' (main, origin/main)
* A'
* P
* R
.
.
.