I have commited some changes from feature branch onto remote master branch and opened pull request. But i have forgot to fix phpcs violations (running code sniffer) for which i need the difference between when there were no changes in a file/commit and changes i made - I need to get back into the point where i was before commiting - to have all the rows changed saying "modified" and i need to do that for 3 commits of that feature branch. Would someone please help me how to achieve that, or how is it called?
Asked
Active
Viewed 90 times
-1
-
1Are you asking how to use `git rebase -i`? – knittl Sep 29 '22 at 19:11
-
Please only add tags that are relevant to your issue. This is only about git, not PHP or phpsniffer. – M. Eriksson Sep 29 '22 at 19:17
-
git rebase does not work – Qwertz Sep 29 '22 at 19:41
-
Why not? It allows you to edit every single commit in the todo list. It allows you to reset before a commit, thus staging/unstaging changes as you like, then redo the commit – knittl Sep 29 '22 at 19:48
-
What should i write into that file? – Qwertz Sep 29 '22 at 20:05
-
edit or pick [commit_id] results in Could not apply [commit_id] – Qwertz Sep 29 '22 at 20:17
-
Now I somehow ran edit for 4 commits, but none of the files showing modified so i guess its not what i want to do – Qwertz Sep 29 '22 at 20:28
-
Does this answer your question? https://stackoverflow.com/questions/57179501/how-to-split-git-commit-in-this-specific-way or even described in the [git rebase man page](https://git-scm.com/docs/git-rebase#_splitting_commits) – knittl Sep 29 '22 at 20:59
-
That question you linked is i think too complex. Imagine empty branch with no commits, lets call it EmptyBranch. And i want to copy-paste all the changes from FixBranch (changes being already commited and pushed) onto that EmptyBranch so i can run tool that needs to see the changes. While that EmptyBranch as result being the FixBranch. So in the end having one more commit where the tool ran and showed me errors that i fixed. Is it possible without well.. copy-paste? (I want all that happening from single branch, 2 branches are just for demonstration) – Qwertz Sep 29 '22 at 21:12
-
And one thing i forgot to mention if it even matters, in 3 of 5 commits im creating new file, others are just modifications. So i want to get into stage where i can see "modified file(s)" and/or "new file(s)" in git status/IDE – Qwertz Sep 29 '22 at 21:57
-
Checkout the branch you want and make sure you have no uncommitted changes. Then run a "git reset HEAD~X" (x being the number of commits you want to go back). This should leave you with a set of staged files that represent all the changes in the X commits you went back. Once you run your validation tool, you can stage the new changes it did then commit everything. – Yazeed Sabri Sep 30 '22 at 00:55
-
Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – trojan Oct 03 '22 at 21:16
1 Answers
0
Use an interactive rebase to edit/redo all of your commits. You need to follow the procedure mentioned in the splitting commits section in the git rebase
documentation without actually splitting your commits.
Before running this, make sure to have a backup of your branch and a clean working copy (git stash push -u
).
git rebase -i $basecommit
# change "pick" to "edit" for the commits you want to redo
# rebase will stop at first "edit" commit
git reset --soft HEAD^
# run your tool, make necessary changes
# add changes
git commit -C HEAD@{1} # make a new commit reusing message and author from the commit before reset
# resolve potential conflicts
# rinse and repeat
Another option would be to use git cherry-pick
:
git checkout $basecommit^{commit} # go to detached HEAD state
git cherry-pick --no-commit $firstcommit
# run your tool, make necessary changes
# add changes
git commit -C $firstcommit # make new commit reusing message and author from first commit
# repeat for $secondcommit, $thirdcommit, etc.
git checkout -B $yourbranch # label the newly created history with your branch name

knittl
- 246,190
- 53
- 318
- 364
-
Looks like git reset did the trick after rebasing, thank you also for linking the documentation part. – Qwertz Sep 30 '22 at 07:32