常用Git命令及技巧总结

最近切换到git版本控制来维护代码,记录常用命令如下。另外,推荐git学习的书籍,《Pro Git》。另外,记录一些Git技巧。


1. 常用Git命令

初始化本地git仓库

git init


提交文件

git add *.c
git add README
git commit -m 'initial project version”


克隆远程仓库到本地

git clone git://github.com/schacon/grit.git
git clone git://github.com/schacon/grit.git mygrit


查看git状态,未提交的文件等

git status


比较更改

git add benchmarks.rb
git diff
git diff --cached

本地提交代码

git commit

git commit -m "Story 182: Fix benchmarks for speed
git commit -a -m 'added new benchmarks


本地删除代码

git rm grit.gemspec
git rm --cached readme.txt


本地移动文件

git mv file_from file_to

查看git仓库的提交记录
git log
git log –p -2
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s
git log --pretty=format:"%h %s" --graph
git log --since=2.weeks
git log --pretty="%h:%s" --author=gitster --since="2008-10-01" --before="2008-11-01" --no-merges -- t/

追加提交代码,等同于一次提交
git commit --amend
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
git reset HEAD benchmarks.rb
git checkout -- benchmarks.rb

建立远程仓库与本地仓库的联系
git clone git://github.com/schacon/ticgit.git
cd ticgit
git remote
git remote -v
git remote add pb git://github.com/paulboone/ticgit.git
git fetch pb


修改远程仓库branch

git push origin master
git remote show origin
git remote rename pb paul
git remote rm paul


Tag操作
git tag
git tag -l 'v1.4.2.*'
git tag -a v1.4 -m 'my version 1.4'
git show v1.4
git tag -s v1.5 -m 'my signed 1.5 tag'
git tag v1.4-lw
git tag -v v1.4.2.1
git tag -a v1.2 9fceb02
git push origin v1.5
git push origin --tags

Branch分支操作
git branch testing
git checkout testing
git checkout master

git checkout -b iss53
git checkout master
git merge hotfix
git branch -d hotfix
git checkout iss53
git checkout master
git merge iss53
git branch -d iss53


git branch -v
git branch --merged
git branch --no-merged

建立远程分支与本地分支的联系与操作
git fetch origin
git push origin serverfix
git merge origin/serverfix
git checkout -b serverfix origin/serverfix
git checkout --track origin/serverfix
git checkout -b sf origin/serverfix

git push origin :serverfix



1. Git本地配置

主要是配置你的用户名和邮箱。

git config --global user.name "Yu Zhou (yzhou86)"
git config --global user.email "yzhou86@gmail.com"

2. 配置SSH key到Git远程仓库

在你需要下载、提交和操作远程仓库的服务器或pc上,运行如下命令:

#ssh-keygen

一路回车。会在你的用户主目录下生成ssh key。

#ls ~/.ssh

其中一个文件是公钥。复制他的内容。

#cat ~/.ssh/id_rsa.pub

将公钥内容拷贝到git远程仓库admin页面,你的account页面的ssh key中。这样,此台服务器就可以以你的名义连接到git远程仓库。






3. 初始化远程git代码仓库

如果有一个空的远程git代码仓库,如何从本地提交代码来初始化远程git代码仓库。

You have an empty repository

To get started you will need to run these commands in your terminal.
New to Git? Learn the basic Git commands
Configure Git for the first time
#git config --global user.name "Yu Zhou (yzhou86)"
#git config --global user.email "yzhou86@gmail.com"
Working with your repository
I just want to clone this repository
If you want to simply clone this empty repository then run this command in your terminal.
#git clone ssh://git@githost:port/project.git
My code is ready to be pushed
If you already have code ready to be pushed to this repository then run this in your terminal.
#cd existing-project
#git init
#git add --all
#git commit -m "Initial Commit"
#git remote add origin ssh://git@githost:port/project.git
#git push origin master

My code is already tracked by Git
If your code is already tracked by Git then set this repository as your "origin" to push to.
#cd existing-project
#git remote set-url origin ssh://git@githost:port/project.git
#git push origin master








4. 重置git仓库并清空history

Completely reset?

Delete the .git directory locally.

Recreate the git repostory:

$ cd (project-directory)
$ git init
$ (add some files)
$ git add .
$ git commit -m 'Initial commit'

Push to remote server, overwriting. Remember you're going to mess everyone else up doing this … you better be the only client.

$ git remote add origin <url>

