git 详细教程,从头开始,看简单英文比中文好懂

本文是一份Git教程,涵盖了从设置用户名和邮箱开始的基础知识,包括创建本地仓库、克隆远程仓库、新增文件并推送、获取远程仓库更新。对于进阶用户,介绍了分支管理、打标签、修改远程分支、重命名分支以及对比差异等高级操作。

Get started

Setting up your username and email


  • GIT records your name and email address with every commit you make, so the first step is to tell GIT what these are so you can get credit for your work
GIT command
$ git config --global user.name 'Your Bitbucket username'
$ git config --global user.email you@somedomain.com
Sample Usage
$ git config --global user.name 'ravidte'
$ git config --global user.email 'ravid.te@owmessaging.com'


Beginner

 

PLEASE NOTE, Section I and II below, are not to be followed in order, they are 2 separate Actions (one to create a new local git repo to push to remote GIT, and the other to obtain "clone" an existing repo from GIT).

Please use one or the other for your purposes.

 

      NOTE:  Using Project DEVOPS for Sample Usage Examples


I. Version controlling your existing project

  • "git init" can be used to create a new GIT repository by converting an existing unversioned project to a GIT repository or initializing a new empty repository.
GIT command
$ cd ~ /workspace/project
$ git init
$ git remote add origin git@bitbucket.org:openwave/{reponame}.git
$ git pull origin master
$ git add <files>
$ git commit -m "JIRAKEY comment"
$ git push origin master
Sample Usage
$ cd ~/Work/AUTOMATION/
$ git init
Initialized empty Git repository in /Users/rte/Work/AUTOMATION/.git/
$ git remote add origin git @bitbucket .org:ravidte/testmigration.git
$ git pull origin master
remote: Counting objects: 5 , done.
remote: Compressing objects: 100 % ( 2 / 2 ), done.
remote: Total 5 (delta 0 ), reused 0 (delta 0 )
Unpacking objects: 100 % ( 5 / 5 ), done.
From bitbucket.org:ravidte/testmigration
  * branch            master     -> FETCH_HEAD
$ git add .
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   testfile1.java
#    new file:   testfile2.java
#    new file:   testfile3.java
#
$ git commit -m "DEVOPS-243 adding new project to test migration"
[master 64a0261] DEVOPS- 243 adding new project to test migration
  3 files changed, 3 insertions(+)
  create mode 100644 testfile1.java
  create mode 100644 testfile2.java
  create mode 100644 testfile3.java
$ git push origin master
Counting objects: 6 , done.
Delta compression using up to 4 threads.
Compressing objects: 100 % ( 2 / 2 ), done.
Writing objects: 100 % ( 5 / 5 ), 389 bytes | 0 bytes/s, done.
Total 5 (delta 0 ), reused 0 (delta 0 )
To git @bitbucket .org:ravidte/testmigration.git
    7448278 ..64a0261  master -> master

 



II. Cloning the codebase from remote GIT Bitbucket repository


  • "git clone" will copy the entire history of a project so you have it locally and it will give you a working directory of the main branch of that project so you can look at the code or start editing it. If you change into the new directory, you can see the .git subdirectory - that is where all the project data is.
GIT command
$ git clone https: //yourbitbucketusername @bitbucket.org /openwave/repo .git
Sample Usage
$ git clone https: //ravidte@bitbucket.org/openwave/devops.git
Cloning into 'devops' ...
remote: Counting objects: 11 , done.
remote: Compressing objects: 100 % ( 6 / 6 ), done.
remote: Total 11 (delta 0 ), reused 0 (delta 0 )
Unpacking objects: 100 % ( 11 / 11 ), done.
Checking connectivity... done





III.  Create a new file and push it to the remote Bitbucket Git repository


 

  • use "git add" to start tracking new files and also to stage changes to already tracked files
  • use "git commit" to record the items you have staged (using git add),the -m option provides the commit message in which a JIRAKEY is required as a prefix to the commit message
  • use "git push" to move/push your changes to the remote repository
GIT command
$ git add
$ git commit -m "JIRAKEY-123 short but descriptive commit message"
$ git push

NOTE: Wildcards can be used:

