顯示具有 GIT 標籤的文章。 顯示所有文章
顯示具有 GIT 標籤的文章。 顯示所有文章

2019年3月6日 星期三

“error: branch '.*' not found when removing a branch is attempted.

find .git//refs -name -delete

In case of it is repository hosted by repo,
find .repo//refs -name -delete

2018年3月31日 星期六

Linux 使用 Gitolite 架設 Git Server

原文 https://blog.longwin.com.tw/2011/03/linux-gitolite-git-server-2011/

單純使用 SSH 架設可見此篇: Linux 架設使用 SSH 共享存取的 Git Server
想要控管 User / Project 權限, 而且還想要控管 branch / tag 等讀寫權限, 則需要靠 Gitolite 等套件來協助.
  • gitolite - SSH-based gatekeeper for git repositories
此篇主要寫的是 Gitolite 架設, 若之前已經有依照上述文章架設共享存取的 Git Server, 而 Gitolite 也想用 git 的帳號來管理, 則需做下述動作先改回原始設定.
註: 此文會用 gitolite 的帳號來管理, 不會用 git 帳號, 所以不需要做下述更改的動作
  1. vim /etc/passwd
    git:x:1000:1000::/home/git:/usr/bin/git-shell
    改回
    git:x:1000:1000::/home/git:/bin/sh
  2. mv /home/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys.bak

相關資料準備

  • 系統: Debian / Ubuntu Linux
  • Server: example.com
  • Project name: project_name
  • Gitosis (Git) Repository 位置: /var/lib/gitolite/repositories # Debian / Ubuntu Linux 套件預設位置
  • Group name: myteam

系統套件安裝

  • apt-get install gitolite git-core
  • 安裝完成會出現此訊息: No adminkey given - not initializing gitolite in /var/lib/gitolite.
  • 相關設定參考可見 /etc/gitolite/example.conf, /etc/gitolite/example.gitolite.rc

產生 SSH 公鑰

  1. ssh-keygen -t rsa # 產生 id_rsa, id_rsa.pub
  2. mv id_rsa ~/.ssh/ # 將 id_rsa 放在 ~/.ssh/ 內.
  3. scp id_rsa.pub example.com:/tmp/user1.pub # 將 id_rsa.pub 丟到 Server 上, 大家的 public key 都需要傳到 Server 上.
  4. scp id_rsa.pub example.com:/tmp/admin.pub # 管理者的 key 同 user key, 在此設為 admin.pub, 避免下述內容造成混淆.

Gitolite Server 架設

  1. ssh example.com # Git Server
  2. sudo su - gitolite
  3. gl-setup /tmp/admin.pub # 匯入管理者的 Public key. 注意: 此檔名即是帳號名稱, 不要使用 id_rsa.pub 當檔名, 建議用 "帳號.pub" 當檔名
  4. exit

Gitolite Server 設定專案、新增帳號

  1. Gitolite 的專案權限 / 帳號管理 是使用 Git 來管理, 專案名稱: gitolite-admin.git
  2. git clone gitolite@example.com:gitolite-admin # 因為 Gitolite 是用 gitolite-admin.git 來管理, 所以需要抓下來修改、設定(未來所有管理也是如此)
  3. cd gitolite-admin # 會看到下述
    • conf/gitolite.conf # 設定檔, 設定誰可以讀寫哪個專案的 Repository
    • keydir # 目錄, 放每個帳號的 public key. 放置的檔案命名: user1.pub, user2.pub (user1, user2.. 為帳號名稱(檔名 = 帳號), 建議使用 "帳號.pub" 當檔名)

設定專案權限

  1. cd gitolite-admin
  2. vim conf/gitolite.conf # 會看到下述, 不要動他, 於最下方設定自己的 Group / 專案名稱即可.
    repo    gitolite-admin
    RW+     =   admin
    repo    testing
    RW+     =   @all
  3. 由此檔案 新增 / 修改後, commit + push 即可.

建立專案

  1. git clone gitolite@example.com:testing # 對應 gitolite.conf 的 repo testing, 會出現下述訊息
    Cloning into testing...
    warning: You appear to have cloned an empty repository.
  2. cd testing
  3. touch readme
  4. git add .
  5. git commit -m 'add readme'
  6. git push origin master

