Do these only if you havent pushed your changes yet.
To modifying message without doing another commit:
git commit --amend -m "new message here"
Note: that the commit hash would be changed once you change the message.
To add the files to the last commit:
git commit --amend
To move commit from one branch to another, you can use
git cherry-pick command.
To do this, get the commit hash of the commit to be moved using:
Note: only few of characters from hash will work as well.
Now move the commit to another branch:
git cherry-pick <hash-characters>
To remove the commit from the original branch:
Three types of resets:
- Mixed (default)
This removes the commit but keeps the changes in the staging area.
git reset --soft <hash-characters>
This will remove the commit entry from the
git log. But changes will be available in the
staging area, including new files.
If you dont specify the type of reset, it will default to mixed reset.
git reset <hash-characters>
The files will be added to
working directory instead of staging area unlike soft reset.
Will remove the commit along with the changes that were done as part of that commit.
git reset --hard <hash-characters>
This will still keep the new files(as untracked files).
Removing new files that are yet to be added to staging area, you can run the following command:
git clean -df
d specified directories and
f specifies files. So, clean will remove both untracked directories and files using this command.
In case of unwanted reset, you can still go back to having your changes as it was before reset(not always).
To revert back the state of the repo, you can get list of operations that were performed on the repo using:
Now pick the hash of the operation, that you want to revert to. Git will set the state of the working directory to what it was after this commit:
git checkout <hash-characters>
This will set the working directory to
detached HEAD state. This will add a new commit to git log for that
hash checked out. You can check the same using:
To save these changes we will have to make a branch from the current
hash (checked out commit):
git branch <new-branch-name>
Revert creates new commit to reverse the effect of earlier commit. Doesnt modify or delete existing commits, so that the history remains intact unlike reset.
To do this get the hash of the commit to be reverted using
git log and run the following:
git revert <hash-of-commit-to-be-reverted>
This will revert working directory to the state, before the commit whose hash is specified here. It will add a new commit to git log for this action.
To compare two commits, get the hash of the commits from the
git log and run the git diff:
git diff <hash-of-commit-1> <hash-of-commit-2>