To add all files: git add *

To add all txt files:  git add *.txt

Sample Usage
$ cat > test.out
this is some text
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    test.out
nothing added to commit but untracked files present (use "git add" to track)
$ git add test.out
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   test.out
#
$ git commit -m "DEVOPS-243 added test.out file"
[master 8b67d01] DEVOPS- 243 added test.out file
  1 file changed, 1 insertion(+)
  create mode 100644 test.out
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
$ git push
Counting objects: 4 , done.
Delta compression using up to 4 threads.
Compressing objects: 100 % ( 2 / 2 ), done.
Writing objects: 100 % ( 3 / 3 ), 329 bytes | 0 bytes/s, done.
Total 3 (delta 0 ), reused 0 (delta 0 )
To https: //ravidte@bitbucket.org/openwave/devops.git
    fa42bbc..8b67d01  master -> master
ravids-imac:devops vid401t$ git status
# On branch master
nothing to commit, working directory clean

NOTE: "git status" used above is to showcase the status changes for each command, it is completely optional to use, but recommended to verify execution

 

 

IV.  Deleting a file from the remote Bitbucket Git repository

 

 

GIT command
$ git rm filetoberemoved
Sample Usage
$ git rm test.out
rm 'test.out'
$ git commit -m "DEVOPS-243 deleting test.out"
[master 7448278 ] DEVOPS- 243 deleting test.out
  1 file changed, 1 deletion(-)
  delete mode 100644 test.out
$ git push
Counting objects: 3 , done.
Writing objects: 100 % ( 2 / 2 ), 210 bytes | 0 bytes/s, done.
Total 2 (delta 0 ), reused 0 (delta 0 )
To https: //RavidTe@bitbucket.org/RavidTe/testmigration.git
    7e574ff.. 7448278  master -> master
$ git status
# On branch master
nothing to commit, working directory clean

 

 

V.  Get updates to your local workspace from a remote repository


 

You can use two methods

  • git fetch & git merge
  • git pull

 

git fetch will update your local remote tracking branches but not your local workspace

git pull will update your local remote tracking branches as well as your local workspace with changes

 

GIT command
$ git fetch
$ git whatchanged ...origin
$ git merge
Sample Usage
$ git fetch
remote: Counting objects: 4 , done.
remote: Compressing objects: 100 % ( 2 / 2 ), done.
remote: Total 3 (delta 0 ), reused 0 (delta 0 )
Unpacking objects: 100 % ( 3 / 3 ), done.
From https: //bitbucket.org/openwave/devops
    bef508d..b7f730c  master     -> origin/master
$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean
$ git whatchanged ...origin
commit b7f730c4ecedd250975cec3ec7b43c01c157fe0a
Author: ravidte-owmessaging <ravid.te @owmessaging .com>
Date:   Thu Feb 27 13 : 37 : 06 2014 - 0500
 
     Added new file
 
: 000000 100644 0000000 ... fa49b07... A  newfile.txt
$ git merge origin/master
Merge made by the 'recursive' strategy.
   newfile.txt | 1 +
   1 file changed, 1 insertion(+)
   create mode 100644 newfile.txt

Intermediate

      NOTE:  Using Project DEVOPS for Sample Usage Examples

I. Branching and merging


  • "git branch" gives you a list of all the branches (use -v for more detail, eg. git branch -v)
  • "git branch branchname" is used to create a new branch
  • "git checkout branchname" let's you switch to the branch
  • "git merge branchname" this let's you merge your current active branch with the other branch
  • "git branch -d branchname" this deletes the mentioned branchname

 

Scenario:  An urgent fix is needed to fix DEVOPS-242

GIT command
git status
$ git branch <somebranchname>
$ git checkout <somebranchname>
$ git add <file>
$ git commit -m  "JIRAKEY comment"
$ git checkout <master>
$ git merge <somebranchname>
$ git branch -d <somebranchname>
Sample Usage
$ cd ~/workspace/project
$ git status
# On branch master
nothing to commit, working directory clean
$ git branch -v
* master f59726b DEVOPS-241 added the missing footer
$ git branch DEVOPS-242
$ git branch -v
  DEVOPS-242 f59726b DEVOPS-241 added the missing footer