新增帳號

  1. cd gitolite-admin
  2. cp /tmp/user1.pub keydir/user1.pub # 請依照實際帳號命名, 不要取 user1, user2
  3. cp /tmp/user1.pub keydir/user1@machine.pub # 若相同帳號, 則使用 user@machine.pub
  4. cp /tmp/user2.pub keydir/user2.pub
  5. git add keydir/user1.pub keydir/user1@machine.pub keydir/user2.pub
  6. git commit -m 'add user1, user1@machine, user2 public key'
  7. git push

gitolite.conf 更多設定條件

下述摘錄自: Gitolite 構建 Git 服務器 - 授權使用者建立屬於自己的空間 (User 下面可以建 N 個 Repository), 在此就不記載, 請自行詳見: 此文的 章節 2.4.3
# 取自 2.3.1 授權文件基本語法
@admin = jiangxin wangsheng
repo gitolite-admin
RW+    = jiangxin
repo ossxp/.+
C       = @admin
RW     = @all
repo testing
RW+                   =   @admin
RW      master        =   junio
RW+     pu            =   junio
RW      cogito$        =   pasky
RW      bw/           =   linus
-                        =   somebody
RW      tmp/           =   @all
RW      refs/tags/v[0-9] =   junio
# 取自 2.3.3 ACL
repo testing
RW+   = jiangxin @admin
RW    = @dev @test
R      = @all

gitolite.conf 語法說明

repo 語法
  • repo 語法: <權限> [零個或多個正規表示式批配的引用] = [ ...]
  • 每條指令必須指定一個權限, 權限可以用下面任何一個權限的關鍵字: C, R, RW, RW+, RWC, RW+C, RWD, RW+D, RWCD, RW+CD
    • C : 建立
    • R : 讀取
    • RW : 讀取 + 寫入
    • RW+ : 讀取 + 寫入 + 對 rewind 的 commit 做強制 Push
    • RWC : 授權指令定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+C : 同上, C 是允許建立 和 regex 配對的引用 (branch、tag 等)
    • RWD : 授權指令中定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+D : 同上, D 是允許刪除 和 regex 配對的引用 (branch、tag 等)
    • RWCD : 授權指令中定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+CD : C 是允許建立 和 regex 配對的引用 (branch、tag 等), D 是允許刪除 和 regex 配對的引用 (branch、tag 等)
    • - : 此設定為不能寫入, 但是可以讀取
    • 註: 若 regex 不是以 refs/ 開頭, 會自動於前面加上 refs/heads/
群組
  • @all 代表所有人的意思
  • @myteam user1 user2 : user1, user2 都是屬於 myteam 這個群組

常用命令

下述全部都在 gitolite-admin.git 內操作
  • 新增帳號
    • cp /tmp/user1.pub keydir/user1.pub # 注意: 檔名要取 "帳號.pub"
  • 新增專案
    1. vim conf/gitolite.conf # 增加 repo, 例如:
      repo testing
      RW @all
    2. git clone gitolite@example.com:testing
  • 設定專案
    • vim conf/gitolite.conf # 增加 repo, 設定讀寫群組、使用者的權限

相關網頁

使用 Gitolite 架設 Git Server,並安裝 Gitweb

原文: http://soarlin.pixnet.net/blog/post/42810173-%E4%BD%BF%E7%94%A8-gitolite-%E6%9E%B6%E8%A8%AD-git-server%EF%BC%8C%E4%B8%A6%E5%AE%89%E8%A3%9D-gitweb

  總共參考了大約四篇文章的教學,最後總算成功的搞定
1. Linux 架設使用 SSH 共享存取的 Git Server
2. Linux 使用 Gitolite 架設 Git Server
3. [Linux] 使用 Git + Gitolite + Gitweb 架設 Git Server @ Ubuntu 12.04
4. Install git/gitolite/gitweb on Ubuntu 14.04

  底下就稍微紀錄一下自己的操作步驟:

第一步,在 Linux 環境下架設共享存取的 Git Server

系統套件安裝
  • sudo -s (切換帳號為root)
  • apt-get install git git-core
