My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label commit. Show all posts
Showing posts with label commit. Show all posts

Tuesday, March 23, 2021

How to undo a Git Add Command befor any commit

 This is a very simple mistake by developers that they accidentally add a file locally using the Git add command.


Sometimes a git add * command also adds all the files. What to do in such situation is the aim of this post.


Let's take an example:

Suppose, I mistakenly added files to Git using the command:

git add anwar-jamal-faiz.txt

I have not yet run git commit. Is there a way to undo this, so these files won't be included in the commit?

You can undo git add before commit with


git reset <file>

eg: git reset anwar-jamal-faiz.txt

which will remove it from the current index (the "about to be committed" list) without changing anything else.


You can also use: git reset

without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.


Cheers ;)


Tuesday, January 22, 2019

How to undo a Git commit that was not pushed

To undo a Git commit that was not pushed, you are given a few major options:

Method 1:
Undo the commit but keep all changes staged
git reset --soft HEAD~; 


Method 2:
Undo the commit and unstage the changes
git reset HEAD~; 
or 1 git reset --mixed HEAD~; 


Method 3:
Undo the commit and lose all changes
git reset --hard HEAD~;


Method 4:
In case you just want to rewrite the commit message, you could use:
git –amend instead.

eg: git commit --amend -m "an updated commit message"



However if you only want to undo Last Git Commit

Then easiest way to undo the last Git commit is 
$ git reset --soft HEAD~1


I will add following more helpful commands to check commit history, go back to a commit, and remove commits etc:
  1. git log
  2. git log --oneline to simplify the output:
  3. To test a specific commit: git checkout . You will be in 'detached HEAD' state. You can look around, make experimental changes and commit them, even create a new branch from here using git checkout -b new_branch_name
  4. To fix the detached head issue: git checkout
  5. Undo this commit: git revert . This will create another commit operation in github

Free Additional Tip:
To Undo a git add i.e. remove files staged for a git commit
1. git reset filename.txt OR
2. git reset *
After this you can add again the required files


Cheers :)
-Anwar Jamal Faiz


Read some of my other posts related to Git tricks below :

Friday, March 24, 2017

Git : Cherry-pick a commit in branch

Friends, it a very common situation that if you are in a new branch of your own but want to bring information or changes from another branch.

Sometimes you also land in a situation where you have been asked to cherry pick a commit. But you have no clue idea what it means! Trust me. That was the case with me when i heard this while I was in Symantec. The history is that we used Perforce for the version control, but a new team started using Git. And cherry picking was something that needed a beer to actually understand ;)

So what does cherry picking a commit in git mean? How do you do it?

Solution:
Cherry picking in git means to choose a commit from one branch and apply it onto another.
Note that it is different from merge and rebase which normally applies many commits onto a another branch.

Syntax:
git cherry-pick

Example:
First make sure you are on the branch you want apply the commit to. For instance suppose you have to bring changes in master itself:
git checkout master

Now Execute the following:
git cherry-pick
Eg: git cherry-pick 3d05ac6159e4de0bba404f40f8abdca3af1903d3


Voila!
You're done. Just check your branch for the new changes. The diff history will show you changes in that commit to enter in your branch as well.

CAUTION:
In case you get an error like:
fatal: bad object 02x661db5adf2anwarJamalFaizeacf09f048b8b11
It means that the branch information is not locally present. To solve this I have already written another post. Thank me for this ;)  http://www.w3lc.com/2017/03/fatal-bad-object-error-in-git-cherry.html


Note:
Do also read if you want to change the name of your git branch. The process to do so is explained fully on my earlier blog post:
W3LC: Rename Branch Name in Git