Can we backtrack multiple files simultaneously?

Question

As we seen in this lesson, we can stage and commit multiple files simultaneously. Can we also backtrack on multiple files at the same time?

Answer

The answer is yes if each of the files that you would like to backtrack are in the same commit and the same branch. If this is the case, then as we see from git status, we can backtrack to the previous commit using the command

$ git checkout -- <file> ...

where the ellipses are replaced by other files which you would like to backtrack.

6 Likes

I’d just like to add that in that particular exercise you can add/backtrack/commit multiple files with this syntax:

git add scene*

which is easier than scene-1.txt scene-2.txt scene-3.txt…

:slight_smile:

13 Likes

Or you can just do this

git add .

3 Likes

This actually worked for me whereas trying to add by “scene-3.txt” etc didn’t work.

1 Like

In this exercise, for the part

In scene-3.txt , everywhere you see the name “LARRY” change it to “LAERTES.”
I would like to use the following code on the command-line as shown below.
Capture

However, when you display the contents of the file afterwards, the file is empty.
It works if you output to a new file then rename it to the file you want in this case, scene-3.txt
What would be a good way of accomplishing this?
Thank you for your help.

1 Like

You could, for example, write the output and move operations on one line using &&:

sed 's/LARRY/LAERTES/g' scene-3.txt > tmp && mv tmp scene-3.txt

sed also provides an options to do this place:

image

It makes sense that in place is not the default behavior, too risky.

1 Like

Thanks bleier. This one works better for me using the wildcard* for series of filenames.

May I ask what is the tmp here?

I used tmp as the (arbitrary) name of the temporary file in which to store the sed output, before renaming that temporary file again. Does that help? :slight_smile:

1 Like

shouldn’t put “–” before the word when the syntax is not like that

I didn’t understand what is the use of git add ffilename1 filename2, how it’ll help in real life.

Thank You

you could add the files one a time as well:

git add filename1
git add filename2

but that is a lot more work.

1 Like

but what is the main use of it, I’m not able to understand that

of what? adding files to the staging area? So that you can commit the changes/files.

1 Like

Ok Ok, I got it and one more thing I want to know
If I add multiple file in staging area, can I change the all the file at the same time

what does this mean? The sentence doesn’t make sense.

Sorry for so much of confusion
I’m asking suppose I add 3 file into the staging area and there are some changes which has to be done in the file and those changes are same in all those 3 files, so can I make edit those changes in those 3 files at the same time

what does this have to do with git?

the 3 files in the staging area stay the same.
the changes would show as changes in the current working directory

so the 3 files could be commited with the changes in the staging area, which will ignore the changes in your working directory. Or you could add these changes to the staging area as well

you could literally do an experiment with this:

  1. make changes to files
  2. add the files to staging area.
  3. show status and difference (git status and git diff)
  4. make more changes toe files
  5. run git status and git diff again