Git笔记

FROM: http://everet.org/notes/git.html

1 git flow

2 git 恢复单个文件

如果你只是要恢复一个文件(修复未提交文件中的错误),如”hello.rb”, 你就要使用 git checkout

$ git checkout -- hello.rb

3 clone

git clone --depth=1 git://github.com/imatix/zguide.git

Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches.

4 svn

Git and SVN treat tags differently. Tags are a powerful feature in Git, but just a folder in SVN, much like how branches are.

So to create tags in SVN from git-svn, you have to create a branch (tag) in git-svn repo. Git tags will not be "dcommitted"

You can use git svn branch –tag or its shorthand git svn tag to create tags and then dcommit them.

Related question with detailed answer: Git-svn: create & push a new branch/tag?

Exporting a project to a Git repository

–dry-run (-n)

# Create SVN branch called "my_topic"
git svn branch -m "Topic branch" my_topic
# Create the Git branch for "my_topic"
git checkout --track -b my-topic remotes/my_topic

# Hack hack hack...

# Make sure you're committing to the right SVN branch
git svn dcommit --dry-run
# Commit changes to "my_topic" branch in SVN
git svn dcommit
  • svn update
    git svn rebase
    
  • svn commit
    git svn dcommit
    
  • clone
    git svn clone file:///tmp/test-svn -T trunk -b branches -t tags
    

    其中-T trunk -b branches -t tags 告诉 Git 该 Subversion 仓库遵循了基本的分支和标签命名法则。如果你的主干(译注:trunk,相当于非分布式版本控制里的master分支,代表开发的主线),分支或者标签以不同的方式命名,则应做出相应改变。由于该法则的常见性,可以使用 -s 来代替整条命令,它意味着标准布局(s 是 Standard layout 的首字母),也就是前面选项的内容。下面的命令有相同的效果:

    git svn clone file:///tmp/test-svn -s
    

4.1 tag

git svn tag tag_name

此时创建的tag是直接在服务器将当前分支拷贝到tags目录,所以在此之前一定要将本地的修改推送到服务器,否则会将打错tag。

记得**dcommit**!

4.2 创建SVN分支

要在 Subversion 中建立一个新分支,需要运行 git svn branch [分支名] :

git svn branch opera

这相当于在 Subversion 中的 svn copy trunk branches/opera 命令,并会对 Subversion 服务器进行相关操作。值得注意的是它没有检出和转换到那个分支;如果现在进行提交,将提交到服务器上的 trunk, 而非 opera。

Git 与其他系统 - Git 与 Subversion

4.3 切换SVN分支

Git 通过搜寻提交历史中 Subversion 分支的头部来决定 dcommit 的目的地——而它应该只有一个,那就是当前分支历史中最近一次包含 git-svn-id 的提交。

如果需要同时在多个分支上提交,可以通过导入 Subversion 上某个其他分支的 commit 来建立以该分支为 dcommit 目的地的本地分支。比如你想拥有一个并行维护的 opera 分支,可以运行

git branch opera remotes/opera
git checkout opera
# or
git checkout -b opera refs/remotes/opera

然后,如果要把 opera 分支并入 trunk (本地的 master 分支),可以使用普通的 git merge。不过最好提供一条描述提交的信息(通过 -m),否则这次合并的记录是 Merge branch opera ,而不是任何有用的东西。记住,虽然使用了 git merge 来进行这次操作,并且合并过程可能比使用 Subversion 简单一些(因为 Git 会自动找到适合的合并基础),这并不是一次普通的 Git 合并提交。最终它将被推送回 commit 无法包含多个祖先的 Subversion 服务器上;因而在推送之后,它将变成一个包含了所有在其他分支上做出的改变的单一 commit。把一个分支合并到另一个分支以后,你没法像在 Git 中那样轻易的回到那个分支上继续工作。提交时运行的 dcommit 命令擦除了全部有关哪个分支被并入的信息,因而以后的合并基础计算将是不正确的—— dcommit 让 git merge 的结果变得类似于 git merge –squash。不幸的是,我们没有什么好办法来避免该情况—— Subversion 无法储存这个信息,所以在使用它作为服务器的时候你将永远为这个缺陷所困。为了不出现这种问题,在把本地分支(本例中的 opera)并入 trunk 以后应该立即将其删除。

4.4 合并SVN分支

# Create alias for checkout command:
git config alias.co checkout

# Make sure that you local branches are up to date:
git co master    # checkout branch that tracks subversion's trunk
git svn rebase
git co local/foo # checkout branch that tracks subversion's branches/foo
                 # It assumes that  the branch is created with the command:
                 # `git co -b local/foo remotes/foo`
                 # And the repo was created with:
                 # `git svn clone --stdlayout SVN_REPO_URL`
