0

I have just made a commit of some changes to about 4 files. After sending this commit to remote, I have noticed that a couple of extra files were included with this commit. How can I

  1. Make sure that the remote does not contain changes in these files.
  2. Make sure that the production server also does not contain these files (I already did a pull there)
  3. Have the original changes to these two files in my local working tree.

In other words, I would like to revert changes as far as these files are concerned.

In case it matters, this is the latest commit. There has not been any after this one. Also, these were newly created files, not files that were being changed.

Edit:

When I do git show, I can see that the files I actually wanted in the commit have something like:

diff --git a/mysite/apps/common/models.py b/mysite/apps/common/models.py
index 9d86707..1a53b94 100755
--- a/mysite/apps/common/models.py
+++ b/mysite/apps/common/models.py

whereas the files that were somehow wrongly included have

diff --git a/mysite/apps/payment/migrations/0011_auto_20221006_1850.py b/mysite/apps/payment/migrations/0011_auto_20221006_1850.py
new file mode 100644
index 0000000..26320ba
--- /dev/null
+++ b/mysite/apps/payment/migrations/0011_auto_20221006_1850.py

Where did they come from?

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107

3 Answers3

2

Do not overcomplicate it by asking to keep it in your working tree. If you do a revert (and lose it on the working tree) and push so that it gets corrected where you need it now), then you can find out how to git it back the way you want, which is done (after a revert) with:

git checkout HEAD~ -- file1 file2

And then you can commit that with the right commit message, the right files and stuff.

By the way, it's not like what you asked for can't be done... it can be done, but I do not see why you want to do it that way when stuff is in production already.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Is it possible that the two files were somehow added to the commit, but don't exist in my project? I don't recall adding them, they just appeared in the commit out of nowhere. – MadPhysicist Oct 17 '22 at 13:50
1

Well! you can't keep them in the commit if you don't want to be in the remote/production server. If you think there are some files added to your last commit by mistake then you can revert back to last staging state by following:

git reset --soft HEAD^

This will revert your last commit and move your head to previous commit while keeping all the files in staging. From there you can remove unwanted files by git reset <file>.

rafathasan
  • 524
  • 3
  • 15
0

Well, lets try to answer on your questions:

1. Make sure that the remote does not contain changes in these files.
2. Make sure that the production server also does not contain these files (I already did a pull there)
3. Have the original changes to these two files in my local working tree.

Make sure that the remote does not contain changes in these files

I don't know your SCM vendor. If you have UI service, just open commit's link and explore files. If you have access to a remote server, you can just:

ssh me@otherhost "cd repo && git log -n 10"
OR
ssh me@otherhost "cd repo && git show --pretty='' --name-only <commit>"

Browse and display files in a git repo without cloning

Make sure that the production server also does not contain these files (I already did a pull there)

You also can connect to a remote production server and find needed commit with

git log --oneline --decorate --oneline --graph <branch>

If you know commit hash on production server, you can use follow

git show --pretty="" --name-only <commit hash>

to get list of changed files

Have the original changes to these two files in my local working tree

So, if your changes were published (commited) and you don't have another local changes (instead just git stash then and git stash list, git stash apply) you just can reset your local HEAD to a previous commit (parent).

git reset --hard HEAD~ #if you didn't create any commits
# If you made some commits after publishing
# You should to rewrite linear history of branch
# Or make revert commit
# Case with rewriting history of your branch, let's name your branch as devel
1. find your published commit, let's name it as `git tag unstable <unstable_published_commit>`
2. Move temp_devel to parent of an unstable - `git branch -f temp_develop unstable~`
3. Checkout on temp_devel - `git checkout temp_devel`
4. "Pull" your changes from unstable commit - `git cherry-pick -n <unstable_commit>` and `git status` and remove unwanted files
5. git commit -m 'Your message'
6. Will be created a new commit with your two files
7. Rewrite history of `devel`, `git rebase -i HEAD devel`
8. You are in an interactive mode right now
9. You can remove or edit some of commits, read an instruction
10. `:wq` in `vim` to save and close your file
11. If rebase will be successful you can publish your `devel` branch with correct commit with two files. Otherwise just run - `git rebase --abort` to return everything as it was.

if you know that you doing at 8 step, than use git rebase --continue to continue your rebase state

It's remove your local changes and indexed changes (be careful with it, use git status to make sure)

You also can use .gitignore file to store list of files or directories to ignore when you adding your changes to an index stage.

if you need your couple files with payload data, you can use git cherry-picn -n <published_commit_hash> to get same changes and exclude unwanted files from index stage with git checkout -- filename or git reset --filename, look at git status help message

Evgenii
  • 86
  • 7