顯示具有 電腦資訊-版本控制-git 標籤的文章。 顯示所有文章
顯示具有 電腦資訊-版本控制-git 標籤的文章。 顯示所有文章

2016/04/29

git 如何移除本地端中 remote 已不存在的 branch



常常我們合併了一個 branch 出去之後必須把該 branch 關掉

通常 git server 都可以在網頁上操作關閉該 branch

可是你的 local 並不會自動移除該 remote branch,所以你下 git remote -a 時就會有一堆你曾經 checkout 過的 branch

可以下
```
git remote prune "你要同步的 remote"
```
他就會把 remote 上不存在的分支從你的本地端移除囉

2016/04/27

git 好用的 alias


紀錄一下 git 好用的 alias 以避免公司鎖 evernote 讓我又找不到了

```
[alias]
co = checkout
ci = commit
di = diff --ignore-space-change --ignore-space-at-eol
st = status -sb
br = branch -v
rt = reset --hard
unstage = reset HEAD
uncommit = reset --soft HEAD^
l = log --pretty=oneline --abbrev-commit --graph --decorate
lg = "log --color --graph --oneline --decorate --pretty=format:'%C(auto)%h%Creset%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
amend = commit --amend
who = shortlog -n -s --no-merges
g = grep -n --color -E
cp = cherry-pick -x
nb = checkout -b

# 'git add -u' handles deleted files, but not new files
# 'git add .' handles any current and new files, but not deleted
# 'git addall' now handles all changes
addall = !sh -c 'git add . && git add -u'

# Handy shortcuts for rebasing
rc = rebase --continue
rs = rebase --skip
ra = rebase --abort
ri = rebase --interactive

logf = log --follow
purge = clean -d -f -x
head = !sh -c 'git clean -d -f -x && git reset --hard HEAD'
```

2015/03/06

在CentOS 5 安裝新版 git-1.9.5



在 CentOS 5 下,如果使用 yum 安裝 git 的話目前只會安裝到 git-1.7 版

如果想要用新一點的版本就只能自己編譯了

在ubuntu安裝最新版的git


以下指令在 ubuntu 14.04 / 12.04 測試過

目前最新版本是 git 2.3.1 (2015/03/06)

安裝步驟:
```
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
```

查看現在版本,已經變成2.3.1囉
```
moa@ubuntu:~/$ git --version
git version 2.3.1
```

2015/02/26

將別人的git repo加到自己repo底下的某個資料夾中



這標題實在很難下...

舉個例子來說

我原先有一個 repo git@github.com:moa/webserver.git

後來,我需要加入一些靜態網頁,但是又不想混在webserver這個repo裡

所以我建立了一個 git@github.com:moa/webpage.git

裡面只有放靜態網頁

現在,因為某些因素必須將 webpage.git 加到 webserver.git 的 static/webpage/ 資料夾下

首先我已經在webserver的repo裡了
指令如下
```
# 將webpage.git加到名為remotes/webpage/*
git remote add webpage git@github.moa/webpage.git
# 更新webpage.git到remotes/webpage/*
git fetch webpage
# 利用ours策略將remotes/webpage/master merge到所在branch
git merge -s ours --no-commit webpage/master
# 把webpage/master載到static/webpage/
git read-tree --prefix=static/webpage/ -u webpage/master
# 送交
git commit -m "Imported webpage.git to static/webpage/ as a subtree."
```

2015/01/21

重新命名 git branch

rename git branch

常常開了一個新分支後發現取名不夠好想修改

那麼該怎麼做呢

很簡單

第一個用法-要改所在分支的名字
1. 看一下改名前
```
$ git branch
* br_oldname 12345678 Branch to rename
master abcdef12 I am master
```

2. 改所在的br_oldname為br_newname
```
$ git branch -m br_newname
```
3. 修改結果
```
$ git branch
* br_newname 12345678 Branch to rename
master abcdef12 I am master
```