協同開發人員產生 SSH public key
  • mkdir .ssh
  • cd .ssh
  • ssh-keygen 
  • cat ~/.ssh/id_rsa.pub  or 上傳到 Server /tmp/目錄下統一保存
Git Server 的設定
  1. ssh 連線到 Server 上
  2. sudo -s (切換帳號為root)
  3. useradd -s /bin/bash -m -G sudo -d /home/git git (建立名為 git 的使用者,並給予 sudo 權限)
  4. su - git (切換到 git user,以下動作以 git 權限操作)
  5. 將開發人員的 public key 加入認證檔
  6. cat /tmp/user1.pub >> ~/.ssh/authorized_keys
  7. cat /tmp/user2.pub >> ~/.ssh/authorized_keys
  8. 或是將上一步,每位開發人員 cat 到的 public key 自行增加到 ~/.ssh/authorized_keys 檔案中

建立 Git Repository

  1. ssh 連線到 Server 上
  2. su git (切換到 git user,以下動作以 git 權限操作)
  3. mkdir -p ~/project_name.git
  4. cd ~/project_name.git
  5. git init --bare --shared

 

開發人員 clone 程式碼

  • git clone git@<網址 or IP>:/home/git/project_name.git

  以上作法是最基本的以 git 帳號來建立 Git Server,優點是適合人少的專案,架設步驟簡單快速;缺點是無法針對不同專案給予不同開發者存取權限,所以需要往下一步
 

第二步,使用 Gitolite 管理 Git Server

系統套件安裝
  • apt-get install gitolite
 
產生/收集 SSH Public Key
  • ssh 連線到 Server 上
  • su - git (切換到 git user,以下動作以 git 權限操作)
  • cd .ssh (若沒有該目錄,請先自行建立)
  • ssh-keygen -t rsa -f admin (產生名為 admin 的 public key)
  • 請所有人將各自的 public key 放到 /tmp/ 下,並且已各自名稱命名 (e.g. alex.pub, ben.pub, john.pub …)
  • 請注意,這裡有別於第一步是將開發人員的 public key 通通加入 authozied_keys 內,如果之後想改用 Gitolite 來控制,可以先把 authozied_keys 另行更名備份,避免與接下來安裝步驟有牴觸
 
Gitolite Server 架設
  • ssh 連線到 Server 上
  • su - git (切換到 git user,以下動作以 git 權限操作)
  • gl-setup ~/.ssh/admin.pub (匯入管理者的 Public key)
  • 這裡是將 git 這個帳號當作 Gitolite 的管理者,所以後續管理 repository 主要也是透過 git 這個帳號來管理
.
├── .gitolite/
├── .gitolite.rc
├── repositories/
│   ├── gitolite-admin.git
│   └── testing.git
├── .ssh
├── .vim
└── .vimrc

這時 git 帳號的 $HOME(家目錄)下,會產生

  • .gitolite/ 目錄
  • repositories/ 目錄
  • .gitolite.rc 設定檔

 
repositories/ 目錄下,有兩個專案

  • gitolite-admin.git (用來管理所有專案的主要專案)
  • testing.git (測試用專案)



建立專案
因為gitolite的專案管理,是透過在主專案(gitolite-admin.git)裡,改寫 config 檔案來設定,所以需要以管理者帳號
(剛剛使用 git 帳號,因為產生的 admin.pub,被設定為 gitolite 管理者的認證)
  • ssh 連線到 Server 上
  • su - git (切換到 git user,以下動作以 git 權限操作)
  • git clone git@localhost:gitolite-admin.git (抓主專案來修改設定)
  • cd gitolite-admin/
  • vim conf/gitolite.conf

範例:增加一個名為 ios_test 的專案,並設定 alex 為可存取的使用者,john 為可讀,加入以下幾行到 conf 檔中
※ 這邊需要先加入開發人緣,可以先參考下方教學來加入開發人緣

repo    ios_test
        RW      =   alex
        R       =   john

  • 修改完畢後,以一般 git 上 code 的流程處理
  • git add .
  • git commit -m "add ios_test project"
  • git push

