Friday, March 24, 2017

Fatal: bad Object error in Git cherry-pick - Why Exception is thrown during cherry picking in Git ?

In one of my earlier blog post, I explained how to cherry-pick in Git. To understand more about what does cherry picking a commit in git mean and how to do it, refer:
W3LC: Git Cherry Pick Command Tutorial

This post is to deal a situation when you get a fatal error in your attempt to cherry pick. The error is as follows:
"fatal: bad object"

So here I would like to explain why this occurs. The cause is that there is no information about remote branch on your local system.

Technically, if the branch is not locally present - you don't have the right data. You will errors like " fatal: bad object "

Solution:
To solve this you first need to fetch information on your local system. To do this use fetch command of Git.

1. fetch just the one remote
git fetch
2. or fetch from all remotes
git fetch --all

Here we should take a short break and understand what is Git Fetch command:

W3LC Definition: In the simplest terms, git pull does a git fetch followed by a git merge.
This means that the fetch operation never changes any of your own local branches under refs/heads, and is safe to do without changing your working copy. A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. This updating your other remote-tracking branches is called as fetch.


So, let us assume that you fetched the information about all branches.

Now, make sure you're back on the branch you want to cherry-pick to. And use the git cherry-pick command again. Example:
git cherry-pick 9505ac61c924de0bba404f40f8abdc53af1909d8

Great. Now it should work!


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: Git Rename a Branch Name

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

Git : Rename / Change Branch name : How to Change the branch name of a local or a remote branch in git

There are multiple scenarios if you want to change the name of the branch. Sometimes you could have named a branch incorrectly, and sometimes it could be because you didn't follow the naming guidelines as per your team and organisation. Whatever be the cause, you very well need to know the method to do so.


The process is as follows:

1. First, rename your local branch.  (Sometimes you need to only do this. For example if the branch is yet not pushed on stash.)
If you are on the branch you want to rename:
Syntax: git branch -m new-name


2. In case you are on a different branch:
Syntax: git branch -m old-name new-name


3. Now suppose you want o delete the old remote branch and push the new-name local branch.
Syntax: git push origin :old-name new-name


Or, 4. You can also reset the upstream branch with the new branch local branch.
For this you need to switch to your new local branch and then:
Syntax: git push origin -u new-name