The sweet and sour of Git command line

Fixing the commit

Do these only if you havent pushed your changes yet.

Message alone

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.

Adding files to the commit

To add the files to the last commit:

git commit --amend

Moving commit to another branch

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:

git log

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:

Resetting Commit

Three types of resets:

  • Soft
  • Mixed (default)
  • Hard

Soft Reset

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.

Mixed Reset

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.

Hard 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).

Working with untracked files

Removing untracked files

Removing new files that are yet to be added to staging area, you can run the following command:

git clean -df

Where, d specified directories and f specifies files. So, clean will remove both untracked directories and files using this command.

Reverting back your commit

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:

git reflog

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:

git log

To save these changes we will have to make a branch from the current hash (checked out commit):

git branch <new-branch-name>

Reverting commit that has been pulled by others

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.

Comparing commits

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>