git常見操作

來源:互聯網
上載者:User

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

$ git init
將在目前的目錄建立一個隱藏的名為".git"的目錄。
$ git init project1
等價於 $ mkdir project1 && cd project1 && git init
$ git status
檢查目前的目錄是否包含一個git repo
$ ls .git
查看git目錄
$ 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 檔案。

$ 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 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.