* master     f59726b DEVOPS-241 added the missing footer
$ git checkout DEVOPS-242
Switched to branch 'DEVOPS-242'
$ vim index.html
$ git add index.html
$ git commit -m "DEVOPS-242 fixed closing tag of footer div"
[DEVOPS-242 451f2d4] DEVOPS-242 fixed closing tag of footer div
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git status
# On branch DEVOPS-242
nothing to commit, working directory clean
$ git checkout master
Switched to branch 'master'
$ git merge DEVOPS-242
Updating f59726b..451f2d4
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git branch -v
  DEVOPS-242 451f2d4 DEVOPS-242 fixed closing tag of footer div
* master     451f2d4 DEVOPS-242 fixed closing tag of footer div
$ git branch -d DEVOPS-242
Deleted branch DEVOPS-242 (was 451f2d4).
$ git branch -v
* master 451f2d4 DEVOPS-242 fixed closing tag of footer div

 



II. Creating and using tags


  • "git tag"  let's you tag specific points in history as important
  • "git checkout <tagname>"
  • "git describe"
GIT command
$ git status
$ git tag <tagname>
$ git add <file>
$ git commit -m "JIRAKEY comment"
$ git describe --tags
$ git push origin <tagname>

NOTE:  if you want to push all your tags, you can use --tags, eg.  git push origin --tags

NOTE: you can also create branches from tags, eg. git checkout -b <branchname> tagname 

Sample Usage
cd ~/workspace/project/
$ git status
# On branch master
nothing to commit, working directory clean
$ git tag
$ git tag v0
$ git tag
v0
$ vim index.html
$ git add index.html
$ git commit -m "Version 1.0"
[master 830f4e6] Version 1.0
 1 file changed, 2 insertions(+), 1 deletion(-)
$ git tag v1
$ git tag
v0
v1
$ git describe --tags
v1
$ git checkout v0
Note: checking out 'v0'.
 
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
 
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
 
  git checkout -b new_branch_name
 
HEAD is now at 451f2d4... DEVOPS-242 fixed closing tag of footer div
$ git describe --tags
v0

 

 

 

III. Modifying remote branches


  • "git checkout -b <new local branch> <remote branch> , this will create a copy of the remote branch to your local workspace as the "new local branch"

 

GIT command
$ git status
$ git branch -a
$ git checkout -b <local branch> <remote branch>
$ git commit -m "JIRAKEY comment"
$ git push
Sample Usage
cd ~/workspace/project/
git status
# On branch master
nothing to commit, working directory clean
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/cox
  remotes/origin/master
  remotes/origin/release_1-5-1
  remotes/origin/sbm
  remotes/origin/vm
$ git checkout -b release_1-5-1 origin/release_1-5-1
Branch release_1-5-1 set up to track remote branch release_1-5-1 from origin.
Switched to a new branch 'release_1-5-1'
$ vim ravid.test
$ git add ravid.test 
$ git commit -m "JIRA-123 made changes to ravid.test"
[release_1-5-1 bcb194f] made changes to ravid.test
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git push

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://ravidte-owmessaging@bitbucket.org/ravidte-owmessaging/ravid-testfork.git
   a48d7c5..bcb194f  release_1-5-1 -> release_1-5-1

 

 

 

IV. Renaming remote branches


  • "git branch -m <old branch name> <new branch name>  , this will change the local branch name
  • "git remove -v" , this checks which remotes you have (assuming origin for these steps)
  • "git push origin :<remote branch to delete>"  , this will delete the remote branch
  • "git push origin <new branch name>"  , this will push the new branch name to the remote

 

GIT command
$ git banch -m <old branch name> <new branch name>
$ git push origin :<remote branch to delete>
$ git push origin <new branch name>
Sample Usage
git status
# On branch master
nothing to commit, working directory clean
$ git branch -m features feature- 123
$ git remote -v
origin    https: //ravidte-owmessaging@bitbucket.org/ravidte-owmessaging/devops-ci.git (fetch)
origin    https: //ravidte-owmessaging@bitbucket.org/ravidte-owmessaging/devops-ci.git (push)
$ git push origin :features
To https: //ravidte-owmessaging@bitbucket.org/ravidte-owmessaging/devops-ci.git
  - [deleted]         features
