===================client端初始化===================:
# git config --global user.name "willku"
# git config --global user.mail "willku@coderr.cn"
# git config --global core.editor vim
# git config --list (查看配置信息)
# mkdir linuxcode && cd linuxcode
# git clone root@192.168.1.1:/root/linuxprobe.git (克隆server端现有代码目录)
# git init
# git add README.md (添加到本地暂存区)
# git commit -m "first commit"(提交数据到server)
# git remote add server root@192.168.1.1:/root/linuxprobe.git (添加server端)
# git push -u server master(推送到master分支)
# 测试连接
# ssh-keygen -t RSA
# ssh-add
# ssh git@github.com
# git remote add origin git@github.com:[git_username]/[project].git
===================分支操作===================
//查看当前分支
# git branch
//切换分支
# git checkout new_branch
//创建分支
# git branch new_branch
//删除分支
# git branch -d new_branch
//创建并切换分支
# git checkout -b new_branch
//合并到master分支
# git merge new_branch
//工作目录各文件目前状态
# git status
===================打版本tag===================
# git tag v1.0
# git tag (查看版本)
# git show v1.0 (查看版本详细变更)
# git tag -d v1.0(删除标签)
===================配置上传忽略文件===================
# vim "工作目录/.gitignore"
# cat gitignore
//忽略所有以.a为后缀的文件
*.a
//但是lib.a这个文件除外
!lib.a
//忽略build目录内所有的文件
build
//忽略build目录内以txt为后缀的文件
build/*.txt
//忽略名字为git.c 的文件
git.c
===================git提交===================
# git init(已初始化可省略)
# git add readme.txt (加入暂存区)
# git commit -m "add new file:readme.txt" (提交)
# git status (查看一下,确认文件状态是否改变)
# git commit -a -m "Modify Again"
(automatically notice any modified (but not new) files, add them to the index, and commit, all in one step.)
# git add -f git.c (强制把.gitignore中忽略的文件加入到git)
===================git移除===================
# git rm --cached test.txt(移除暂存区,但是不删除文件)
# git rm -f test.txt(移除暂存区,同时删除文件)
===================git更名===================
# git mv test.txt test.c (在commit状态下会显示rename了文件)
或者
# mv test.txt test.c
# git rm test.txt (从commit库中删除)
# git add test.c
# git status
# git commit -m "modify test.txt-->test.c"
===================git历史===================
# git log
# git log -2(最近2条)
# git log -p -1(查看最近1条记录)
# git log --stat -2 (--stat参数来简要的显示数据的增改行数)
# git log --pretty=oneline
# git log --pretty=fuller
===================git还原===================
//对比文件差异
# git diff readme.txt
# git log pretty=oneline
# git reset --hard HEAD^(回到过去某个指针)
# git reflog (查看所有历史记录)
# git reset --hard 5cee5b
//直接从暂存区恢复
# git checkout -- readme.txt
git config --global user.name willxin
git config --global user.mail "willxin@tencent.com"
git config --global core.editor vim
git init
git clone http://git.code.com/g_DSECU_Platform_Support/XXX.git
cd anping-cmdb/
git checkout -b willxin_branch
git add .
gut status
git remote -v
git commit -m "add a test icon"
git status
git remote add willxin git@git.code.com:g_DSECU_Platform_Support/XXX.git
git push -u willxin willxin_branch
git clone的两种用法:
(1)克隆时创建grit目录
$ git clone git://github.com/schacon/grit.git
(2)克隆时候新建目录mygit
$ git clone git://github.com/schacon/grit.git mygit
git diff对比的两种方法:
(1)对比工作目录中当前文件和暂存区域快照之间的差异
$ git diff
(2)对比已经暂存起来的文件和上次提交时的快照之间的差异
$ git diff --cache(Git 1.6.1 及更高版本还允许使用 git diff --staged,效果是相同的,但更好记些。)
git commit相关:
(1)跳过git add直接提交已经track过的文件(注意:untrack的新文件将不会被提交)
$ git commit -a -m "msg..."
(2)最近一次提交时的commit信息
$ git commit --amend [-v]
git branch相关:
(1)选择git reflog中的记录,开辟新的分支:
$ git branch --orplan [new_branch_name] [4b23ac]
(2)若要查看各个分支最后一个提交对象的信息
$ git branch -v
(3)筛选出已经(或尚未)与当前分支合并的分支,可以用 --merge 和 --no-merged 选项(Git 1.5.6 以上版本)。
$ git branch --merged
(4)删除一个分支
$ git branch -d [branch_name](无未提交)
$ git branch -D [branch_name](有未提交)
(5)重命名分支
$ git branch (-m | -M) <oldbranch> <newbranch>
git rm相关:
$ git rm --cached test.txt(移出暂存区,但是不删除文件,变为untrack状态)
$ git rm -f test.txt(移出暂存区,同时删除文件)
git push/fetch/pull远程库相关:
(1)查看远程仓库
$ git remote -v(--verbose)
(2)添加远程仓库:git remote add [shortname] [url]
$ git remote add pb git://github.com/paulboone/ticgit.git
要抓取所有 Paul 有的,但本地仓库没有的信息,可以运行 git pull/fetch pb
推送分支给远端:git push [remote-name] [branch-name]
$ git push orgin master
(3)查看远程仓库的信息:git remote show [remote-name]
$ git remote show origin
(4)重命名仓库名:git remote rename
$ git remote rename pb paul
(5)若想把本地serverfix分支远程分支叫作 awesomebranch:git push [远程名] [本地分支]:[远程分支]。
$ git push origin serverfix:awesomebranch
(6)把远程的分支作为自己的本地的某个分支:git checkout -b [分支名] [远程名]/[分支名]
$ git checkout -b myserverfix origin/serverfix($ git checkout --track origin/serverfix)
(7)删除远程分支:git push [远程名] [空]:[分支名]
$ git push origin :serverfix
git push [远程名] [本地分支]:[远程分支] 语法,如果省略 [本地分支],那就等于是在说“在这里提取空白然后把它变成[远程分支]
git stash相关:
(1)暂存不想去提交的暂存区的东西
$ git stash
(2)显示stash历史记录
$ git stash list
(3)恢复stash list中的状态
$ git stash apply stash@{2}
git 撤销:
(1)如果刚才提交时忘了暂存某些修改,可以先补上暂存操作,然后再运行 --amend,最终只是产生一个提交,第二个提交命令修正了第一个的提交内容。
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
(2)恢复rm的文件到stage,再恢复到work directory(commit-->stage-->work directory)
$ git reset HEAD huffman.h
$ git checkout -- huffman.h
git tag相关:
(1)轻量级标签
$ git tag v1.4-lw
(2)注解性标签(高端选项:-a,-s 或 -m )
$ git tag -a v2.1 -m "This is my version 2.1"
(3)推送标签到仓库: git push origin [tagname]
$ git push origin v1.5
git log查看历史
(1)完整显示以及简略显示
$ git log --pretty=oneline(等价于$ git log --oneline)
$ git log --pretty=full
(2)关注显示的格式
$ git log -p -2(-p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新)
$ git log --stat
选项 说明
-p 按补丁格式显示每个更新之间的差异。
--stat 显示每次更新的文件修改统计信息。
--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
--relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
--graph 显示 ASCII 图形表示的分支合并历史。
--pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。
(3)format格式化git log 的显示
$ git log --pretty=format:"%h - %an, %ar : %s"
常用的格式占位符写法及其代表的意义。
选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明
(4)git log查找类
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
(注意:git log选项是路径(path),如果只关心某些文件或者目录的历史提交,可以在 git log 选项的最后指定它们的路径。因为是放在最后位置上的选项,所以用两个短划线(--)隔开之前的选项和后面限定的路径名)
选项 说明
-(n) 仅显示最近的 n 条提交
--since, --after 仅显示指定时间之后的提交。
--until, --before 仅显示指定时间之前的提交。
--author 仅显示指定作者相关的提交。
--committer 仅显示指定提交者相关的提交。
备份地址: 【git学习】