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

简介:

转自: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

gitinit".git"gitinit将在当前目录创建一个隐藏的名为".git"的目录。 git init project1
等价于 mkdir project1 && cd project1 && git 
init
mkdir project1 && cd project1 && git init
 git status 
检查当前目录是否包含一个git repo
ls.gitgitls.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 directory
iMochaApp/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 文件。

gitcommitaa使gitcommit−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 <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 <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 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 gitbranchorgitbranchorgit 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 branches
git branch -r

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

# change to the branch locally
git checkout some_branch

# make changes and commit them locally
....

# push your changes to the remote repository:
git push
复制代码
复制代码

创建远程分支

复制代码
复制代码
# create a new branch locally
git branch name_of_branch
git checkout name_of_branch
# edit/add/remove files
# ...
# Commit your changes locally
git add fileName
git 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














本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/5197638.html,如需转载请自行联系原作者

相关文章
|
11天前
|
缓存 数据可视化 网络安全
Git命令大全
Git命令大全
45 1
|
15天前
|
开发工具 git
Git教程:深入了解删除分支的命令
【4月更文挑战第3天】
36 0
Git教程:深入了解删除分支的命令
|
26天前
|
开发工具 git
记IDEA Git版本回退并push到远程操作
记IDEA Git版本回退并push到远程操作
28 1
记IDEA Git版本回退并push到远程操作
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux git命令使用教程
【Shell 命令集合 文件管理】Linux git命令使用教程
33 0
|
1月前
|
开发工具 git
git常用命令整理
git常用命令整理
13 0
|
1月前
|
开发工具 git 开发者
|
1月前
|
开发工具 git
web后端-IDEA的Git操作
web后端-IDEA的Git操作
|
21天前
|
开发工具 git 开发者
Git常用命令大全:让你轻松驾驭版本控制
Git命令速查:`git init`新建仓库,`git clone`克隆,`git add`入暂存区,`git commit -m`提交,`git status`查看状态,`git log`查看历史,`git branch`创建分支,`git checkout`切换,`git merge`合并,`git pull`拉取更新,`git push`推送,`git remote -v`查看远程,`git checkout --`撤销本地修改,`git reset HEAD`取消暂存,`git reset --hard`回退版本。掌握这些,提升代码管理效率!
18 0
|
1天前
|
算法 Java BI
云效产品使用报错问题之平台上导出的统计数据和 git 中使用命令导出的数据统计都对不上,如何解决
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
8天前
|
Linux 开发工具 git
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!