GIT handbook

流程:取代码 -> 每次工作前更新代码到最新版本 -> 修改代码 -> 提交代码到服务器

 

 

h3. 取代码及修改全局设置

 

设置用户名与邮箱

 

git config --global user.name "My Name"

git config --global user.email "my@email.com"

 

 

从已有的 git库中提取代码

 

git clone git@server:app.git myrepo

 

 

h3. 每次更改代码的操作

 

更新本地代码到最新版本(需要 merge才能合到本地代码中)

 

git fetch

 

 

合并更新后的代码到本地

 

git merge

 

 

更新代码方式的另一种方法 (git pull git fetch git merge命令的一个组合 )

 

git pull

 

 

修改代码后,查看已修改的内容

 

git diff --cached

 

 

将新增加文件加入到 git

 

git add file1 file2 file3

 

 

git中删除文件

 

git rm file1

git rm -r dir1

 

 

提交修改

 

git commit -m 'this is memo'

 

 

如果想省掉提交之前的 git add 命令,可以直接用

 

git commit -a -m 'this is memo'

 

 

bq. commit commit -a的区别 , commit -a相当于:

 

* 第一步:自动地 add所有改动的代码,使得所有的开发代码都列于 index file

 

* 第二步:自动地删除那些在 index file中但不在工作树中的文件

 

* 第三步:执行 commit命令来提交

 

提交所有修改到远程服务器,这样,其它团队成员才能更新到这些修改

 

git push

 

 

 

 

h3. 其它常用命令

 

显示 commit日志

 

git log

 

 

不仅显示 commit日志,而且同时显示每次 commit的代码改变。

 

git log -p

 

 

回滚代码:

 

git revert HEAD

 

你也可以 revert更早的 commit,例如:

 

git revert HEAD^

 

 

销毁自己的修改

 

git reset --hard

 

 

查看最新版本和上一个版本的差异 (一个 ^表示向前推进一个版本 )

 

 

 

 

 

 

 

 

 

git diff HEAD HEAD^

 

 

branchname分支合并到当前分支中。 (如果合并发生冲突,需要自己解决冲突 )

 

git merge branchname

 

 

解决冲突

 

bq. merge命令自身无法解决冲突的时候,它会将工作树置于一种特殊的状态,并且给用户提供冲突信息,以期用户可以自己解决这些问题。当然在这个时候,未发生冲突的代码已经被 git merge登记在了 index file里了。如果你这个时候使用 git diff,显示出来的只是发生冲突的代码信息。

 

bq. 在你解决了冲突之前,发生冲突的文件会一直在 index file中被标记出来。这个时候,如果你使用 git commit提交的话, git会提示: filename.txt needs merge

 

bq. 在发生冲突的时候,如果你使用 git status命令,那么会显示出发生冲突的具体信息。

 

在你解决了冲突之后,你可以使用如下步骤来提交:

 

第一步 (如果需要增加文件 )

 

git add file1

 

第二步:

 

git commit

 

 

h3. git恢复删除了的文件

 

git pull git服务器取出,并且和本地修改 merge, 类似于 SVN up,但是对删除的文件不管用,恢复删除文件用

 

git checkout -f

 

 

 

GIT中文教程: http://www.bitsun.com/documents/gittutorcn.htm

详细手册

CONFIGURE

标识你自己:电子邮件和名字:


git config --global user.name "David Beckwith"

git config --global user.email "dbitsolutions@gmail.com"


看所有用户:


git config --list


或者


cat .gitconfig


设置别名 


git config --global alias.co checkout


查看你的配置 


cat .gitconfig


去忽略空白 (Ruby is whitespace insensitive)


git config --global apply.whitespace nowarn


Some nice aliases:

