Skip to content

Latest commit

 

History

History
448 lines (325 loc) · 13.6 KB

File metadata and controls

448 lines (325 loc) · 13.6 KB

Git

Git is a popular Version Control Software (VCS) optimized for text files e.g. Software source code.

Git isn't good at handling binary files and it is considered best practice to avoid including big binary blobs like compiled binaries, firmware images, movies, and so on.

Quickstart guides

  • TOOD: git webguide(s).

Configuration

  • TODO: Mandatory config - username + email.
  • TODO: Identity config.
  • TODO: Keeping a global gitignore file in your homedir.
  • TODO: Rename default branch back to 'master'.
  • TODO: Moving config between machines.
  • TODO: Scripting git config CLI.
  • TODO: templating git config file format.

Scope

System: Systemwide for all users.

Global: For the system user.

Local: For the repo.

Example of file locations for different scopes

# Via: $ git --no-pager config --show-scope --show-origin --list
## SCOPE ORIGIN KEY=VALUE
system  file:C:/Program Files/Git/etc/gitconfig core.autocrlf=true
global  file:C:/Users/User/.gitconfig   core.autocrlf=false
local   file:.git/config        core.bare=false

Setting configuration

Disabling CRLF conversion (\n <-> \r\n)

git config --global core.autocrlf=false

repo-local configuration

Configuration specific to one git repository. Create a file named .gitattributes in the repository root dir.

Setting config values to repo-local scope:

## $ git config --local KEY=VALUE
git config --local core.autocrlf=false

Listing only repo-local settings:

git config --local --list

Listing config

Show active config values:

## KEY=VALUE
git --no-pager config --list

Show active config including scope:

## SCOPE ORIGIN KEY=VALUE
git --no-pager config --show-scope --list

Show active config including scope and origin:

## SCOPE ORIGIN KEY=VALUE
git --no-pager config --show-scope --show-origin --list

Branching

  • TODO: Guide on branching and at least one example viable development workflow using branching.

DESCRIPTION

TODO

Remotes

Listing remotes on git a repository:

git remote --verbose

To add a remote:

git remote add home-server "ssh://git@10.1.1.69:/~/my-project.git"

To push to a remote:

git push home-server

To remove a remote:

git remote remove home-server

Setup basic homserver remote

To init and push an exisitng repository to a home git server:

Create bare repo to push to: (On server)

## As: dev@home-server:~$
## Init bare repo on git server:
## Alternative?: # sudo -u "git" git init --bare "/home/git/my-project.git"
dev@home-server:~$ sudo -u git /usr/bin/bash
git@home-server:~$ git init --bare development-pointers.public.git

## Ensure client's pubkey is present:
dev@home-server:~$ sudo -u "git" nano "/home/git/.ssh/authorized_keys"

Add reference to remote and push repo data: (On client / workstation)

## As dev@workstation:~/repos/my-project$
## Add reference to remote to local git repository:
git remote add home-server "ssh://git@10.1.1.69:/~/my-project.git"

## Push repo to remote:
git push home-server

Creating bare repo

e.g. for lan server selfhosting

Init bare repo:

git init --bare "/home/git/my-project.git"

Example: sudo into git account to init bare repo on git server:

$ sudo -u "git" git init --bare "/home/git/my-project.git"

Add remote to reference server:

~/repos/my-project$ git remote add origin "ssh://git@10.1.1.69:/~/my-project.git"

Authentication

  • TODO: ssh auth
  • TODO: Env vars.
  • TODO: GNUPG integration.
  • TODO: Signing commits.
  • TODO: 2FA.

Basic git examples

Create a new empty repository in the current dir:

git init .
## git clone SOURCE_REPO [DEST]
git clone https://github.com/example/example.git

Pushing to default upstream:

git push

Pulling from default upstream (usually brings working files to latest upstream commit):

gut pull

Retrieving new changes from upstream without changing current working files:

git fetch

Stage file(s) for commit. (Record the state of the staged files in preparation for committing them.)

## $ git stage [FILE...]
git stage ./some-file

Commit staged changes: (Persist the state of the staged files to the repo.)

git commit -m "Some message from the commandline, as an example."

Netowrk issue mitigation

Sometimes git clone errors out from network troubles; how to fix that? To make git work even if network connection is unreliable.

