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

g i t i n i t " . g i t "  git init project1
等价于  mkdir project1 && cd project1 && git init  git status 
检查当前目录是否包含一个git repo
l s . g i t g i t  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 文件。

g i t c o m m i t 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  g i t b r a n c h o r 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 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,如需转载请自行联系原作者

相关文章
|
8月前
|
开发工具 git
Git版本控制工具合并分支merge命令操作流程
通过以上步聚焦于技术性和操作层面指南(guidance), 可以有效管理项目版本控制(version control), 并促进团队协作(collaboration).
2297 15
|
8月前
|
开发工具 git 开发者
Git版本管理常见文件提交流程讲解
以上就是Git常见文件提交流程概述。掌握此流程对于任何使用Git进行版本控制和协同工作项目团队成员都至关重要。
336 13
|
11月前
|
安全 开发工具 git
git的常用操作命令
git的常用操作命令
638 57
|
人工智能 前端开发 Java
用git rebase命令合并开发阶段中多条commit提交记录
通过 `git rebase`,可以合并多个提交记录,使开发历史更简洁清晰。操作分为 6 步:查看提交历史 (`git log --oneline`)、设置需合并的提交数 (`git rebase -i HEAD~N`)、修改动作标识为 `s`(squash)、保存退出编辑、调整提交信息、强制推送至远程仓库 (`git push -f`)。此方法适合清理本地无关提交,但若有团队协作或冲突风险,需谨慎使用以避免问题。
2609 60
|
9月前
|
存储 缓存 开发工具
Git stash命令的详细使用说明及案例分析。
通过上述案例,我们看到stash命令能够在不丢失进度的情况下,帮助开发者临时切换开发上下文,这在处理多个任务或紧急bug时特别有用。正确使用Git stash可以大大提高开发的灵活性和效率。
2582 0
|
存储 项目管理 开发工具
Git常用命令及操作技巧
以上是Git的常用命令及操作技巧,尽管看起来有些繁琐,但实际上只要花费一些时间进行实践,您将很快熟练掌握。随着使用熟练度的提高,您会发现Git对项目管理和协同工作的强大帮助。
285 20
|
Linux 开发工具 git
版本控制工具:Git的安装和基本命令使用指南。
结束这段探险,掌握了Git你就等于掌握了一个宝藏,随时可以瞥见你的编程历程,轻松面对日后的挑战。Git,无疑是编程者的强大武器,开始你的Git探险之旅吧!
489 28
|
开发工具 git 索引
怎么取消对project.private.config.json这个文件的git记录
通过以上步骤,您可以成功取消对 `project.private.config.json`文件的Git记录。这样,文件将不会被包含在未来的提交中,同时仍保留在您的工作区中。
379 28
|
网络安全 开发工具 git
mac git clone命令提示git@gitee.com: Permission denied (publickey).问题修复
mac git clone命令拉取gitee上项目代码时提示密钥问题
1301 19
|
Java 网络安全 开发工具
Git进阶笔记系列(01)Git核心架构原理 | 常用命令实战集合
通过本文,读者可以深入了解Git的核心概念和实际操作技巧,提升版本管理能力。