git client usage


shallow clone

root@n1:/usr # git clone -o freebsd -b main --depth 1 https://github.com/freebsd/freebsd-src.git src

root@n1:/usr # git clone -o freebsd -b stable/14 --depth 1 https://github.com/freebsd/freebsd-src.git src

config user

% git config user.email "your_email@abc.example"
% git config user.name "your_username"

find remote URL

% git remote show origin

add/change, commit and push

% git add bash_scripts/mnt_nfs.bash
% git commit -m "Initial commit"
% git push -u origin master

cherry pick a commit

% git cherry-pick -x commit_xxx --edit

create/apply a patch

## single patch with two commits
% git format-patch HEAD~2..HEAD --stdout > two_commits.patch

## single patch with one commit
% git format-patch -1 commit_hash --stdout > one_commit.patch

cc@s1:/usr/src % sudo git apply --stat ~/patch.diff 
 sys/conf/options      |    3 +
 sys/netinet/ip_icmp.c |  142 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 145 insertions(+)

cc@s1:/usr/src % sudo git apply --check ~/patch.diff 
cc@s1:/usr/src % sudo git reset --hard stable/14
cc@s1:/usr/src % sudo git am --abort
cc@s1:/usr/src % sudo git apply ~/patch.diff 

create a tar ball

## the branch name `main` can be replaced with a specific commit or branch/tag
git archive --format=tgz -o /proj/src.tgz --prefix=src/ main

get more code context

# git diff -U10
# git log -p -U10

git log history of a specific user

git log --author="Cheng Cui"

git server usage

initiate a bare repository

$ git init --bare siftr2.git

push to server

% cd myproject
% git remote add origin cc@server:/git/myproject.git
## or change the current `origin` to a new server
% git remote set-url origin cc@server:/git/myproject.git
% git push -u origin main
## optional: push every branch that exists locally
git config --global push.default matching
% git push --all
% git push --tags

clone from server

% git clone cc@server:/git/myproject.git

chengcui/git (last edited 2025-12-02T14:14:22+0000 by chengcui)