第二個用法-在其他branch要改其他branch的名字

1. 看一下改名前
```
$ git branch
* br_here 55661234 I am here
br_oldname 12345678 Branch to rename
master abcdef12 I am master
```

2. 改br_oldname為br_newname
```
$ git branch -m br_newname br_oldname
```
3. 修改結果
```
$ git branch
* br_here 55661234 I am here
br_newname 12345678 Branch to rename
master abcdef12 I am master
```

2014/10/03

如何產生ssh key供git登入


以下如果要產生給github用的 ssh key

我習慣命名為github

```
ssh-keygen -t rsa -P '' -f ~/.ssh/github -C 'your@email5566.com'
```

這樣會在你的~/.ssh下建立兩個檔案
~/.ssh/github 與 ~/.ssh/github.pub

~/.ssh/github是私鑰要好好保存
~/.ssh/github.pub可以丟到github上

接著建立~/.ssh/config檔案,內容為

```
Host github.com
Hostname github.com
User git
Port 22
identityfile ~/.ssh/github
```

這樣在使用ssh連到github.com時他就會自己做認證了

如何做 “git export” (像 “svn export”)?



完成一個版本後想要發行

如果用git clone的話又會產生.git資料夾

以下提供如何做到像svn export一樣的功能

利用git archive
```
$ git archive -h
usage: git archive [options] <tree-ish> [<path>...]
or: git archive --list
or: git archive --remote <repo> [--exec <cmd>] [options] <tree-ish> [<path>...]
or: git archive --remote <repo> [--exec <cmd>] --list

--format <fmt> archive format
--prefix <prefix> prepend prefix to each pathname in the archive
-o, --output <file> write the archive to this file
--worktree-attributes
read .gitattributes in working directory
-v, --verbose report archived files on stderr
-0 store only
-1 compress faster
-9 compress better

-l, --list list supported archive formats

--remote <repo> retrieve the archive from remote repository <repo>
--exec <cmd> path to the remote git-upload-archive command
```

使用方法1:
倒出master到某個資料夾

```
git archive master | tar -x -C /somewhere/else
```


使用方法2:
倒出master時加入參數直接打包壓縮
```
git archive --format tar.gz --output /full/path/to/zipfile.tgz master
```

2014/10/01

轉移 svn 到 git



最近公司開始導入 git

我也開始學了一下~ 覺得 git 實在很好用

所以要把自己的 svn server 改成 git

以下教學如何把 svn 移植到git

首先先將 svn 中原有使用者資訊給列出

```shell
svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > ~/authors-transform.txt
```

再來用灌 git-svn 並執行:
```shell
git svn clone "你的 svn repo" --no-metadata -A ~/authors-transform.txt --no-minimize-url ~/temp
```

這樣~/temp就會是你的 repo 了,如果原先 repo 很雜亂,多個專案都使用同一個的話也沒問題。

再來把git專案push到git server上

```shell
git remote add origin git@bitbucket.org:yours.git
git push -u origin --all
```

完成

2015/02/03 補充

在轉換的 svn 中,空的資料夾會遺失
必須加上 --preserve-empty-dirs

```shell
git svn clone "你的 svn repo" --no-metadata --preserve-empty-dirs -A ~/authors-transform.txt --no-minimize-url ~/temp
```
官網說明:
--preserve-empty-dirs
Create a placeholder file in the local Git repository for each empty directory fetched from Subversion. This includes directories that become empty by removing all entries in the Subversion repository (but not the directory itself). The placeholder files are also tracked and removed when no longer necessary.

--placeholder-filename=<filename>
Set the name of placeholder files created by --preserve-empty-dirs. Default: ".gitignore"


2016/01/07 補充

若只想 clone 部分範圍的 commit 可已加上 -r "開始版號":"結束版號"

```shell
git svn clone -r 100:200 "你的 svn repo" --no-metadata --preserve-empty-dirs -A ~/authors-transform.txt --no-minimize-url ~/temp
```