Git: Difference between revisions

From Wiki
Jump to navigation Jump to search
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Cheat Sheets ==
https://about.gitlab.com/images/press/git-cheat-sheet.pdf
https://github.com/kenmueller/gitignore
== Initial Setup ==
== Initial Setup ==
<pre>
<pre>
Line 6: Line 11:
</pre>
</pre>
May need to set up SSH-key authentication for gitlab.
May need to set up SSH-key authentication for gitlab.
== Basics ==
<pre>
git init                  # create git repo inside current directory
git add .                # add all changes to staging
git commit                # commit staged changes to master branch
git commit --amend        # redo/fix the commit you just did
</pre>
== Branches ==
<pre>
git branch                # list branches (master is default, HEAD/* represents current)
git branch mybranch      # create a new branch
git switch mybranch      # switch to new branch
git checkout mybranch    # switch to new branch
git switch -c mybranch    # create new branch and switch to it
git checkout -b mybranch  # create new branch and switch to it
git branch -d mybranch    # delete branch
</pre>
== Merging Branches ==
Example: To merge the <code>bugfix</code> branch back into the <code>master</code> branch, switch to the master branch and then type
<pre>
git merge bugfix
</pre>
Three possible merge situations
# Fast-forward: <code>master</code> hasn't changed, only <code>bugfix</code> has
# No-conflict merge: both have changed, but not in a way that conflicts
# Conflict merge: the same line of some file has changed in different ways in the two branches.
Conflict text will appear in the affected file(s).  Edit this as appropriate, then commit the change to master.
NOTE: the bugfix branch will still be different.


== Clone Repository ==
== Clone Repository ==
Line 29: Line 65:
<pre>
<pre>
git fetch --all
git fetch --all
git reset --hard origin/master
git show-branch  # to see whether branch is "master" or "main"
git reset --hard origin/master   # (or "origin/main" as above)
</pre>
 
== Overwrite a single file from repository ==
<pre>
git checkout HEAD~ <filename>
# or just write out a specific version for comparison
git show 7f7a8214befd1903260e6c6ae65d9158e4249881:rails/public/faq.html > faq.html.old
</pre>
</pre>


== Cheat Sheets ==
== Git Server ==
https://about.gitlab.com/images/press/git-cheat-sheet.pdf
https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server
<pre>
/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
</pre>
<pre>
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:::
</pre>
<pre>
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
</pre>
<pre>
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
</pre>
<pre>
root@perth:/opt/git-server/keys/martin# cat adelaide
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDbyBYOnAoKLyNvittK+xyEt01NPnPC2XUFKz4bHeolz martin@adelaide
</pre>


https://github.com/kenmueller/gitignore
=== Initialize repository ===
As root on git server:
<pre>
cd ~git
mkdir project.git
cd project.git
git init --bare
cd ..
chown -R git.git project.git
</pre>
As developer on developer PC:
<pre>
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
</pre>
As another user:
<pre>
git clone git@irc-git-host:project.git
</pre>

Latest revision as of 22:50, 25 April 2025

Cheat Sheets

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

https://github.com/kenmueller/gitignore

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.

Basics

git init                  # create git repo inside current directory
git add .                 # add all changes to staging
git commit                # commit staged changes to master branch
git commit --amend        # redo/fix the commit you just did

Branches

git branch                # list branches (master is default, HEAD/* represents current)
git branch mybranch       # create a new branch
git switch mybranch       # switch to new branch
git checkout mybranch     # switch to new branch
git switch -c mybranch    # create new branch and switch to it
git checkout -b mybranch  # create new branch and switch to it
git branch -d mybranch    # delete branch

Merging Branches

Example: To merge the bugfix branch back into the master branch, switch to the master branch and then type

git merge bugfix

Three possible merge situations

  1. Fast-forward: master hasn't changed, only bugfix has
  2. No-conflict merge: both have changed, but not in a way that conflicts
  3. Conflict merge: the same line of some file has changed in different ways in the two branches.

Conflict text will appear in the affected file(s). Edit this as appropriate, then commit the change to master. NOTE: the bugfix branch will still be different.

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 show-branch   # to see whether branch is "master" or "main"
git reset --hard origin/master   # (or "origin/main" as above)

Overwrite a single file from repository

git checkout HEAD~ <filename>
# or just write out a specific version for comparison
git show 7f7a8214befd1903260e6c6ae65d9158e4249881:rails/public/faq.html > faq.html.old

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