GIT- fixing common mistakes
This page contains the various methods in GIT, which you can use to rollback any changes you've made or correct accidental mistakes.
1. If you've made some local changes in any git project file and have saved it, but you need to revert the change now,
$git checkout filename
2. Made an incorrect commit message, and you need to change it
$git --amend -m "correct message"
3. Forgot to add a file into the last commit section, and to fix it, use --amend (with file as argument)
Eg: #touch .gitignore
$ git status ==> gitignore is untracked.
$git add .gitignore ==> now its in staging area.
But we now need it to add it to last commit.
$git commit --amend
An interactive editor will now open, containing previous commit message and listing new file (.gitignore). Just save and close it.
Done.
$git log ==> we can see that only last commits are there.
4. Accidentally committed on wrong branch (say master instead of development)
git cherry-pick command can be used to solve this.
cherry-pick creates a new commit based on original commmit. (does not delete original).
The process is,
1. git log
=> then select the hash of the needed commit.
2. Switch to subtract branch (branch where commit was actually needed).
$git checkout development.
$git log (to verify)
$git cherry-pick <hash from master>
$git log (now see the commit is on development).
Now we need to delete the commit from master branch.
git reset command can be used for this.
The process is,
$git checkout master
Then, #git log (see the commits).
say the hashes are like,
hash1
hash2
.
.
and hash2 is the point where we need to go back to.
We can try either soft, mixed or hard reset for the purpose.
Soft:
i) copy the hash of needed point.
ii)git reset --soft <hash>
iii)git log (see the last commit is not there anymore)
iv)git status (there are still changed files present in the staging area.)
Mixed:
i)copy hash of 1st commit.
ii)git reset <hash>
iii)git log (last commit is gone, which is what we need)
iv)git status (changes are not in staging area (added) but still present in working directory).
Hard:
i)copy hash of 1st commit.
ii)git reset --hard <hash>
iii)git log (same)
iv)git status (modified files are not shown. ie, modification reverted for that tracked file. (but any untracked file will remain in working directory).
If we still want to remove untracked file,
$git clean -df (remove all untracked files)
Ref: To learn more about soft, mixed and hard resets, see : https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard
https://davidzych.com/difference-between-git-reset-soft-mixed-and-hard/
5. To get the changes back when we have accidentally done a #git reset --hard
$git reflog (show all git activity)
- Now copy hash before the reset event.
- Then #git checkout <the above said hash>
- git log (we have now our changes back).
Howeevr, we are in a detached head state. (will be trashed later).
To save these changes, make a branch from it.
$git branch backup
$git branch (shown current one as detached HEAD)
$git checkout master
$git branch (backup branch is there. If you check it, the needed change is there).
6. Made some unwanted changes in github, and some people have already pulled these changes into their systems.
** Use git revert to solve this.
This revert the changes of a specific commit. It doesn't alter or delete git history.
Example:
$git log
<hash1>
<hash2>
.
.
where hash1 is the unwanted commit.
$git revert hash1 ( A message will pop up, save it and exit).
Done.
7. Git stash is another useful command which can be used everyday. You can see more of it in this page: http://192.168.1.103/mediawikisb/index.php/GIT_stash