git svn rebase

# Merge branches:
# create new local branch based on `master`
git co master
git co -b merging_branch_foo
# merge, resolve conflicts, etc (pure git)
git merge local/foo

# rebase `merging_branch_foo` to linearize history for subversion
git rebase master # or `rebase -i`

# merge `merging_branch_foo` into `master`
git co master
# git merge merging_branch_foo  # --squash to create single commit
git merge --no-ff # no fast-forward. This will force git to create a merge commit, which can then be dcommitted to svn.

# commit changes to svn
git svn dcommit

# (optionally) delete `merging_branch_foo`
git branch -D merging_branch_foo

git-svn: reset tracking for master

4.5 删除远程SVN分支

Currently, it is not possible to delete an SVN branch using git-svn. But it is easy to delete the branch using SVN, without even having to check it out. So simply type

svn rm $URL/branches/the_branch

Please note that deleting a Subversion branch does not cause it to be deleted from the git-svn repository. (This is intentional, because deleting a Subversion branch does not cause any information loss, whereas deleting a git branch causes its existence to be forgotten following the next git garbage collection.) So if you want the remote SVN branch to be deleted from your git repository, you have to do it manually:

git branch -D -r the_branch
rm -rf .git/svn/the_branch

To delete a git branch that corresponds to a Subversion tag, the commands are slightly different:

git branch -D -r tags/the_tag
rm -rf .git/svn/tags/the_tag

Delete a svn-Branch via git?

4.6 Git-Svn 总结

git svn 工具集在当前不得不使用 Subversion 服务器或者开发环境要求使用 Subversion 服务器的时候格外有用。不妨把它看成一个跛脚的 Git,然而,你还是有可能在转换过程中碰到一些困惑你和合作者们的迷题。为了避免麻烦,试着遵守如下守则:

保持一个不包含由 git merge 生成的 commit 的线性提交历史。将在主线分支外进行的开发通通衍合回主线;避免直接合并。

不要单独建立和使用一个 Git 服务来搞合作。可以为了加速新开发者的克隆进程建立一个,但是不要向它提供任何不包含 git-svn-id 条目的内容。甚至可以添加一个 pre-receive 挂钩来在每一个提交信息中查找 git-svn-id 并拒绝提交那些不包含它的 commit。

如果遵循这些守则,在 Subversion 上工作还可以接受。然而,如果能迁徙到真正的 Git 服务器,则能为团队带来更多好处。

5 取消git add

To undo git add . ,use git reset

6 在服务器部署Git

可以使用一些Git软件如Gitosis。

不过也可以直接运行:

git clone --bare my_project my_project.git
# same as cp -Rf my_project/.git my_project.git

# by ssh
scp -r my_project.git user@git.example.com:/opt/git
git clone user@git.example.com:/opt/git/my_project.git


如果某个 SSH 用户对 /opt/git/my_project.git 目录有写权限,那他就有推送权限。

服务器上的 Git - 在服务器上部署 Git

7 Proxy

7.1 unset proxy

git config --global --unset core.gitproxy

7.2 git协议的代理

export GIT_PROXY_COMMAND="~/bin/proxy-wrapper"

8 submodule

8.1 添加

git submodule add git://github.com/chneukirchen/rack.git rack

8.2 删除

git submodule deinit asubmodule
git rm asubmodule
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached asubmodule

How do I remove a Git submodule?

9 丢失commit

在用git-svn或者某些外部程序调用git的时候,有时候会导致git丢失一些log。但是丢失log并不带代表是丢失commit,我们只需要找回以前的commit就行了。

利用reflog,我们很容易可以找到所有的commit,然后我们就可以将master之类的游标reset到我们想要去到的commit。

git reflog
git reset --hard 1e35a3

10 对比分支之间的区别

git diff --name-status master..file_buffer

11 设置个人信息

git config [--global] user.email "me@here.com"

12 Enable Colourful git diff

git config --global color.diff auto

13 打开git输出颜色

git config --global color.ui true

14 资料

15 submodule

15.1 delete

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes git add .gitmodules
  3. Delete the relevant section from .git/config.
  4. Run git rm –cached path_to_submodule (no trailing slash).
  5. Run rm -rf .git/modules/submodule_name
  6. Commit
  7. Delete the now untracked submodule files
  8. rm -rf path_to_submodule

From: How do I remove a Git submodule?

16 删除远程tags

git tag -d v1.0
git push --delete origin v1.0

