nus-cs2113-AY2223S2 / forum

10 stars 0 forks source link

💡Useful git commands #47

Open haoyangw opened 1 year ago

haoyangw commented 1 year ago

Beyond the usual git commands, here's a compilation of some I found useful during my group's TP:

-git rebase -i HEAD~<how-many-commits>: Allows you to edit commits up to before the latest commit of your current branch. Change the first word before any commits(see highlighted words in screenshot below) from pick to edit to modify one or more commits' changes. Example screen of git rebase -i:

image

-git diff and git patch: If you have changes to transfer to another branch conveniently, do a git diff > changes.patch to generate uncommitted and unstaged changes, or git diff <another-branch> > changes.patch to generate all changes between current and . Then apply the changes in another branch using git patch changes.patch, assuming changes.patch is the output file from git diff earlier

-git reset --soft HEAD~<how-many-commits>: Uncommits the past <how-many-commits> commits but preserves all the changes as staged files in the current branch. You can then unstage changes/add changes and git commit again so that you can split up the changes into different commits, make additional changes, or anything else that may be useful

git revert <commit-SHA>: Undos the changes made in a specific commit(identified by the commit hash). This generates a new commit that is the opposite of the specified commit(undo changes), which can be helpful when you've already pushed changes to the remote repo and don't want to force-push to modify the commit history(which is not recommended).