<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://kb.owrench.com/index.php?action=history&amp;feed=atom&amp;title=GIT-_fixing_common_mistakes</id>
	<title>GIT- fixing common mistakes - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://kb.owrench.com/index.php?action=history&amp;feed=atom&amp;title=GIT-_fixing_common_mistakes"/>
	<link rel="alternate" type="text/html" href="http://kb.owrench.com/index.php?title=GIT-_fixing_common_mistakes&amp;action=history"/>
	<updated>2026-05-04T04:23:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://kb.owrench.com/index.php?title=GIT-_fixing_common_mistakes&amp;diff=136&amp;oldid=prev</id>
		<title>imported&gt;Kishor at 09:53, 9 March 2018</title>
		<link rel="alternate" type="text/html" href="http://kb.owrench.com/index.php?title=GIT-_fixing_common_mistakes&amp;diff=136&amp;oldid=prev"/>
		<updated>2018-03-09T09:53:37Z</updated>

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