转自:http://www.cnblogs.com/elfsundae/archive/2011/07/17/2099698.html

References:

http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide

http://www.kernel.org/pub/software/scm/git/docs/

http://progit.org/book/

git安装、配置用户名邮箱、SSH服务器搭建

http://www.cnblogs.com/elfsundae/archive/2011/07/06/2099182.html

Create/List/Remove a new Project/Repository

$ git init
将在当前目录创建一个隐藏的名为".git"的目录。
$ git init project1
等价于 $ mkdir project1 && cd project1 && git init
$ git status
检查当前目录是否包含一个git repo
$ ls .git
查看git目录
$ rm -rf .git/
移除有关git的所有东西

Configure git to ignore files

.gitignore文件可以定义要忽略的文件。详细规则见http://www.kernel.org/pub/software/scm/git/docs/gitignore.html

过滤文件夹: /build/
过滤某种类型的文件:  *.tmp
过滤某各文件: /Build/Products/test.app
!开头表示不过滤: !*.c , !/dir/subdir/
支持通配符: *.[oa] 过滤repo中所有以.o或者.a为扩展名的文件

有三种方法应用过滤:

  1. 对该repo的所有用户应用过滤:
    将 .gitignore 文件放在工作目录的跟目录,编辑.gitignore完成后提交
    git add .gitignore
  2. 仅对自己的repo备份过滤:
    添加/编辑你工作目录的$GIT_DIR/info/exclude,例如你的working copy目录是
    ~/src/project1 , 则路径为
    ~/src/project1/.git/info/exclude
  3. 系统全局过滤
    创建一个ignore文件,名字随意起,比如我的放在 ~/.gitglobalignore ,然后配置git:
    $ core.excludesfile = ~/.gitglobalignore

.gitignore文件示例:

.DS_Store### build directoryiMochaApp/build/iMochaSDK/build/### Testing projects directory/Testing/

Getting the latest Code

$ git pull <remote> <branch> # fetches the code and merges it into                              # your working directory$ git fetch <remote> <branch> # fetches the code but does not merge                              # it into your working directory

$ git pull --tag <remote> <branch> # same as above but fetch tags as well$ git fetch --tag <remote> <branch> # you get the idea

Checking Out Code (clone)

$ git clone user@host.com/dir/to/repo [Target DirName]

Commit Changes

当修改了文件,你需要提交(commit)这些更改。

$ git commit source/main.c
上句将提交 ./source/ 目录下的 main.c 文件。

$ git commit -a
-a标识表示提交所有修改过的文件,但是不提交新增加的文件。新增加的文件需要使用$ git-add 将其添加到git的索引中。

“提交”仅改变你本地repo,如果要提交更改到服务器,需要使用push:
$ git push <remote> <branch>

查看当前状态

$ git status 可以查看当前工作与那个branch,将要提交什么,提醒你忘记了什么等等...

Undo/Revert/Reset a commit

如果不想让当前的更改生效,返回之前的提交,可以运行如下命令:
# Revert to a previous commit by hash:
$ git-reset --hard <hash>

可使用 HEAD^ 快捷指定上一次提交hash:
# Revert to previous commit:
$ git-reset --hard HEAD^

文件比较

比较命令是 $ git diff

# to compare 2 revisions of a file:
$ git diff <commit1> <commit2> <file_name>

# to compare current staged file against the repository:
$ git diff --staged <file_name>

#to compare current unstaged file against the repository:
$ git diff <file_name>

How do you see the history of revisions to a file?

$ git log -- filename

git branch (分支)

git默认分支叫 master

# create a new branch
$ git branch <branch-name>
# to see a list of all branches in the cureent repoitory
$ git branch
# if you want to switch to another branch you can use
$ git checkout <branch-name>
# to create a new branch and switch to it in one step
$ git checkout -b <branch-name>
# to delete a branch:
$ git branch -d <branch-name>
# to create a branch with the changes from the current branch,do :
$ git stash
$ git stash branch <branch-name>

How do you merge branches?

if you want to merge a branch(e.g. "master" to "release"), make sure your current branch is the target branch you'd like to merge into(use $git branch or $git status to see your current branch).

Then use
$ git merge master
(where master is the name of the branch you want to merge with the current branch).

If there are any conflicts, you can use
$ git diff
to see pending conflicts you have to resolve.

跟踪远程分支

假设你已经clone了一个具有 'some_branch' 分支的远端repo.下面的命令将本地跟踪这个分支:

# list remote branchesgit branch -r

# start tracking one remote branchgit branch --track some_branch origin/some_branch

# change to the branch locallygit checkout some_branch

# make changes and commit them locally....

# push your changes to the remote repository:git push

创建远程分支

# create a new branch locallygit branch name_of_branchgit checkout name_of_branch# edit/add/remove files    # ... # Commit your changes locallygit add fileNamegit commit -m Message# push changes and new branch to remote repository:git push origin name_of_branch:name_of_branch

