-2

I am doing some code copy and merge, first I create a new branch git checkout -b new_branch to store all merged code, after I created it, I deleted all the previous code to get a empty branch and then copied code to it.

And later I found that, if I just copy the code from other branch, all commits history would be lost. So I want to checkout to the branch while it is created, and based on that commit, to rename/refactor the code.

So how to find that commit ID?

Litchy
  • 623
  • 7
  • 23
  • 1
    A branch is a pointer to a commit. No new commit is created when you create a branch. You just have to see where the newly created branch points to (and there are plenty of resources that tell you how to do that, for instance [this](https://stackoverflow.com/questions/949314/how-to-retrieve-the-hash-for-the-current-commit-in-git)) – mnestorov Sep 22 '21 at 07:42
  • If you've lost the commit you want, and it's recent, you can use the `git reflog` command, which is a bit of a recycle bin for `git` – Jiří Baum Sep 22 '21 at 10:34
  • Also, you may want to go through some git tutorials or books or other resources; it feels like you're missing background knowledge here, rather than the solution to this specific issue? – Jiří Baum Sep 22 '21 at 10:41
  • You can run `git log` to see a list of commits. Is that what you're looking for? – mason Sep 22 '21 at 10:50
  • I would guess is a visual inspection of the commit graph would give you your answer. Try `git log --format=oneline --graph --decorate --all` or any visual log inspector that shows you all the branches. – joanis Sep 22 '21 at 13:47
  • There is also `git merge-base`, which will directly give you the most recent commit two branches have in common. `git merge-base new_branch master` might give you the answer you're looking for. – joanis Sep 22 '21 at 13:49
  • @mason This is based on the assumption that there are so many logs which are too hard to find. – Litchy Sep 23 '21 at 06:03

1 Answers1

-1

commits are made to keep track of files changes. they are snapshots of files whereas branches are just made to organize these snapshots in a timeline.a branch represents an independent line of development. there's no real need to keep track of created new branches but you can always check your list for new branches.