17 删除远程分支

git push origin --delete <branchName>
git push origin :<branchName>

18 代理上Github

假设我们已经有了socks5代理

Host github.com
    ProxyCommand nc -x 127.0.0.1:8989 -X 5 %h %p

19 全局忽略

git config --global core.excludesfile '~/.gitignore'

然后在`~/.gitignore`里面添加全局忽略

20 undo last commit

$ git commit ...              (1)
$ git reset --soft "HEAD^"    (2)
$ edit                        (3)
$ git add ....                (4)
$ git commit -c ORIG_HEAD     (5)

How to undo the last Git commit?

21 checkout one file from

# checkout from branch develop
git checkout develop -- VideoEncoder.cpp

Author: Hua Liang [Stupid ET]

Last Updated: 2015-01-26 00:27 Monday

### Git 使用教程与学习笔记整理 Git 是一个分布式版本控制系统,广泛应用于软件开发中的代码管理和团队协作。以下是关于 Git 的使用教程和学习笔记的总结,涵盖基础命令、安装方法及常用操作。 #### 1. Git 安装方法 在不同的操作系统下,Git 提供了多种安装方式: - **Linux 系统**:可以通过包管理器安装,例如在基于 Debian 的系统上使用以下命令: ```bash sudo apt-get install git ``` 如果需要从源码编译安装,则可以依次执行 `./config`、`make` 和 `sudo make install` [^2]。 - **Windows 系统**:推荐使用官方提供的安装包进行安装。此外,已安装 Git 的用户可通过以下命令更新到最新版本: ```bash git update-git-for-windows ``` #### 2. 基础命令 Git 提供了一系列命令用于日常的版本控制操作: - **初始化仓库**: ```bash git init ``` 该命令用于创建一个新的 Git 仓库。 - **克隆远程仓库**: ```bash git clone <仓库地址> ``` 克隆现有仓库到本地,便于后续开发和修改 [^3]。 - **查看状态**: ```bash git status ``` 显示当前工作目录的状态,包括哪些文件已被修改但尚未提交。 - **添加文件至暂存区**: ```bash git add <文件名> ``` 将指定文件添加到暂存区,准备提交。若要添加所有文件,可使用 `git add .` [^3]。 - **提交更改**: ```bash git commit -m "提交信息" ``` 将暂存区的内容提交到本地仓库,并附带一条描述性的信息 [^3]。 - **推送更改至远程仓库**: ```bash git push origin <分支名> ``` 将本地提交的更改推送到远程仓库的指定分支上 [^3]。 - **拉取远程更改**: ```bash git pull origin <分支名> ``` 从远程仓库获取最新的更改并合并到本地分支中。 #### 3. 分支管理 分支管理是 Git 的核心功能之一,允许开发者在同一项目上并行开发多个特性或修复 bug。 - **查看分支**: ```bash git branch -v ``` 列出所有本地分支及其最新的提交记录 [^3]。 - **创建新分支**: ```bash git branch <分支名> ``` 创建一个新的分支,但不会自动切换到该分支 [^3]。 - **切换分支**: ```bash git checkout <分支名> ``` 切换到指定的分支,以便在该分支上进行开发 [^3]。 - **创建并切换分支**: ```bash git checkout -b <分支名> ``` 一次性完成创建新分支并切换的操作 [^3]。 - **合并分支**: ```bash git merge <分支名> ``` 将指定分支的内容合并到当前分支中 [^3]。 - **删除分支**: ```bash git branch -d <分支名> ``` 删除不再需要的分支 [^3]。 #### 4. 远程仓库操作 远程仓库通常用于团队协作,确保所有成员都能访问相同的代码库。 - **添加远程仓库**: ```bash git remote add origin <远程仓库地址> ``` 将远程仓库与本地仓库关联起来 [^3]。 - **查看远程仓库信息**: ```bash git remote -v ``` 显示所有远程仓库的详细信息 。 - **推送本地分支到远程仓库**: ```bash git push -u origin <分支名> ``` 将本地分支推送到远程仓库,并设置为默认跟踪分支 [^3]。 #### 5. 日常开发流程 在实际开发过程中,建议遵循以下步骤以保持良好的版本控制习惯: 1. 拉取远程仓库的最新代码: ```bash git pull origin main ``` 2. 添加所有修改的文件到暂存区: ```bash git add . ``` 3. 提交更改并附带清晰的提交信息: ```bash git commit -m "描述本次提交的内容" ``` 4. 推送更改到远程仓库: ```bash git push origin main ``` 通过以上流程,可以有效地管理项目的版本历史,并与其他开发者协同工作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值