Git

2013/03/06 Linux

License: (CC 3.0) BY-NC-SA

YOU SHOULD READ PROGIT IF YOU WANT TO BE PROFESSIONAL

reference

client

set default commit editor:

git config --global user.name "Zhiqiang Fan"
git config --global user.email "aji.zqfan@gmail.com"
git config --global core.editor vim
git config --global color.ui auto
git config --global core.excludesfile '~/.gitignore'
git config --global credential.helper store
# for security, cache for 30 days
git config --global credential.helper 'cache --timeout=2592000'
# for chinese character issue
git config --global core.quotepath false

create new branch from a branch and switch to it:

git checkout -b new-branch origin/master

rename branch:

git branch -m oldname newname

update branch

git branch --set-upstream master origin/master
git checkout master
git pull --ff-only

merge branch:

git checkout master
git merge new-branch
git branch -d new-branch

if new-branch is not merged yet, git branch -d will not success, you can use git branch -D to force deletion.

delete remote branch:

git push origin :branch-name

delete local ref of remote branch

git branch -d -r origin/branch-name

git log:

git log
git log --pretty=oneline
git log --pretty=format:"%h %s" --graph

git patch:

make patches between two commit [new, old), each commit has a seperate patch

git format-patch -M -C --output-directory /tmp/ hash_begin...hash_end

make a total patch compared to master branch, this assume you’re on the branch ahead of your master:

git format-patch master --stdout > /tmp/your-patch-name.patch

Note, master can be replaced by any branch and tag. Apply patches:

git apply --stat xxx.patch
git apply --check xxx.patch
git am --signoff < xxx.patch

If your git am is failed, you cannot do it again, an error will generate: previous rebase directory /path/to/your/.git/rebase-apply still exists but mbox given. Use git am --abort to cancel last am.

referece:

git tag

  • git tag v0.1 directly tag
  • git tag -a v0.1 -m 'v0.1' using a message with annotation
  • git push origin v0.1 push tag v0.1 to repo
  • git push origin --tags push all tags to repo
  • git tag -d v0.1
  • git push origin :refs/tags/v0.1
  • git show-ref --tags prints commit sha of tags
  • git push origin --delete $(git tag -l "v3.0.*") deletes all tags with certain pattern in remote repo

revert deleted file

  • git checkout commit_id – path_to_file

在开发packer项目时,遇到个很奇怪的现象。直接git clone项目到本地,git status就能看到有变更的文件。无论是使用dos2unix工具尝试改变文件格式,还是尝试删除文件,然后用git checkout恢复,都无法改变状态。之后通过这篇问答https://stackoverflow.com/questions/1575682/cant-seem-to-discard-changes-in-git才能切实解决:

# Completely remove the autocrlf & safecrlf settings from ~/.gitconfig
# Completely remove the autocrlf & safecrlf settings from your repo's local config ./.git/config
git rm --cached -r .
git reset --hard

packer项目专门有一个提交解决这个问题https://github.com/hashicorp/packer/commit/3e73abc93aeffca39a71bbab7496a98fb27316cf。根源还是unix和dos的格式问题。

roll back

  • git reflog to get brief message
  • git log -g to get detailed message
  • git reset --hard HEAD~1 revert to last commit
  • git reset --hard HEAD@{1} revert to last ref

urlencode your password

password is insecure, ssh key is recomanded, but if have to and password contains special characters, it should be urlencoded when in git clone.

!   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

server

github

  1. pre-receive hook: https://docs.github.com/en/enterprise/2.19/admin/policies/creating-a-pre-receive-hook-script

local server

blueprint

[X] 1. read & write for all team member
[-] 2. write protect

blueprint 1

thanks to Elton’s blog

generate your ssh key

# ssh-keygen

mail your ~/.ssh/id_rsa.pub to your git server admin or just scp to the git server if you’re the admin

on the git server, to the following command:

# sudo apt-get install git-core
# sudo adduser git
# su - git
# mdkir .ssh
# cat /path/to/member's/id_rsa.pub >> ~/.ssh/authorized_keys

you can make a git repo now, for instance:

# mkdir test.git
# cd test.git
# git --bare init

you should disable git user for shell login, otherwise, people can log into your server via git user.

# sudo vim /etc/passwd
git:x:1000:1000::/home/git:/usr/bin/git-shell

that it’s, enjoy yourself.

Search

    Table of Contents