Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active June 4, 2024 03:08
Show Gist options
  • Save wilmoore/32f4caaeca733a94447418aade624805 to your computer and use it in GitHub Desktop.
Save wilmoore/32f4caaeca733a94447418aade624805 to your computer and use it in GitHub Desktop.
Software Engineering :: Source Control :: VCS :: Git

Software Engineering :: Source Control :: VCS :: Git

⪼ Made with 💜 by Polyglot.

related

examples

git log --stat

Undo

Remotes

https://stackoverflow.com/a/14290145

> git remote set-url --add --push origin git://original/repo.git
> git remote set-url --add --push origin git://another/repo.git

Config

If you're using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

SSH Agent

$ eval "$(ssh-agent -s)"
> Agent pid 59566

Identities

Setup
Environment Variable Based

GIT_SSH_COMMAND

env GIT_SSH_COMMAND='ssh -i ~/.ssh/repo.pem' git clone [email protected]:ORG/repo.git

Branching

> git fetch && git checkout feature/...

Credential Helpers

Retrieve and store user credentials

> git credential <fill|approve|reject>

git config

[credential "https://gist.github.com"]
  helper              = 12-factor GIST_GITHUB_API_TOKEN
  username            = token

git-credential-12-factor(1)

#!/bin/sh
logger -is -p local3.info -t $0 "(\$1) $1 [env] (\$2) $2 [action]"

# respond only to `get` action (this environment-based helper is read-only)
if [ "$2" = "get" ]; then
  echo "password=${!1}"
  exit 0
else
  logger -is -p local3.info -t $0 "action not supported ($2)"
  exit 1
fi

NOTE: open Console.app to view logs and search for git-credential-gist

Custom Credential Helpers

Credential helpers are programs executed by Git to fetch or save credentials from and to long-term storage (ideally, encrypted at rest).

hacks that work, but I won't use

git-credential-store - Helper to store credentials on disk

git config credential.helper 'store [<options>]'
git \
  -c credential.helper="!f(){ printf 'username=%s\npassword=%s\n' "$USERNAME" "$PASSWORD" };f" \
  push origin master

AWS Code Commit

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true

Git Credential Manager for Windows (GCM)

The Git Credential Manager for Windows (GCM) is a credential helper that stores credentials in the Windows Credential Manager.

Git HTTPS authentication with the GCM

  1. git needs to work with a remote host over the HTTPS protocol and invokes git-remote-https.
  2. git-remote-https negotiates with the host.
  3. The host rejects git-remote-https due to lack of credentials.
  4. git-remote-https fails with a reason code linked to credentials.
  5. git invokes git-credential in hopes of acquiring useful credentials.
  6. git-credential scans Git's configuration to see if any helpers are registered.
  7. git-credential invokes the helpers one at a time in the order listed in hopes of one having useful credentials for the + values.
  8. git-credential finds that credential.helper=manager and invokes git-credential-manager with the "get" option.
  9. git-credential-manaer lacks credentials for the remote.
  10. git-credential-manager looks at the configuration to determine if these are basic credentials, Visual Studio Team Services, or GitHub; if the request is multi-factor authentication; etc.
  11. In the case of basic credentials, git-credential-manager tells git-credential the truth that it does not have any credentials for it.
  12. git-credential then prompts the user at the command line for credentials.
  13. The user enters credentials.
  14. git-credential invokes git-credential-manager with the "store" option and supplies the credentials for storage.

git.io

Git Hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment