Skip to the content.

Common Git Aliases

https://adtc.github.io/

Git CLI Setup

git user config

git config --global user.name # get current value of user.name config
git config --global user.email # get current value of user.email config
git config --global user.name "Mona Lisa" # set value of user.name config
git config --global user.email "YOUR_EMAIL" # set current value of user.email config
git config --global --list # list config

Note about Github emails: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/setting-your-commit-email-address

Git Credential Stuff

https://git-scm.com/doc/credential-helpers - list known helpers. In general try to use Git Credential Manager for any https access to repos.

Git Credential Storage

https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

Git Credential Manager

Replacement (Better?) than built in Git Credential Storage

Install: https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/install.md

Git for Windows

If using windows, its usually easier to just install Git for Windows which installs GCM

CLI:

git credential-manager github # review commands for github
git credential-manager github list
git credential-manager github login # has suboptions
git credential-manager github logout <account>
git credential-manager configure # make sure GCM is set as cred manager
git credential-manager unconfigure
git credential-manager get

More CLI Options: https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/usage.md
Configuration: https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/configuration.md
Multiple Users: https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/multiple-users.md

You can tell GCM to remember which user to for a specific repo:

git config --global credential.<URL>.username <USERNAME>

Docs Index: https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/README.md

Github Device flow

Useful for when login isn’t associated with default browser

  1. git credential-manager github login –device –username desired username
  2. Get code
  3. Go to http://github.com/login/device
  4. Enter code

autocrlf

git config core.autocrlf: set up CRLF settings. Can be set per repo or globally.

In theory it should be set to automatic during install but you may need to adjust.

More Info:

Git with Windows

This matters a lot work working with git on windows and linux machines. I don’t know if things need to be changed from their defaults but an understanding should be sought.

You can also look into --renormalize Option in git. I haven’t, but here is a suggested answer: https://stackoverflow.com/questions/7156694/git-how-to-renormalize-line-endings-in-all-files-in-all-revisions

Set your Editor

Some settings also need to be configured in your editor. Look into how you can set it. See files.eol under Settings in VSCode Settings and Extensions

There may be some value in looking up these settings:

Quick Github Auth

Create secure correctly scoped github personal access token here: https://github.com/settings/personal-access-tokens

Then type: git clone https://<username>:<PAT>@github.com/<owner>/<repository>.git

Copy a single file from one branch to another

From https://betterstack.com/community/questions/how-do-i-copy-version-of-single-file-from-one-branch-another/

git checkout target-branch
git checkout source-branch -- path/to/file
git add path/to/file
git commit -m "Copy file from source-branch to target-branch"
git push origin target-branch

Restore/Rehydrate/Reinflate a repo with just the .git folder

Useful for browsing/using etckeeper backups or when something goes wrong and you just have the .git folder.

On a machine with Git installed:

  1. Create a copy folder somewhere outside of the repo, download folder, that is clean, temporary but safe
  2. Copy into that new folder the .git folder. If it is named something like MyRepoName.git, rename the folder to just .git once copied
  3. open up command prompt/terminal, etc to the folder created in step 1
  4. Type git init
  5. Type git reset --hard branchname but change branchname to master, main or whatever branch you want to get back to

Special Git Repo files & folders

.gitignore

https://git-scm.com/docs/gitignore

.gitattributes

.gitmodules

For Git submodules.

.github

https://stackoverflow.com/a/61301254/5435742

TBC

Internet References

https://www.reddit.com/r/git/comments/1htmt9k/the_top_1120_commands_you_need_to_recover_from/
https://ohshitgit.com/