-3

Commit 1 contains file1 and file2

I did some changes in file2 and committed it as commit2 But I want commit2 to include both file1 and file2 even though there is no change in file1.

Or

I would want a single commit that includes/merges both commit1 and commit2

Is there a way to do this in git?

Why do I have to include unmodified files in my commit?

My deployment happens using hash id generated as part of the commit. So in this case if I provide commit2 then it will include only file2, hence I would like to create a commit that includes the changes in commit1 and commit2. This may be an unconventional way, but this is my requirement!

Vivek
  • 4,452
  • 10
  • 27
  • 45
  • 5
    Why would you need to include a file in a commit if you didn't actually change it? What use case do you have for doing this? – mason Jul 30 '21 at 18:15
  • @mason: my deployment for a CR can include only one commit id and we have to include all of the files related to the CR in the single commit. – Vivek Jul 30 '21 at 18:18
  • 1
    Just squash the 2 commits into 1. That question has been answered here: https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git – Dan Csharpster Jul 30 '21 at 18:20
  • @Vivek How is a file related to a CR if it hasn’t been changed? – esqew Jul 30 '21 at 18:20
  • 1
    @Vivek You could add a space in `file1`, and disable `ignore whitespace` in Git. You will be able to commit it. Or add comments or vice versa. If you want to include all previous changes in `commit2`, use `squash` as others stated – Huy Phạm Jul 30 '21 at 18:20
  • Actually, even if at first sight, "`commit2` does not contain *changes* for `file1`", it appears **`commit2` does contain `file1`** (because a commit is not a diff, a commit contains a whole tree). – ErikMD Jul 30 '21 at 18:20
  • I still don't understand. What do you mean by CR? And if you're doing a deployment, wouldn't you just checkout the code at that commit, which would include all of the files? Your use case here still isn't clear. – mason Jul 30 '21 at 18:20
  • 1
    What do you mean by "contains"? I commit does not contain a file. It contains a reference to a tree. The tree contains a reference to a blob. The content of file 1 is in a blob that is in the tree referenced by your commit. So the commit already "contains" file 1. – William Pursell Jul 30 '21 at 18:25
  • 1
    While you *could* squash the commits, I'd really hestitate to actually recommend that until knowing exactly what problem you're trying to solve. Squashing is likely unnecessary. Same thing with making an artificial change such as whitespace/comments just to force it to appear in a commit. – mason Jul 30 '21 at 18:26
  • @vivek all revisions in git contain _all_ files, modified or not modified, regardless. Now, you what you can't force git to do is to _report_ it as modified between the two revisions when it hasn't been. – eftshift0 Jul 30 '21 at 18:41
  • 1
    If you want to get like the list of all files in a revision, you might try: `git ls-tree -r --name-only HEAD` (or any branch or revision instead of `HEAD`) – eftshift0 Jul 30 '21 at 18:43
  • @all: My deployment happens commit-wise and not using any branch or repository. So at any point in time, the commit I provide to the deployment team is "expected" to have all changes that are part of CR(Change Request). Here in my case, consider i had 10 files modified as part of CR but later I realized that the 10th file needs some change. So if I do via the usual way then the `commit2` will only show 10th file as part of it! Hope it clarifies for you, if not, please read the link available in the answer I have selected! – Vivek Jul 31 '21 at 06:37
  • @WilliamPursell Agreed! When `commit2` is viewed would it refer `file2` alone or both the files? – Vivek Jul 31 '21 at 06:40
  • @HuyPhạm yes, currently we follow the "artificial" changes/commit you mentioned to include other files that are part of the change request. But I would like to know the `Git` way of doing this, hence this question. – Vivek Jul 31 '21 at 06:43
  • @Vivek what do you mean by "viewed"? You could do `git show commit2:path/to/file1` to see the content of that file. – William Pursell Jul 31 '21 at 13:33
  • 1
    "My deployment happens using hash id generated as part of the commit. So in this case if I provide `commit2` then it will include only `file2`"—then your deploy process is _badly broken_. Deploying `commit2` should actually check `commit2` out, in which case both your files will be there. – ChrisGPT was on strike Jul 31 '21 at 22:49
  • @chris i use bitbucket to check my commit and pull request. i cant see `file 1` in `commit 2` – Vivek Aug 01 '21 at 04:04
  • 1
    @Vivek, I don't understand what "i use bitbucket to check my commit and pull request" means in this context. I _strongly_ suggest you ask a new question that clearly shows how you are currently deploying as a [mre]. _That_ is the process that needs to be fixed. – ChrisGPT was on strike Aug 01 '21 at 12:22

1 Answers1

1
git reset --soft HEAD~2

where 2 is the number of commit you want to rollback. After that, you will have all the changes you made in both commits as uncommited. You will need to commit again.

  • I would hold off on answering until I clearly understood the problem trying to be solved. – mason Jul 30 '21 at 18:26
  • 1
    the person said: my deployment for a CR can include only one commit id and we have to include all of the files related to the CR in the single commit. I had to do this in some open source projects. It's a requirement for contributing, like here: https://github.com/keycloak/keycloak-operator/blob/master/CONTRIBUTING.md#contributing-to-the-keycloak-operator – Alexandre Lussier Jul 30 '21 at 18:50
  • @mason Whatever provide above is the requirement! and alex's answer did resolve my problem! – Vivek Jul 31 '21 at 06:23