• Menu
  • Skip to right header navigation
  • Skip to content
  • Skip to footer

LEVELPRIME

Make technologies

Header Right

  • English
  • Home
  • Servizi
  • Blog
  • Home
  • Servizi
  • Blog

Header Right

  • English

git

Multiple github accounts on same machine (windows os) without using SSH keys

20 novembre, 2018 By //  by Filippo Iovine

Having some problems to set multiple github accounts on my windows machine and with the target to not use any SSH approach, I chose to play with GIT credential helpers to fit the best one for my needs.

Final solution, in my case, was to set GIT using git-credential-store helper: a credential helper that stores GIT credentials locally on disk.

Be aware that probably a credential helper is already in place in your GIT environment and probably that is GIT Credential Manager (GCM). That’s because GCM is installed alongside GIT for Windows that updates GIT system config file accordingly (credential.helper=manager is added into the GIT system config file, usually located in \Program Files\Git\mingw64\etc\gitconfig).

 

Clean GIT config file from previous helpers

Open a dos CLI as administrator and unset helper from GIT system config:

git config --system --unset credential.helper

 

Set new credential.helper=store into global config file:

git config --global credential.helper store

 

Finalize GIT to handle multiple github accounts.

To complete the job, you need to set remote url to https like below or edit local git config file (.git/config) looking for [remote … ] sections and add missing USERNAME@:

git remote set-url origin https://USERNAME@github.com/USERNAME/REPO.git

Now push to force git asking for credentials:

git push

 

 

Cons

With the store credential helper, GIT stores credentials in the GIT credential file (usually located in %HOME%\.git-credentials) in a unencrypted format (password included).

So be careful with that and protect credential file at least by filesystem permissions.

 

Why can we not use GCM to handle multiple github accounts?

GCM saves GIT accounts (encrypted) credentials into Windows Credential (WC) wallet database.

When GCM is set and GIT requests credentials:

  1. GCM is invoked, a PAT (Personal Access Token) is requested to github and saved remotely in the Settings/Developer settings/Personal access tokens section on your github account.
    Locally PAT account data is stored in your WC wallet as PersonalAccessToken/[PASSWORD] user with URI key internet/network address=git:https://github.com.
  2. In case 1) won’t be performed (or interrupted) then GIT command line asks for credentials (USER NAME and PASSWORD) to locally saves those in your WC wallet as [USER]/[PASSWORD] account with URI key internet/network address=git:https://github.com.

Performing same operation with another github account, GCM stores account info using again same URI internet/network address=git:https://github.com overwriting previous one (WC can save one account per URI).

Finally, GCM does not support multiple users per Uri (as reported in Microsoft/Git-Credential-Manager-for-Windows:issue 363).

Filed Under: git, windows Tagged With: credentials, git, multiple accounts, windows

How to move files/directories from one git repository into another preserving history

24 ottobre, 2018 By //  by Roberto Fichera

When working with Git repositories, sometimes become necessary to move files/directories from one repository into a new one. This process isn’t simple as you might think especially if you want to preserve the history! So how to do that?

First of all you have to clone your repository into a temporary folder, with something like:

git clone -b <branch> <your-git-url> <new-git-url>

Now the black-magic:

cd <new-git-url>
git filter-branch --tree-filter 'rm -rf `ls|/usr/bin/grep -v <directory_or_filename_to_keep>`' --prune-empty HEAD

After entering in the directory <new-git-url>, you have basically to run the git filter-branch command applying the –tree-filter option for rewriting the tree and its content. The argument is evaluated as shell command in the working directory. So we are going to apply the command

rm -rf `ls|/usr/bin/grep -v <directory_or_filename_to_keep>`

which recursively remove all files and directory coming from the result of listing the content of the current working directory, filtered by grep. The grep -v switch will invert the pattern match, so we want a match all files and directories NOT matching <directory_or_filename_to_keep>. The execution of the command might take a while, but at the end you will get a clean repo having only the <directory_or_filename_to_keep> you want including its related history you can check with

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

Finally we have to do our last operations,  cleanup unnecessary files and optimize the local repository

git gc --aggressive
git prune
git clean -df

setup the new remote

git remote set-url origin <new-git-url>
git push --force

and we are done!

If we want to go further with more complicated pattern matches fitting our repository content, we could use the grep power but don’t forget to apply -v for the inverse matching otherwise you get the contrary ;-)!

Filed Under: git Tagged With: branch, filter-branch, git, tree-filter

Footer

Pagine

  • Home
  • Servizi
  • Blog

Contatti

  • Sede Roma: +39 06 66178031
  • Sede Oristano: +39 0783 71339
  • Sede Londra (Regno Unito): +44 (0)208 144 9929
  • Sede Anversa (Belgio): +32 3 633 1196
  • info@levelprime.com

Policies

  • Privacy Policy
  • Cookie Policy

Copyright © 2023 @ Level Prime srl