Git

From Wiki
Jump to navigation Jump to search

Initial Setup

git config --global user.name "Harry Truman"
git config --global user.email "harry.truman@example.com"
git config --global --list

May need to set up SSH-key authentication for gitlab.

Clone Repository

git clone git@gitlab.com:scottcamacmartin/my_project.git

Upload Existing Code Directory as Gitlab Repository

First, creat a new project on gitlab. Then, on the local dev machine:

git clone git@gitlab.com:scottcamacmartin/my_project.git
cd my_project
# then copy code into new folder my_project
# DELETE ANY SENSITIVE PASSWORDS, ETC
# also set up .gitignore file
git status
git add .
git commit -m "initial commit"
git push -u origin master

Overwrite local from repository

git fetch --all
git reset --hard origin/master

Overwrite a single file from repository

git checkout HEAD~ <filename>

Cheat Sheets

https://about.gitlab.com/images/press/git-cheat-sheet.pdf

https://github.com/kenmueller/gitignore


Git Server

https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server

/opt/git-server/bin         # refresh-auth command
/opt/git-server/keys        # ssh keys
/opt/git-server/repos       # home directory with repos
/opt/git-server/repos/.ssh  # authorized_users file
root@perth:/etc# grep git /etc/passwd
git:x:222:222:Git Service User:/opt/git-server/repos:/usr/bin/git-shell
root@perth:/etc# grep git /etc/group
git:x:222:
root@perth:/etc# grep git /etc/shadow
git:*:17767:0:99999:7:::
root@perth:/opt/git-server/bin# cat refresh-auth 
#!/bin/bash

GIT_SERVER_DIR=/opt/git-server
KEY_DIR=$GIT_SERVER_DIR/keys
SSH_KEY_FILE=$GIT_SERVER_DIR/repos/.ssh/authorized_keys
SSH_KEY_PREFIX="no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding"

find $KEY_DIR -type f -exec grep -v '#' {} \; | awk '$1' | sort -u | sed -e "s/^/${SSH_KEY_PREFIX} /" > $SSH_KEY_FILE
root@perth:/opt/git-server/keys# ls -l
total 16
drwxr-xr-x 2 root root 4096 Nov 28 16:28 dc
drwxr-xr-x 2 root root 4096 Nov 30 10:18 martin
drwxr-xr-x 2 root root 4096 Nov 28 16:28 root
drwxr-xr-x 2 root root 4096 Nov 28 16:28 statsuser
root@perth:/opt/git-server/keys/martin# cat adelaide 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDbyBYOnAoKLyNvittK+xyEt01NPnPC2XUFKz4bHeolz martin@adelaide

Initialize repository

As root on git server:

cd ~git
mkdir project.git
cd project.git
git init --bare
cd ..
chown -R git.git project.git

As developer on developer PC:

cd project
emacs .gitignore  # include a line with "**/.hg" to exclude .hg content
git init
git add .
git commit -m 'Initial commit'
git remote add origin git@irc-git-host:project.git
git push origin master

As another user:

git clone git@irc-git-host:project.git