September 3, 2024

Git: Only merge the last commit of a specific branch to the main branch

Note: this the second part of the wiki which shows how to only push the last commit to a remote: https://yctct.com/git-push-specific-commits-only-remote

You have a bunch of unmerged commits on a branch but only want to merge to the last one to the main branch. To do so, run:

$ git branch          # you are on hte branch where you want to pick the last commit from 
branch-with-the-commit-you-want-to-merge
$ git last        # print the hash of commit you want to merge
7f023b7 last commit       # copy the hash to the clipboard
$ git switch main 
$ git cherry-pick 7f023b7 # merge commit with hast 7f023b7 

Make sure that files of the last commit have not been modified previously on that same branch where you are picking the last commit from. Otherwise, reorder and squash (see https://yctct.com/git-squash).


personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki git

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88