順利的話,可以看到畫面上出現類似下面,建立 project 的訊息

Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 423 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: creating ios_test...
remote: Initialized empty Git repository in /home/git/repositories/ios_test.git/
remote:
To git@localhost:gitolite-admin.git
   a37d282..62dffa9  master -> master
 
加入開發人員
在主專案中,加入開發人員的 public key 檔
  • ssh 連線到 Server 上
  • su - git (切換到 git user)
  • cd gitolite-admin/
  • cp -r /tmp/*.pub keydir/ (將之前收集的pub檔,複製到keydir目錄下)
  • git add -A
  • git commit -m "add developers public key"
  • git push


第三步,安裝 Gitweb 並整合 Gitolite
系統套件安裝
  • sudo apt-get install gitweb apache2
  • sudo usermod -a G git www-data (將 www-data 使用者加入git group)
 
Apache 2.4 與 Gitweb 設定
gitweb 安裝後,產生的設定是 Apache 2.2 的設定寫法,所以在2.4版需要做些修正
  • 調整 gitweb 設定檔位置
  • sudo cp /etc/apache2/conf.d/gitweb /etc/apache2/conf-available/gitweb.conf
  • cd /etc/apache2/conf-enabled
  • sudo ln -s ../conf-available/gitweb.conf
 
  • 修正 gitweb conf 內容
  • sudo vim /etc/apache2/conf-enabled/gitweb.conf
Alias /gitweb /usr/share/gitweb
 
  Options +FollowSymLinks +ExecCGI
  AddHandler cgi-script .cgi
 
  • sudo a2enmod cgi (Apache 載入 cgi model)
  • sudo service apache2 restart

 
修正 Gitweb conf

  • sudo vim /etc/gitweb.conf , 修改下列黑字,增加藍字

$projectroot = "/home/git/repositories";
# Syntax highlighting
$feature{'highlight'}{'default'} = [1];
# 提供系統 loadavg check,若系統繁忙,逛 gitweb 只會看到 503 - The load average on the server is too high 訊息
$masload = 500;
$projects_list = $projectroot; # unmark this line
 
修正 Gitolite 部分
  • ssh 連線到 Server 上
  • su - git (切換到 git user)
  • vim .gitolite.rc
$REPO_UMASK = 0027; # change this value
$WEB_INTERFACE = "gitweb”;  # unmark this line
$GL_GITCONFIG_KEYS = "gitweb.owner gitweb.description .*”;   # change this value
 
建立專案設定檔修改部分 gitolite-admin/conf/gitolite.conf
  • 將所有專案加入 gitweb 與 daemon 兩個帳戶的讀取權限,才能讓 gitweb 讀取到專案資料
  • 原有的專案可以添加一些資訊,來透過 gitweb 顯示出來

加入部分
repo    @all
        R       =   gitweb daemon
 
專案描述部份
repo    testing
        RW+     =   @all
    config gitweb.owner         = "Project Owner Name"
    config gitweb.description   = "Project brief description"
    config gitweb.url           = git@:testing.git
 
  • 檢查 repositories 目錄權限可被讀取
  • sudo service apache2 restart (重啟 Apache)
  • 連線到 http://IP or 網址/gitweb

2015年11月25日 星期三

Handbook - GIT

--------------------------------------------------------------------------------------------------------------------
GIT DIFF
--------------------------------------------------------------------------------------------------------------------
List the file names only that changed between two commits:
$ git diff --name-only [commit1] [commit2] [file3]

List the file names (and some more detail) that changed between two commits:
$ git diff --stat svr/bl2 svr/bl3
$ git diff --name-status [SHA1 [SHA2]]
Show diff using graphics GUI:
$ git difftool -t meld  [commit1] [commit2] [file3]

git diff [-- folder/file] => workspace vs staged
git diff HEAD => workspace vs HEAD
git diff --cached [HEAD] => staged vs HEAD
git diff HEAD^ HEAD => HEAD^ vs HEAD

--------------------------------------------------------------------------------------------------------------------
GIT  all
--------------------------------------------------------------------------------------------------------------------
Remove local untracked files from workspace:
$ git clean -f -d -n         # -d to include folders;  -n for dry run
$ git clean -f -d -x         # -x to ignore .gitignore

List all branches/tag:
$ git branch -a
$ git tag
$ git ls-remote --tags
List all tags and their SHA:
$ git show-ref --tag

Remove a local branch:
$ git branch -d
$ git branch -D

Remove a local tag:
$ git tag -d &lt;tag>


Remove a remote branch/tag:
$ git push :&lt;branch/tag>

Fetch a repository from a remote (update database only):
$ git fetch --prune

Multiple remotes, pushed (and fetched) independently:$ cd myproject
$ git remote add bitbucket ssh://git@bitbucket.org/user/myproject.git
$ git push bitbucket master
Single remote with multiple URLs, pushed (and fetched) consecutively.
$ cd myproject
$ git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git 
$ git push origin master

Revoke recent 2 commits:
git reset HEAD~2

List SHA and comment, one line per one commit, from bl2 to bl3:
$ git log --pretty=oneline --author=flxm.com svr/bl2..svr/bl3

Replay the the patches, from the common ancestor of the bl2 and dev branches, onto dev2:
$ git rebase --onto dev2 svr/bl2 svr/dev1

Display history:
$ gitk --all --select-commit=svrX/branchY

Change commit author at one specific commit
For example, if your commit history is A-B-C-D-E-F with F as HEAD, and you want to change the author of C and D, then you would...
  1. Specify git rebase -i B
  2. change the lines for both C and D to edit
  3. Once the rebase started, it would first pause at C
  4. You would git commit --amend --author="Author Name "
  5. Then git rebase --continue
  6. It would pause again at D
  7. Then you would git commit --amend --author="Author Name " again
  8. git rebase --continue
  9. The rebase would complete.

List all authors
$ git log --format='%aN &lt;%aE>'
$ git log --format='%aN>' | sort -u
List everyone who committed to a particular project, listed alphabetically. To list by commits, add -n to the shortlog.
$ git shortlog -s | cut -c8-
Gets the authors, sorts by number of commits (as a vague way of estimating how much of the project is their work, i.e. the higher in the list, the more they've done) and then outputs the results.
$ git log --format='%aN &lt;%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | cut -d\  -f2-

Cherry-pick only changes to certain files
git cherry-pick -n 

# unstage modifications you don't want to keep, and remove the
# modifications from the work tree as well.
# this does work recursively!
git checkout HEAD 

# commit; the message will have been stored for you by cherry-pick
git commit
If the vast majority of modifications are things you don't want, instead of checking out individual paths (the middle step), you could reset everything back, then add in what you want:
# unstage everything
git reset HEAD

# stage the modifications you do want
git add 

# make the work tree match the index
# (do this from the top level of the repo)
git checkout .
Q: .gitignore doesn't work
A1: for every line, remove all invisible tailing characters, including SPACE; CR,LF,NULL are OK.
A2: use below commands.  If history was pushed to remotes, rework is needed to clean remote history.
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
Q: List commits w/ logs containing a keyword.
A: git log --grep="keyword"

Q: fast forward to branch head
A: git merge /.

Q: find possible filenames in commits
A: git log --name-only | grep

Q: list commits modify a file
A: git log --follow --

Q: show SHA of the commit before a specific commit
A: git log ^1

Q: show SHA of all commits on a specific branch
A: git log --pretty=format:'%H'

Q: check if a given commit on a specific branch
A: git log --pretty=format:'%H' | grep

Q: show history for a sub directory
A: git log --

Q: show commit ID of tags/branches
A: git show-ref

Q: show commit ID of current commit
A: git log --pretty=format:'%h' -n 1

Q: list tags points to given commit ID
A: git tag --points-at

Q: list tags points to current commit
A: git tag --points-at `git log --pretty=format:'%h' -n 1`

Q: List all files currently under source control of a specific branch.
A: git ls-tree -r --name-only

Q: List of all files that ever existed
A: git log --pretty=format: --name-status | cut -f2- | sort -u

Q: checkout certain revision of a file under a new name
A: git show HEAD^:main.cpp > old_main.cpp

Q: Resolve Git merge conflicts in favor of their changes during a pull
A: git merge --strategy-option theirs

Q: How to checkout src/main.c to another filename?
A: git show HEAD~4:src/main.c > main.c.prev4

Q: How to view the change history of a file?
A: git log -- [filename]
    gitk --follow [filename]

Q: How to see remote tags?
A: git ls-remote --tags <remote>

Q: How to list branches that contain a given commit?
A: git branch --contains <commit>

Q: How to archive all files modified ?
A: git archive -o update.zip <dst_commit> $(git diff --name-only <src_commit> <dst_commit>)

Q: How to show stashes in gitk without `--all` option?
A: gitk `git stash list --format=%H` &

Q: How to show current branch?
A: git branch | grep '*'

--------------------------------------------------------------------------------------------------------------------
GIT patch create & apply
--------------------------------------------------------------------------------------------------------------------
  • git format-patch --root
    • 從root到指定commit的patch
  • git format-patch -n
    • 起, 最近n個commit的patch
  • git format-patch 5e86795..f2b286a
    • 從指定起始commit到結束commit的patch
-o 可以設定輸出的資料夾
git am 0001-modify-page.patch


--------------------------------------------------------------------------------------------------------------------
GIT configuration
--------------------------------------------------------------------------------------------------------------------
git config --global commit.template $HOME/.gitmessage.txt
git config receive.denyDeleteCurrent false

--------------------------------------------------------------------------------------------------------------------
REPO
--------------------------------------------------------------------------------------------------------------------
Add remote for all repositories listed in the manifest.xml.
$ repo forall -c "git remote add ssh://@/home//\$REPO_PROJECT"

List tags points to current commit for all repositories
$ repo forall -c "pwd; git tag --points-at \`git log --pretty=format:'%h' -n 1\`"

Apply manifest to another work space
yulin@WNC:/HOUSE/yulin/mdm9607-le-2-1_amss_standard_oem_r00040p1_PostCS/apps_proc$ repo forall -c "cd /HOUSE/yulin/ptr9628/apps_proc/\$REPO_PATH; pwd; git tag LE201r00075.1 \$REPO_RREV"

--------------------------------------------------------------------------------------------------------------------
Account
--------------------------------------------------------------------------------------------------------------------

  • ssh-keygen -t rsa 或 ssh-keygen -d (dsa) => 產生出 id_rsa, id_rsa.pub
  • scp id_rsa.pub server_hostname:~/.ssh/
  • ssh server_hostname
  • cat .ssh/id_rsa.pub >> .ssh/authorized_keys 即可
  • 這樣子就可以 key 認證登入, 不需輸入密碼

--------------------------------------------------------------------------------------------------------------------
'git log' and 'git show-ref' show different SHA1
--------------------------------------------------------------------------------------------------------------------
~/9150w$ git show-ref --deref v00.07.2k.00
2c79b9c8211db8df33b3e3e387a41edd8a391b72 refs/tags/v00.07.2k.00
b1895be23436bd74642bbd7cc9b0177e70177848 refs/tags/v00.07.2k.00^{}

~/9150w$ git show-ref v00.07.2k.00
2c79b9c8211db8df33b3e3e387a41edd8a391b72 refs/tags/v00.07.2k.00

~/9150w$ git log -n 1  v00.07.2k.00 | grep commit
commit b1895be23436bd74642bbd7cc9b0177e70177848

Refer to https://stackoverflow.com/questions/2534773/what-is-dereference-in-the-context-of-git-show-ref-d,
command "git tag <tag> <branch or HEAD or alike>" is suspected to create an annotated tag.
command "git tag <tag>" creates a lightweight tag.



# Show commits that contains the code, then you can: git show <commit_sha>
git log -S "whatever you want to search for"

# An even easier way to show matches right there on the spot
git log -S "your search term" -p

# Same as above, except applied to all branches instead of the checked out branch
git log -S "something" -p --all

# Limit your search to only specific files in a specific directory
git log -S "something specific" -p -- **/models/user*.py

# Search for a regex instead of a string (note the -G instead of -S)
git log -G '^alias \w+=".*"$' -p

# a string or regular expression with the same flag
git log --grep "your search term"

# Same as above, except applied to all branches instead of the checked out branch
git log --grep "something else" --all