$ git push origin feature- 123
Counting objects: 4 , done.
Delta compression using up to 4 threads.
Compressing objects: 100 % ( 2 / 2 ), done.
Writing objects: 100 % ( 3 / 3 ), 315 bytes | 0 bytes/s, done.
Total 3 (delta 0 ), reused 0 (delta 0 )
To https: //ravidte-owmessaging@bitbucket.org/ravidte-owmessaging/devops-ci.git
  * [ new branch]      feature- 123 -> feature- 123

 



V.  Diff changes


 

  • use "git diff" to see what is still unstaged
  • use "git diff -staged" to see what you have staged so far
  • use "git reflog" provides a list of hashes which represent where you have been
  • use "git diff <#commithash> <#commithash>" to compare differences between commit's
GIT command
$ git diff
$ git add <file>
$ git diff --staged
$ git commit -m "JIRAKEY comment"
$ git reflog
$ git diff <#commithash> <#commithash>
Sample Usage
cd ~/workspace/project/
$ git diff
$ vim index.html 
$ git diff
diff --git a/index.html b/index.html
index 957cbfb..5fced4a 100644
--- a/index.html
+++ b/index.html
@@ -14,6 +14,7 @@
 
        This is another line I need to add
 
+        This is a git diff test
 
        <div id=footer> Fix for DEVOPS-242 this is the footer </div>
 </body>
$ git add index.html 
$ git diff --staged
diff --git a/index.html b/index.html
index 957cbfb..5fced4a 100644
--- a/index.html
+++ b/index.html
@@ -14,6 +14,7 @@
 
        This is another line I need to add
 
+        This is a git diff test
 
        <div id=footer> Fix for DEVOPS-242 this is the footer </div>
 </body>
$ git commit -m "DEVOPS-242 this was a diff test"
[detached HEAD c1e21e8] DEVOPS-242 this was a diff test
 1 file changed, 1 insertion(+)
$ git reflog
c1e21e8 HEAD@{0}: commit: DEVOPS-242 this was a diff test
451f2d4 HEAD@{1}: checkout: moving from master to v0
830f4e6 HEAD@{2}: commit: Version 1.0
451f2d4 HEAD@{3}: merge DEVOPS-242: Fast-forward
f59726b HEAD@{4}: checkout: moving from DEVOPS-242 to master
451f2d4 HEAD@{5}: commit: DEVOPS-242 fixed closing tag of footer div
f59726b HEAD@{6}: checkout: moving from master to DEVOPS-242
f59726b HEAD@{7}: commit: DEVOPS-241 added the missing footer
8c31443 HEAD@{8}: commit: DEVOPS-242Added some code in the body
19205aa HEAD@{9}: merge DEVOPS-243: Fast-forward
fa5c22e HEAD@{10}: checkout: moving from DEVOPS-243 to master
19205aa HEAD@{11}: commit: test
fa5c22e HEAD@{12}: checkout: moving from master to DEVOPS-243
fa5c22e HEAD@{13}: commit: Added a test alert to index.html
222e5cc HEAD@{14}: commit (initial): Adding new file index.html
$ git diff 451f2d4 c1e21e8 
diff --git a/index.html b/index.html
index 957cbfb..5fced4a 100644
--- a/index.html
+++ b/index.html
@@ -14,6 +14,7 @@
 
        This is another line I need to add
 
+        This is a git diff test
 
        <div id=footer> Fix for DEVOPS-242 this is the footer </div>
 </body>

git merge <branch_name> 合併另一個 branch,若沒有 conflict 衝突會直接 commit。若需要解決衝突則會再多一個 commit。
git merge --squash <branch_name> 將另一個 branch 的 commit 合併為一筆,特別適合需要做實驗的 fixes bug 或 new feature,最後只留結果。合併完不會幫你先 commit。
git cherry-pick 321d76f 只合併特定其中一個 commit。如果要合併多個,可以加上 -n 指令就不會先幫你 commit,這樣可以多 pick幾個要合併的 commit,最後再 git commit 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值