Fixing Git: refusing to merge unrelated histories

I have a new project and I add git repo later on after the project finished.
After setup remote repo to my project, I got this kind of error when pulling data from the repository.

fatal: refusing to merge unrelated histories

The problem fixed by adding this argument to git command “–allow-unrelated-histories”.

$ git pull upstream master --allow-unrelated-histories



Git: Undoing Rebase

In case you accidentally had some fault when rebasing your commit, run this command below to undo your rebase to recent commit before the rebase.

$ git reset --hard ORIG_HEAD


OSX: Upgrade Preinstalled Git on OSX

Git just announced latest version git 2.10 with several improvement. Here is how to upgrade preinstalled git on OSX to latest version.

$ git --version # git version 2.7.4 (Apple Git-66)
$ brew update
$ brew install git

Then modify your PATH on .bashrc or .zshrc, and put this line on the beginning of your path.

export PATH="/usr/local/bin:..another config..:"  # Separate each config with ":" colon

Last step what you need to do just restart your terminal.

$ source ~/.zshrc # I'm using ZSH
$ git --version   # git version 2.10.0


Git: Fix warning: unable to access ‘/.config/git/ignore’: Permission denied

I just cloned my repository from my github on my local machine and I don’t know why I got this error when trying to see my project changes using “git status”.

warning: unable to access '/Users/my_user/.config/git/ignore': Permission denied
On branch dev
Your branch is up-to-date with 'origin/dev'.
nothing to commit, working directory clean

After I check the folder, I just know that the folder owned by “root” user.

$ ls -lah
drwx------   3 root           staff   102B Aug  1 21:26 .config

I fixed my issue by changing the owner to my user on my laptop.

$ sudo chown my_user .config

Undo Last Published Commit in GIT

Today I’m refactoring my old code in my project and unexpectedly my refactor code messed up my apps. So I have to go back to my previous commit.

I know we can simply use.

$ git reset --hard your_commit_hash

But I couldn’t use that, because my previous commit already pushed to my repository.
Here is how to fix that using revert.

$ git log --oneline -5

a8031a3 Refactor tab host to swipable tabs for history
32d5856 Polishing order history progress # I need to go back here
3a984b0 Remove obsolete order handler
2b09868 Init order history
8102df9 Create order history pojo

$ git revert a8031a3 # commit hash that you need to remove.

Then it will remove my refactor commit and create new commit for revert.

c0cd60f Revert "Refactor tab host to swipable tabs for history" # new commit
a8031a3 Refactor tab host to swipable tabs for history
32d5856 Polishing order history progress
3a984b0 Remove obsolete order handler
2b09868 Init order history

Last step, what you need todo just push your commit. 😀

Thanks to Rezha 😀

Fix Git “Agent admitted failure to sign”

Don’t know why I got this error when I pull from my project, after added a new ssh key on my github account.

Agent admitted failure to sign using the key.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

To solve that error follow this step, enter on your terminal.

$ eval "$(ssh-agent -s)"
$ ssh-add
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/my_other_key