删除远程分支

git push [远程名] :[分支名]

$ git push origin :mybranchname

作者:Elf Sundae (小糊涂)
本站采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。
转载请注明:转载自 Elf Sundae's Blog(http://www.cnblogs.com/elfsundae) (小糊涂闲)

git常见操作--忽略文件以及常用命令【转】相关推荐

  1. linux中剪切文件命令,Linux文件处理常用命令操作技巧

    我是Linux初学者,做个笔记,以下是Linux几个常用文件处理命令: 命令提示符 [root@localhost~]# 其中: root                          表示当前登 ...

  2. 你一定要知道的关于Linux文件目录操作的12个常用命令

    博客园 首页 新随笔 联系 管理 订阅 随笔- 26  文章- 1  评论- 18  你一定要知道的关于Linux文件目录操作的12个常用命令 转自:http://www.cnblogs.com/yo ...

  3. mysql 导入.sql文件_MySQL导入.sql文件及常用命令

    MySQL导入.sql文件及常用命令 在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次执行多条sql命令的,在mysql中执行sql文件的命令: mysql> so ...

  4. xshell中重启指令_远程服务器Xshell的使用 -- 重启服务器操作 和 linux的常用命令...

    一.重启服务 1. 查询相应服务的进程id(在列表中找到相应进程的id) $ ps -ef | grep java 2. kill 相应的id(关闭相应进程) kill -9 xxxx //xxxx表 ...

  5. mysql 导入sql命令_MySQL导入.sql文件及常用命令

    MySQL导入.sql文件及常用命令 在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次执行多条sql命令的,在mysql中执行sql文件的命令: mysql> so ...

  6. Linux文件系统(文件系统类型、设备文件、常用命令、U盘与光盘挂载)

    Linux文件系统(文件系统类型.设备文件.常用命令.U盘与光盘挂载)   本篇文章是Linux文件系统整块集合,包含了Linux文件系统介绍.设备文件介绍.常用文件系统命令(查看.修复与配置).挂载 ...

  7. git rm操作后文件恢复

    git rm操作后文件恢复 在终端下使用git rm file.txt或者rm file.txt命令后,终端显示如下: rm 'license.txt' $ ls readme.txt 恢复file. ...

  8. python的setup.py文件及其常用命令

    python的setup.py文件及其常用命令 上传者:tingting1718      我也要"分享赚钱" 2014/7/7 关注(286) 评论(0) 声明:此内容仅代表网友 ...

  9. SVN:安装svn进行上传和检出文件的常用命令

    QUESTION:SVN:安装svn进行上传和检出文件的常用命令 ANWSER: 1.将文件checkout到本地目录svn checkout path(path是服务器上的目录) 例如:svn ch ...

最新文章

  1. 模拟一个简单计算器_阅读模拟器的简单介绍
  2. python参数化_Python unittest 简单实现参数化的方法
  3. 设计模式之享元模式学习笔记
  4. 网页版python叫什么-python脚本和网页有何区别
  5. HBase之HFile解析
  6. 学生电脑哪个牌子好_面包冷藏车哪个牌子好
  7. python 写linux mysql_(linux)python之mysql数据库操作环境搭建
  8. echarts地图 编辑颜色
  9. 简单暴力到dp的优化(入门篇)
  10. 6-4cifar10数据介绍读取处理(下)
  11. activity绑定service
  12. Spring 下注解说明
  13. DXUT框架剖析(4)
  14. Google笔记本迈向烂笔头
  15. 【CF1244C】The Football Season(思维枚举/扩展欧几里德)
  16. python实现指纹识别毕业论文_指纹识别算法实现本科毕业论文
  17. vi编辑器 末尾添加_linux下的VI编辑器使用手册
  18. 基于站长之家(CNZZ)的网站流量统计分析 (以vue代码为例)
  19. 台湾大学 李宏毅教授的个人主页
  20. tp打印服务器修改ip,tp打印服务器和网络打印机安装方法.docx

热门文章

  1. 盘点 Github 上的高仿 app 项目
  2. Java反射到底慢在哪?
  3. 你还在费力的从零搭建项目吗?
  4. 轮椅度过一生!微软CEO纳德拉26岁长子去世,半生为儿也难逃病魔
  5. 博客文章也能中顶会:ICLR 2022开设博客投稿通道,还有机会跟经典论文原作者直接battle...
  6. 视频也能P!谷歌CVPR 2021最新视频P图模型omnimatte
  7. 「吃鸡」之父自立门户!研发新沙盒游戏连接元宇宙
  8. 【微软亚洲研究院MSRA】招聘多模态方向算法实习生
  9. 北京搜狗已签算法30W,西安银行总包20W,要不要毁约去银行?
  10. 扒出了3867篇论文中的3万个基准测试结果,他们发现追求SOTA其实没什么意义