git使用杂记

Creative Commons
本作品采用知识共享署名

本文记录git使用过程中的一些操作

commit合并

作用:当提交一个Pull request时可能有多个commit信息,Log太长我们可以进行合并(squash)
状态:

1
2
3
4
5
6
7
8
9
10
11
commit e1a7dfa9dfea8e63ad079dba37c61d8e80ffbe1b
Author: Frank Li
Date: Mon Nov 28 14:00:00 2016 +0800

add line in squash.txt

commit c6e45575484666245bb22d2d5d534bfee91f44c6
Author: Frank Li
Date: Mon Nov 28 13:57:43 2016 +0800

create squash.txt

操作:

1
git rebase -i c6e4557

提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pick c6e4557 create squash.txt
pick e1a7dfa add text in squash.txt

# Rebase a71eba2..e1a7dfa onto a71eba2
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

修改为如下并保存:

1
2
pick c6e4557 create squash.txt
s e1a7dfa add text in squash.txt

提示:

1
2
3
4
5
6
7
8
# This is a combination of 2 commits.
# The first commit's message is:

create squash.txt

# This is the 2nd commit message:

add line in squash.txt

修改合并的commit信息保存即可

修改commit信息

作用:修改之前commit信息
操作:

1
git commit --amend

彻底删除文件

作用:从git仓库中彻底删除文件(git rm并不能彻底删除文件)
操作:

1
2
git rm path-to-your-remove-file
it filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path-to-your-remove-file' --prune-empty --tag-name-filter cat -- --all

path-to-your-remove-file为文件路径

bare使用

git init时使用bare可以建立独立仓库,被用于做push和pull,可以在局域网的情况下不搭建git server做多人代码管理,以下操作均在windows git bash下操作
建立bare repo

1
2
3
mkdir /d/repo.git
cd /d/repo.git
git init --bare

推送代码

1
2
3
4
5
cd /f/source
git init
git add .
git commit
git push /d/repo.git master

本机下载代码

1
git clone /d/repo.git

局域网内下载代码:
A-PC windows下共享/mnt/d/repo.git(D:\repo.git),并给予读写权限。共享后B-PC windows下可以通过\192.168.1.66\repo.git访问,在B-PC 的git bash下执行

1
git clone //192.168.1.66/repo.git