$ 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>
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 文件。
$ git commit -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 $git branch or $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 branchesgit branch -r# start tracking one remote branchgit branch --track some_branch origin/some_branch# change to the branch locallygit checkout some_branch# make changes and commit them locally....# push your changes to the remote repository:git push
创建远程分支
# create a new branch locallygit branch name_of_branch git checkout name_of_branch
# edit/add/remove files #
... #
Commit your changes locallygit add fileName git commit
-m Message# push changes and new branch to remote repository:git push origin name_of_branch:name_of_branch