Git

Git

(1) 如何搭建 Git 服务器

请参考4.4 Git on the Server - Setting Up the Server

1
2
3
mkdir project.git
cd project.git
git init --bare

(2) 如何生成 SSH Key

请参考Generating a new SSH key

1
ssh-keygen -t rsa -b 4096 -C "igozhaokun@163.com"

然后拷贝 ~/.ssh/id_rsa.pub 中的内容到 Git 服务器电脑的 ~/.ssh/authorized_keys 文件中去

(3) Git Diff

(4) Git Data Transport Commands

(5) Git Reset

(6) Git Tagging

1
2
3
4
5
$ git tag -a v1.4 -m "my version 1.4"
$ git tag
$ git show v1.4
$ git push origin --tags
$ git checkout -b [branchname] [tagname]

(7) Git Ignore

Visit gitignore.io

(8) 恢复误删除的文件

误删除,但是未提交:

1
2
git reset HEAD file.txt
git checkout file.txt

(9) 添加一个 new remote

1
2
3
4
5
6
7
git remote add origin https://github.com/user/repo.git
# Set a new remote

git remote -v
# Verify new remote
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)

(10) git rm

不想跟踪某个文件或者文件夹了:

1
git rm -r --cached .idea/

(11) 删除分支

删除远程分支:

1
git push origin --delete <your_branch>

删除本地分支:

1
git branch -D <branch_name>

(12) HEAD detached from

当前修改的东西不在任何一个分支上,但是也已经 commit 了,想要将修改部分的东西合并并切换到正确的分支上的方法:

  • 创建一个临时分支
  • 切换到正确分支,merge 临时分支
  • 删除临时分支

参考: What’s a “detached HEAD” in Git?

(13) 如何安装 Git

1
sudo apt install git

(14) 显示某个文件被编辑的历史记录

1
git log -- [filename]

(15) 冲突

(16) 切换到某个 commit 然后再返回

1
2
3
4
5
6
7
git checkout 3c4f7121bdf2f1ab9294dd525f7eb316360c2577
# 一次还可以这样操作,多次使用这个命令就回不来了
git checkout -

# 或者
git branch
git checkout master

(17) 推送本地新分支到远程服务器

1
2
git checkout -b <branch>
git push -u origin <branch>

(18) git pull 的命令

Incorporates changes from a remote repository into the current branch. In its default mode (默认情况), git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

git pull will fetch new commits from all tracked branches (所有跟踪的版本) from the default remote(origin) (默认远程 origin)


To see which remote servers (哪个远程服务器) you have configured, you can run the git remote command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin – that is the default name Git gives to the server you cloned from.

You can also specify -v, which shows you the URLs (显示 URL) that Git has stored for the shortname to be used when reading and writing to that remote.

1
2
3
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)

git pull 默认不能用:

git_pull

git pull origin 的默认行为:

git_pull_origin

执行如下命令后:

1
git branch -u origin/master

会发现 config 文件发生了一点变化:

git_config

我们创建另外一个分支 R1 之后,并再次调用 -u origin/R1 发现:

git_config_R1

git pull 默认合并远程的哪一个分支,是需要配置的。并不是默认 master 就从 master 分支,R1 就从 R1 分支。

(19) 帮助文档

1
git help [COMMAND]

显示所有支持查看帮助文档的命令 [COMMAND]:

1
git help -a

(20) git 查看版本

1
git version

(21) 查看某个文件变化历史

1
2
3
4
5
git log -p filename

# 或者安装 gitk
sudo apt install gitk
gitk filename

(22) Git 浅克隆远程某个 tag 分支

1
git clone --branch zookeeper-3.5.4 --depth 1 git@github.com:apache/zookeeper.git

Git--depth 命令的解释:

1
2
3
--depth <depth>

Create a shallow clone with a history truncated to the specified number of commits.

推荐文章