$ git push --force --set-upstream origin master#



5. 从SVN迁移到Git并保存提交记录

1. 创建usersmapping.txt,建立svn用户和git用户映射关系(左边为svn用户,右边为git用户)
user1 = user11<user11 @test .com>
user2 =user21< user21 @ test .com>
user4 =user4< user4 @ test .com>
2. git命令clone svn仓库
# 这里只保留了trunk信息,如果你使用标准的trunk/branches/tags方式,请查看--stdlayout用法
# usersmapping.txt为用户映射关系
# 下载时间可能会比较长
git svn clone --no-metadata -A usersmapping.txt https: //svnhost/csra csra
3. 如果用户没有找到,更新usersmapping.txt,然后再次fetch
cd csra
git svn fecth
4. 进入新的git仓库
cd csra/
5. 在添加一些必要文件,与`.git`目录平行
  • .gitignore (一些不需要版本控制的文件),以下是示例文件
    # Eclipse project files
    .classpath
    .project
    .settings/
    # Intellij project files
    *.iml
    .idea/
    # Others
    target/
    logs/
  • README.md(简介信息)

添加文件完成后,commit到本地仓库

cd csra
# 查看变更信息
git status
 
# 添加文件
git add -A
git commit -m "init project"
6. 删除老的本地远程仓库,push 到新的远程仓库
cd csra
# 查看远程仓库地址
git remote -v
# 删除老的本地远程仓库地址
git remote rm origin
# 添加新的远程仓库
git remote add origin ssh: //gituser@githost:port/csra.git
# 下载远程仓库并与本地仓库合并
git pull origin master
# push本地master分支到远程仓库,这里请保证已经配置过ssh key认证
git push origin master


6. 撤销本地提交

用以下命令可以撤销上一次本地的提交(commit)

git reset HEAD~1


7. 撤销已经push到远程仓库的commit


如果你已经把你的本地分支push到了远程仓库分支,但是突然发现有一个commit错了。没问题,你可以解决它。但是你必须快点行动,在其他人fetch错误的commit之前解决问题。

首先有两种替代方案,可以不改变history。
替代方案1:用一个新的commit改正错误。
把错误改正,然后commit。

替代方案2:revert整个commit
例如有一个commit,版本号是dd61ab32,那么直接revert它。
$ git revert dd61ab32

关于改写history:
应该避免改变history,否则已经clone或者fork分支的人,就不能继续从远程仓库pull,因为history已经改变。然而有时候又不得不改变history,可能是为了防止敏感信息的泄露,又或者去除掉错误提交的大文件,或者仅仅是为了让history干净。

在开源项目中,仓库维护者一般决不允许修改history,会组织所有非fast forward的push。

情景1: 删除上一次commit
删除上一次commit是最简单的情景。假设有一个远程仓库mathnet,master分支指向commit dd61ab32。我们希望撤销最近一次的commit。翻译成git的语言就是,我们希望强制mathnet远程仓库的master分支指向dd61ab32的父节点。
$ git push mathnet +dd61ab32^:master

git把x^理解为x的父节点,把+理解为强制的非fast forward push。如果你在本地check out了master分支,你也可以简单2步完成:首先reset分支到当前commit的父节点,然后force-push到远程仓库。
$ git reset HEAD^ --hard
$ git push mathnet -f

情景2: 删除上上一次的commit
我们假设commit dd61ab32不是最后一次提交,而是上上一次。我们想撤销它,但是又想保持它之后的提交继续存在。重写history最简单的方式就是,做一次交互型的rebase,在当前commit和dd61ab32的父节点之间进行rebase。
$ git rebase -i dd61ab32^

这将打开编辑器,列出所有dd61ab32之后的commit。
pick dd61ab32
pick dsadhj278
...
简单地将带有dd61ab32的行去掉,保存并退出。resolve所有的冲突,那么你的本地分支就fix了。force push到远程分支,完成!
$ git push mathnet -f

情景3: 在某一次commit中改正一处错误
Case 3: Fix a typo in one of the commits
这和情景2的方法继续一样,区别在于,不是将错误commit的那些行直接去掉,而是把pick改成edit,然后保存退出编辑器。这样rebase时就会在那个commit处停止,将这个改动放入index,然后让你来改。提交这些改动,继续rebase(git会告诉你如何保持commit message和作者信息,如果你需要的话)。然后将改动push。同样的方法,你甚至可以将commit分解成更小的commit,或者将commit合并。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值