git client usage
Contents
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
get tag list
% git tag -n siftr2.2 this is the last commit for siftr version 2.2 siftr2.3 this is the last commit for siftr version 2.3 before adding binary siftr2.4 The last commit for siftr2.4 before adding IPv6 support
add/change, commit and push
% git add bash_scripts/mnt_nfs.bash % git commit -m "Initial commit" % git push -u origin master
remove two local commits (before they are pushed)
% git reset --hard HEAD~2
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
create a bundle file and apply it
git bundle create bundle.git --all // create a bundle file from a git repository git config --global init.defaultBranch main mkdir src cd src/ git init git remote add bundle-remote ../bundle.git // apply the bundle file to the new 'src' repository git pull bundle-remote git init && git remote add bundle-remote ../bundle.git && git pull bundle-remote
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