Git笔记,


Git 工作流程如下:

未跟踪文件:在工作目录中新建的没有add的文件。

修改文件:在工作目录中修改文件。(已跟踪)

暂存文件:将文件的快照放入暂存区域。(已修改)

提交更新:找到暂存区域的文件,将快照永久性存储到 Git 仓库目录。

用户信息

--global 选项 所有本机git项目都使用该配置,不加--global只针对特定项目使用不同的用户信息

git config --global user.name "my_name"
git config --global user.email "my_email@mail.com"

检查配置信息

$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto

获取帮助

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

在现有目录中初始化仓库 移动到该目录后

$ git init

跟踪新文件or添加内容到下一次提交中

提交的版本是最后一次运行 git add 命令时的那个版本

$ git add *.c
$ git add LICENSE

提交更新

$ git commit -m 'initial project version'

跳过使用暂存区域 不用频繁使用add

$ git commit -a -m 'added new benchmarks'

克隆远端的仓库 会在当前目录下建立一个libgit2

$ git clone https://github.com/libgit2/libgit2

克隆远端的仓库 在当前目录下建立一个mylibgit

$ git clone https://github.com/libgit2/libgit2 mylibgit

检查当前文件状态 该命令还显示了当前所在分支(默认:master)

$ git status

状态简览

$ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

新添加的未跟踪文件前面有 ?? 标记,新添加到暂存区中的文件前面有 A 标记,修改过的文件前面有 M 标记。 你可能注意到了 M 有两个可以出现的位置,出现在右边的 M 表示该文件被修改了但是还没放入暂存区,出现在靠左边的 M 表示该文件被修改了并放入了暂存区。 例如,上面的状态报告显示: README 文件在工作区被修改了但是还没有将修改后的文件放入暂存区,lib/simplegit.rb 文件被修改了并将修改后的文件放入了暂存区。 而 Rakefile 在工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录。

忽略文件 文件 .gitignore

GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表

https://github.com/github/gitignore

从工作目录中移除文件

$ git rm PROJECTS.md

从暂存区域中删除文件

$ git rm --cached README.txt

移动文件,重命名文件

$ git mv file_from file_to

查看提交历史 提交时间列出所有的更新

$ git log

显示每次提交的内容差异。 你也可以加上 -2 来仅显示最近两次提交

$ git log -p -2

每次提交的简略的统计信息

$ git log --stat

查看路径下文件的log

$ git log --/web

查看远程仓库 origin 是使用clone 命令时的自动默认命名

$ git remote

你也可以指定选项 -v,会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL。

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

添加远程仓库 并命名为pb origin 是使用clone 命令时的自动默认命名

$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin    https://github.com/schacon/ticgit (fetch)
origin    https://github.com/schacon/ticgit (push)
pb    https://github.com/paulboone/ticgit (fetch)
pb    https://github.com/paulboone/ticgit (push)

从远程仓库中抓取与拉取 可以按命名抓取

$ git fetch origin
$ git fetch pb
$ git pull origin

推送到远程仓库 [remote-name] [branch-name]

$ git push origin master

查看远程仓库 [remote-name]

$ git remote show origin

远程仓库的移除

$ git remote rename pb paul
$ git remote
origin
paul

远程仓库的重命名

$ git remote rm paul
$ git remote
origin

分支创建

$ git branch testing

分支切换

$ git checkout testing

分支合并 (将hotfix合并到master)

$ git checkout master
$ git merge hotfix

删除分支

$ git branch -d hotfix

回退所有内容到上一个版本

$ git reset HEAD^

回退a.py这个文件的版本到上一个版本

$ git reset HEAD^ a.py

向前回退到第3个版本

$ git reset –soft HEAD~3

将本地的状态回退到和远程的一样

$ git reset --hard origin/master

回退到某个版本

$ git reset -q --hard 12752264ae430603c931ed52452ad2402dd21d70

回退到上一次提交的状态,按照某一次的commit完全反向的进行一次commit

$ git revert HEAD

撤销对单个文件做出的更改

$ git checkout -- <file>

从svn平滑迁移至Git

获取当前svn系统的用户信息,用于对应到git的用户信息

svn log svn://127.0.0.1:40004 --username netadmin --password netadmin --xml | grep author | sort -u | \
  perl -pe 's/.*>(.*?)<.*/$1 = /'

格式

schacon = Scott Chacon <schacon@geemail.com>
selse = Someo Nelse <selse@geemail.com>

git命令行

git svn clone svn://svn.qiang-bei.com:40003 --authors-file=users.txt --no-metadata -s my_project
cp -Rf .git/refs/remotes/* .git/refs/heads/
rm -Rf .git/refs/remotes

服务器搭建Git服务器

添加git用户与key文件

adduser git
su git
cd
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

创建git文件夹

mkdir /home/git/gitfile
cd /home/git/gitfile
mkdir project.git
cd project.git
git init --bare

限制git用户只访问自己的内容

确定git-shell所在的路径 /usr/bin/git-shell

which git-shell

添加git-shell至shells /usr/bin/git-shell

vim /etc/shells

设置git用户的shell 为 /usr/bin/git-shell /bin/bash

chsh git

客户端建立ssh公钥 .pub 然后加入到git用户的authorized_keys文件

ssh地址

git@192.168.32.21:/home/git/gitfile/rego.git

ssh地址,更改了默认端口

ssh://git-tlm@git.rego.com:40002/home/git-rego/rego.git

忽略已被跟踪的文件的改动

git update-index --assume-unchanged common/config/db-account.php

恢复跟踪

git update-index --no-assume-unchanged common/config/db-account.php

相关内容