Set git config file values to configure git's HTTP mode to tolerate up to a minute of being under 1KiB/sec: (untested)

## https://git-scm.com/docs/git-config/2.22.0#Documentation/git-config.txt-httplowSpeedLimithttplowSpeedTime
## Control when git gives up when using curl: (Overriden by env vars)
git config --global http.lowSpeedTime=60 # Seconds.
git config --global http.lowSpeedLimit=1024 # Bytes per second.

Use env vars to tell git's HTTP mode to tolerate up to a minute of being under 1KiB/sec: (untested)

## See: https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables
export GIT_CURL_VERBOSE="1" # Similar to 'curl -v'
export GIT_HTTP_LOW_SPEED_LIMIT="1024" # Bytes per second.
export GIT_HTTP_LOW_SPEED_TIME="60" # Seconds.

## After env vars are set git should tolerate low transfer rates.
git clone https://REPO_URL

Pushing to multiple remotes

How to use one command to push to multiple remotes. e.g. simultaneously push to homeserver, github, gitgud, etc.

  • TODO: Figure this out.

gitlab suggested push:

git remote rename origin old-origin
git remote add origin git@ssh.gitgud.io:Ctrl-S/development-pointers.git
git push --set-upstream origin --all
git push --set-upstream origin --tags

git remote add github git@github.com:woodenphone/development-pointers.git

Windows ssh push

First connect directly to host via putty to accept the host key, then retry git operation.

Message given:

The server's host key is not cached. You have no guarantee
that the server is the computer you think it is.
The server's ssh-ed25519 key fingerprint is:
ssh-ed25519 255 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n, Return cancels connection, i for more info)

Push to multiple remotes at once

Push to all remotes with one command. Use a special remote that points at all remote dests, both public and privately hosted ones.

git remote add 

Examples

Methods to do the task I found.

Using a special remote with multiple URLs:

git remote add all git://original/repo.git
git remote -v ## List remotes.
git config -l | grep '^remote\.all' ## Show config for 'remote.all'
git remote set-url --add --push all git://another/repo.git
git remote -v  ## List remotes.
git config -l | grep '^remote\.all' ## Show config for 'remote.all'
git remote set-url --add --push all git://original/repo.git

Useing a git alias:

git config alias.pushall '!git push origin devel && git push github devel'

setting up multiple push destinations via editing .git/config

This method I have tested.

Excerpt of the contents of .git/config

[remote]
	## Override default to push to all remotes
	## i.e. Make '$ git push' behave like '$ git push pushall'
	## remote.pushDefault "The remote to push to by default" - git-config(1) manpage
	pushDefault = pushall
[remote "pushall"]
	## Convenience pseudoremote to push to all remotes at once
	## remote.<name>.url "first is used for fetching, and all are used for pushing" - git-config(1) manpage
	# url = git://original/repo.git
	## remote.<name>.pushurl - git-config(1) manpage
	pushurl = git@github.com:woodenphone/tmux-dump-session.git
	pushurl = git@ssh.gitgud.io:Ctrl-S/tmux-dump-session.git
	pushurl = ssh://git@PERSONAL_GIT_SERVER_IP_REDACTED:/~/tmux-dump-session.git

To push to all remotes (explicitly referencing 'pushall' remote):

git push pushall -v

(-v increases verbosity)

To push to all remotes (implicitly due to remote.pushDefault):

git push -v

Links

Links related to pushing to multiple remotes in one command.


Useful informational commands for troubleshooting

Show the configuration specific to this repo:

git config --list --local ## Show configuration for this repo (local scope).

Show a list of remotes:

git remote --verbose ## Show remotes for this repo.

How to figure out where config values are coming from.

$ git config --list --show-scope --show-origin ## Show currently active configuration, explicitly listing scope and where the value came from.
>local   file:.git/config        core.repositoryformatversion=0

Related utils

Creating git repos from existing codebase history:

Files

Relevant files included in this repo.

Links

*"" *""

Official docs for git

*"" *"" *"" *""

Manpages for git

*"git(1)" - "git - the stupid content tracker" *"" *""

Guides for git

*"" *"" *""

Unsorted links

*"" *"CRLF vs. LF: Normalizing Line Endings in Git"

https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories