git常用命令记录

git常用命令记录

        • 提交代码
        • 查看历史提交记录
        • git根据commit查看信息
        • 远程拉取代码到本地
        • branch操作

  • 查看远程仓库地址命令
    git remote -v

提交代码

  • 提交代码到本地
    git add .
  • 查看提交记录
    $ git status -s
A  README
A  hello.php
  • 代码提交
    $ git commit -m '第一次版本提交'

查看历史提交记录

  • $ git log
    显示
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon 
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test code

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon 
Date:   Sat Mar 15 10:31:28 2008 -0700
  • 显示指定文件相关的每一次diff

$ git log -p [file]

 $ git log -p eBayMobile/src/main/res/values/strings.xml
commit 7eb2b4822e9ff2b4ca619913a521d4ea0ac8be92
Author: Darren Hinderer 
Date:   Wed Dec 2 11:12:45 2015 -0800

    SELLING-2927 Fix status of refunded items

diff --git a/eBayMobile/src/main/res/values/strings.xml b/eBayMobile/src/main/res/values/strings.xml
index 9734c34bc7f..3a635c39c6d 100644
--- a/eBayMobile/src/main/res/values/strings.xml
+++ b/eBayMobile/src/main/res/values/strings.xml
@@ -1338,6 +1338,7 @@
        PAID - SHIP NOW
        SHIPPED
        RELISTED
+       REFUNDED
        You don\'t have any drafts.
        You don\'t have any active items.
        You don\'t have any items with new offers.
  • 查看树状提交记录
    $ git log --pretty=format:"%h %s" --graph
gerTests
|\  
| * 706cdf99abb [FIXIT-CacheManager]: CacheManager Unit Testing Fixes
|/  
*   62f53fae033 Merge pull request #2364 from eBayMobile/mcw/viewitem/coverage
|\  
| * 69af7384b4e Fixed odd quotes on keys as well
| * 2c04bcee9a4 Fixed odd quotes that were inserted
| * fcf8a24373c Added obvious view item owned files to coverage map
* |   aecbfd9e2e1 Merge pull request #2372 from eBayMobile/jesstewart/l10n/pluralfix
|\ \  
| * | 580d8d65321 ImpliedQuantity lint rule modified to warning for L10N Automation
| |/  
* |   d81308d76b2 Merge pull request #2311 from eBayMobile/sangrao/common/imageR
  • gitk 的图形界面

git根据commit查看信息

git commit commitId

git commit commitId^!

$ git show 037393380ef7191cfd9df7213249e6abb1d1ed13
commit 037393380ef7191cfd9df7213249e6abb1d1ed13 (HEAD -> 2823, origin/2823, checkout)
Merge: 285962d35b7 28f1e48675f
Author: Cumings, Mike(mcumings) 
Date:   Fri Jan 11 09:19:50 2019 -0800

    Merge pull request #16533 from eBayMobile/mcumings/ntvarch/7501
    
    NTVARCH-7501 Prevent coverage report being skipped for directive runs

远程拉取代码到本地

有两种,一种git fetch, 一种git pull

  • git fetch
    手动指定了要fetch的remote。在不指定分支时通常默认为maste
    git merge
    合并分支到本地的分支
  • git pull
    与git pull相比git fetch相当于是从远程获取最新版本到本地,但不会自动merge。如果需要有选择的合并git fetch是更好的选择。效果相同时git pull将更为快捷。
  • 放弃本地修改,强制复制本地代码
    $ git fetch --all
    $ git reset --hard origin/master
    $ git pull

branch操作

  • 新建一个分支,指向指定commit
    $ git branch [branch] [commit]
  • 切换到上一个分支
    $ git checkout -
  • 查看所有分支,并按时间排序
    git branch -a --sort=committerdate
    git branch -a 查看所有branch
    –sort= 排序方法
$ git branch -a --sort=committerdate
  remotes/origin/experiment_2.6
  remotes/origin/WRONG_BRANCH
  remotes/origin/my_ebay_android_2.6
  remotes/origin/appcompat_android_2.6
  remotes/origin/android_2.5
  remotes/origin/btd.hack_android_2.6
  remotes/origin/hack/2013/raman_vespucci
  remotes/origin/hackathon_vipswipe_android_2.6
  remotes/origin/slim_wiccans_android_2.6
  remotes/origin/hack/2013/metal
  remotes/origin/jlc_android_2.6_hackathon_KEEP
  remotes/origin/hack/2013/chromecast
  remotes/origin/gws_hackathon_android_2.6
  remotes/origin/currency_android_2.6
  remotes/origin/bjy_qeci_imac_android_2.7
  remotes/origin/post_format_android_2.6
  remotes/origin/post_vip_dm_bugs_android_2.6
  remotes/origin/bsf_android_2.6
  remotes/origin/gm_android_2.6
  remotes/origin/bjy_android_2.6
  remotes/origin/rdr_android_2.6
  remotes/origin/dlh/apphance_2.6
  remotes/origin/mcw_andr_13463
  • 比较两个分支不同
    git diff branchName1 branchName2
$ git diff WRONG_BRANCH master
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 00000000000..819f1da5cf0
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,23 @@
+# Set default behaviour, in case users don't have core.autocrlf set.
+* text=auto
+# Explicitly declare text files we want to always be normalized and converted 
+# to native line endings on checkout.
+*.c text eol=lf
+*.h text eol=lf
+*.java text eol=lf
+*.xml text eol=lf
+*.json text eol=lf
+*.js text eol=lf
+*.html text eol=lf
+*.properties text eol=lf
+*.txt text eol=lf
+# Declare files that will always have CRLF line endings on checkout.
+#*.sln text eol=crlf
+# Denote all files that are truly binary and should not be modified.
+*.png binary
  • 比较不同分支某个文件的不同
    git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9:filename b8e7b00c02b95b320f14b625663fdecf2d63e74c:filename
    另一种方法直接比较不同分支文件
    git diff branch1 branch2

你可能感兴趣的:(小工具,程序开发,git,github,java)