[[ plain
gb = git branch
gba = git branch -a
gc = git commit -v
gd = git diff | mate
gl = git pull
gp = git push
gst = git status


START USING GIT 


git init


TO IGNORE SOME FILES 

在根目录下增加一个叫 .gitignore 的文件,并且增加你想ignore的文件:


.log
db/schema.rb
db/schema.sql


Git自动ignore空目录。如果你想包含log目录,但是想ignore里面的所有文件,首先在.gitignore文件里加log/, 然后在这个空目录下再添加一个空的 .gitignore 文件。


touch log/.gitignore


add新文件以及所有change到git index 


git add .


看状态 


git status


提交 


git commit -m "First import"


看已经被提交的 


git ls-files


删除一个文件 


git rm [file name]


提交当前repos的所有的改变 


git commit -a


添加一个文件到git index 


git add [file name]


当你用-v参数的时候可以看commit的差异 


git commit -v


添加commit信息 


git commit -m "This is the message describing the commit"


-a是代表add,把所有的change加到git index里然后再commit 


git commit -a


一般提交命令: 


git commit -a -v


看你commit的日志 


git log


TO VIEW A LOG OF YOUR COMMITS WITH A GRAPH TO SHOW THE EXTENT OF THE CHANGES 


git log --stat


TO HAVE PAGINATION WHEN VIEWING THE LOG FILE USE THE -v OPTION 


git log -v


TO VISUALIZE YOUR CHANGES 


gitk --all


TO CREATE A NEW BRANCH 


git branch [name of your new branch]


TO VIEW ALL OF THE EXISTING BRANCHES 


git branch


TO VIEW A LIST OF ALL BRANCHES 


git branch -a


TO SWITCH TO ANOTHER BRANCH 

The state of your file system will change after executing this command.


git checkout [name of the branch you want to switch to]

OR

git co [name of the branch you want to switch to]


TO MAKE SURE THAT YOUR NEW BRANCH GETS CHANGES FROM THE MASTER BRANCH (WHERE EVERYBODY ELSE IS WORKING) USE THE REBASE COMMAND 

git rebase master

TO MERGE YOUR NEW BRANCH INTO THE MASTER BRANCH 

First, switch back to the master branch:

git co master

Check to see what changes you’re about to merge together, compare the two branches:

git diff master xyz

If you’re in a branch that’s not the xyz branch and want to merge the xyz branch into it:

git merge xyz

TO REVERT YOUR CHANGES to before the merge. 

git reset --hard ORIG_HEAD

TO RESOLVE CONFLICTS just edit your file. 

Remove the markings, add the file, then commit.

TO CREATE A BRANCH AND SWITCH TO THE BRANCH IN ONE MOVE: 

git checkout -b [name of new branch]

TO CREATE A “CLIPBOARD” or “STASH” OF CHANGES THAT ARE NOT YET COMMITED (SO THAT YOU CAN SWITCH TO ANOTHER BRANCH IN THE MIDDLE OF YOUR CHANGES.), CREATE A STASH.

git stash "Put a message here to remind you of what you're saving to the clipboard"


TO SWITCH AWAY FROM THE CURRENT BRANCH 

git co [branch you want to switch to]

Do whatever Then switch back to the stashed branch 

git co [the stashed branch]

TO VIEW THE LIST OF STASHES 

git stash list

TO LOAD BACK THE “CLIPBOARD” OR “STASH” 

git stash apply

Now you can continue to work where you were previously.

TO DELETE A BRANCH THAT IS NOT USED ANYMORE, but already merged into the current branch. (TO CLEAN UP)

git branch -d [name of branch you want to delete]

TO DELETE AN UNMERGED BRANCH 

git branch -D [name of branch you want to delete]

TO DELETE THE STASH. (ERASE THE “CLIPBOARD” FROM MEMORY)

git stash clear

TO SET UP YOUR REPOSITORY FOR SHARING ON A CENTRAL SERVER 

Copy up your repository. e.g.:

scp -r my_project deploy@yourbox.com:my_project

Move your files on the remote server to /var/git/my_project For security make the owner of this project git On the repository server:

sudo chown -R git:git my_project

Then (for security) restrict the “deploy” user to doing git-related things in /etc/passwd with a git-shell.

TO CHECK OUT THE GIT REPOSITORY TO YOUR LOCALHOST. ON YOUR LOCAL HOST DO THIS:

git clone git@yourbox.com:/var/git/my_project

TO SEE SOME INFO ABOUT THE REPOSITORY THAT WILL TELL YOU WHICH REPOSITORY IS THE MASTER AND WHICH IS THE SLAVE:

cat .git/config

By virtue of having cloned the remote repository, your local repository becomes the slave and will track and synchronize with the remote master branch.

TO UPDATE YOUR LOCAL BRANCH FROM THE REMOTE SERVER: 

git pull

TO GET A COPY OF THE ENTIRE REMOTE REPOSITORY (e.g. a repository named “laptop”) WITHOUT MERGING THEM INTO YOUR LOCAL BRANCHES USE FETCH

git fetch laptop

TO MERGE TWO LOCAL BRANCHES (ie. your local xyz branch with your local master branch) USE MERGE 

git merge laptop/xyz

This merged the (already copied laptop repository’s xyz branch) with the current branch you’re sitting in.

TO MERGE THE REMOTE BRANCH WITH YOUR LOCAL BRANCH THAT YOU ARE SITTING IN USE PULL
TO ADD LOCAL KNOWLEDGE (TO YOUR LOCAL REPOSITORY) OF A 2ND REMOTE REPOSITORY, LIKE YOUR LAPTOP

git remote add laptop duo2book.local:repos/m_project

where ’’‘laptop’’” is the name of the remote repository and ”’‘duo2book.local’’” is the name of the remote machine.

TO VIEW META INFORMATION ABOUT THAT REMOTE REPOSITORY 

git remote show laptop

TO PUSH A COMMITTED LOCAL CHANGE OF THE xyz BRANCH TO THE REMOTE laptop BRANCH 


git push laptop xyz


TO CREATE A TRACKING BRANCH (A SLAVE BRANCH). Ie. to link a local branch to a remote branch:

git branch --track local_branch remote_branch

NOW IF YOU’RE SITTING IN THE LOCAL TRACKING BRANCH, TO PULL YOU DON’T NEED TO SPECIFY THE REMOTE TRACKING BRANCH:

git  pull

Note: You can track(link) different local branches to different remote machines. For example, you can track your friend’s “upgrade” branch with your “bobs_upgrade” branch, and simultaneously you can track the origin’s “master” branch (of your main webserver) with your local “master” branch.

By convention, ‘origin’ is the local name given to the remote centralized server which is the way SVN is usually set up on a remote server.

TO SEE WHICH LOCAL BRANCHES ARE TRACKING A REMOTE BRANCH:

git remote show origin


TO WORK WITH AN SVN REPOSITORY BUT WORK WITH GIT LOCALLY:

git-svn clone [http location of an svn repository]

Now you can work with the checked out directory as though it was a git repository. (cuz it is)
TO PUSH (COMMIT) CHANGES TO THE REMOTE SERVER

git-svn dcommit

TO UPDATE YOUR LOCAL REPOSITORY FROM THE SVN REPOSITORY

git-svn rebase

NOTE: make sure you have your perl bindings to your local svn installation.
I screwed up, how do I reset my checkout?

git checkout -f
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值