2

I have following files in my directory, let's say

helloworld.go

index.html

main.js

These all are latest commit of branch validation.

Now I want to create a new branch which contains,

helloworld.go <previously committed [particular hash] file in same branch>

index.html [current branch's latest commit]

main.js [file from another branch newvalidation in a particular commit]

newone.c [file from another branch newvalidation in a particular commit]

Is that possible?

mike
  • 1,233
  • 1
  • 15
  • 36
imaheshwaran s
  • 167
  • 1
  • 11

2 Answers2

3

First, don't use the confusing and obsolete git checkout command

Use:

Second, there are two approaches to your situation:

  • either you create a new branch from where you are, then restore select files from an old commit: that works only if you have a few files to restore
  • or you create a new branch from an old commit, and restore select files from validation: that works if you want everything from old commit, but a few files from validation

In the first case:

git switch -c newBranch validation
git restore -s <oldSHA1> -SW -- helloworld.go
git restore -s <oldSHA1> -SW -- main.js
git restore -s <oldSHA1> -SW -- newone.c

That will change the working tree and index directly, meaning all you need to do is a commit.

In the second case:

git switch -c newBranch <oldSHA1>
git restore -s validation -SW -- index.htm
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

The general way to checkout a file at some commit commit is:

git checkout <SHA-1 of commit> path/to/your/file.ext

So, you may apply this for helloworld.go, main.js, and newone.c, i.e. use:

# from validation
git checkout -b validation_new
git checkout abc123 helloworld.go
git checkout def456 main.js
git checkout ghi789 newone.c

At this point, you may add the changes to the index and commit, if you wish. If you need to find the SHA-1 hashes of the above three files, then you may use git log to do that.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360