Ben_96
Ben_96

Reputation: 20

How to Rebase by date?

So I want to bring commits of another branch till specific date only and I want them to appear on my branch as if they were my branch's commit. So when I push my branch to github those commits should appear on top.

For ex: I want to bring 'main' branches commits to my 'dev' branch till let's say 2021.10.12 and when I push my branch to GitHub the commits must appear till 2021.10.12.

How is it possible?

So I tried checkout main@{'date'} but it is creating new pointing address, asking if I would like to publish it as new branch. But that is not what I want

Upvotes: 1

Views: 203

Answers (1)

LeGEC
LeGEC

Reputation: 51860

One way is :

# spot the commit on 'main' which you want to get,
# create a 'wip' branch on that commit:
git switch -c wip <commit>

# rebase that branch on top of your dev branch:
git rebase dev

# move your dev branch up to that point:
git checkout dev
git merge --ff-only wip

# you may now drop that wip branch:
git branch -d wip

# and push your dev branch:
git push origin dev

About the first command :

  • main@{'date'} will not look for "the last commit before 'date'", it will look into your local reflog (a history of what happened on your local repo), and get "the state main was in at 'date' on your computer".
    So: suppose your main branch was 3 months old (you hadn't run git pull for a long time), and you ran git pull today, main@{yesterday} will translate to "the 3 month old main".

If you want to get "the state of upstream main at 'date'", you can either:

  • copy the commit hash from some graphical view of your history (for example: copy the hash from github's GUI when you inspect the list of commits on branch main)
  • or use some of the many options of git log :
git log --until="<date>" -1 main

# or
git log --graph --oneline --until="<date>" main
   # and copy the hash from the first line

# or
git log --graph --oneline main
   # figure out up to what commit you want to include
   # and copy the sha of that commit

Upvotes: 1

Related Questions