git workflow常用命令
来源:互联网 发布:人工智能优缺点 编辑:程序博客网 时间:2024/06/06 01:30
[cabox@ githubtest]$ git remote update Fetching origin Password: remote: Counting objects: 17, done. remote: Compressing objects: 100% (10/10), done. remote: Total 17 (delta 4), reused 16 (delta 3), pack-reused 0 Unpacking objects: 100% (17/17), done. From https://github.com/cnweibo/githubtest 0a2f95b..b0c8d59 kidsitbranch -> origin/kidsitbranch 0a2f95b..4784daf master -> origin/master [cabox@box-codeanywhere githubtest]$ git status -uno # On branch master # Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded. # nothing to commit (use -u to show untracked files)
git branch -f branch-name new-tip-commit :在你reset多次后,你将进入detached mode,你希望将你的branch回退到之前的某个commit,则可以使用这条命令,但是注意你还需要 git checkout branch-name
kidsitbranch 737efba [ahead 3] 3rd change * master 1030dba [ahead 4] go
-u
tells Git to remember the parameters(设置master这个branch的default upstream为originName), so that next time we can simply run git push
and Git will know what to do.)。push到remote的repository以便分享给他人(通过pull操作)。 注意:如果有人同步做了更改,那么这个push将会失败,必需首先git pull(暗含两个操作:一是git fetch,二是从origin/master merge到master)随后再git push。由于这种情况下会在Log中增加merge的历史,会污染这个log,所以更好的方法是用rebase来才做1.
git remote add all git@github.com:akrabat/projectname.git
2.
git remote set-url --add all ssh://example.com/path/to/projectname.git 这样all 这个remote就有了两个url可以来push了。该命令通常可以用于不同staging/production/dev环境的一键更新。注意:当push到不同的repo时,所有历史信息都会保留,但是branch本身只有push过去了,在被push的central repo上才会有该branch!!也就是说本地的branch只有主动push了才能被其他人分享和使用
git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME> 可以在Push remote时指示git将本地branch push到remote上的另外一个branch name, 或者直接 '':remotebranchtobedeleted删除远程repo中的branch
git push origin :testbranch --删除server上的testbranchgit push origin master:production --将本地的master分支push到origin上并且更名为production branch
git push origin :refs/tags/1.7.0 --删除origin所标示的远程repo中的1.7.0这个tag
514fbe7 :注意当前是在master分支时,checkout历史commit,则出现如下情况:命令本身返回一条消息说:we are in a detached HEAD state and that the HEAD is now at
514fbe7. HEAD本身标示了当前checkout的snapshot。这意味着红色圆圈代表了Git的HEAD。HEAD往往存在于tip of a development branch.但是当我们checkout branch的前一个commit时,HEAD移动到了当前branch的中间。我们不能再说我们仍然在当前branch(master)上因为当前branch(tip点)比HEAD包含了更多的snapshot。这由git branch输出来反映:we are currently on no branch!
git config remote.testing.push refs/heads/test:refs/heads/master 修改相关ref ,效果等于: git remote add testing xxxxx;git push testing test:master
git update-index --no-assume-unchanged filename :取消该功能 ;
git ls-files -v|grep '^h' :列出所有被暂时取消track的文件集合 : 注意这种情况下,只对你自己的repo有效,也就是说只在你本地repo有效,其他人clone repo后仍然是track的!!
cabox@box-codeanywhere:~/workspace/gitfattest$ git update-index --assume-unchanged read.txt cabox@box-codeanywhere:~/workspace/gitfattest$ git ls-files -v|grep '^h' h read.txt cabox@box-codeanywhere:~/workspace/gitfattest$ vi read.txt cabox@box-codeanywhere:~/workspace/gitfattest$ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean cabox@box-codeanywhere:~/workspace/gitfattest$ git update-index --no-assume-unchanged read.txt cabox@box-codeanywhere:~/workspace/gitfattest$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: read.txt no changes added to commit (use "git add" and/or "git commit -a")
git rm --cached file :该命令将取消掉对file的track,这种情况下.gitignore中定义的file就将会被GIT彻底忽略。但是存在的问题是:后面被clone的文件中将彻底消失这个file文件!
git stash save --patch 上述命令可以交互式地选择你要stash哪个文件
Hook into Git’s Internals: HOOK是一个当Git在某一个事件发生时执行的脚本。比如只要对central-repo.git的master branch上执行完成了push操作,我们就自动部署到生产环境中去。在central-repo.git目录,打开hooks目录将post-update.sample重命名为post-update,这个post-update就将被git在post完成后自动执行.
#!/bin/sh# Output a friendly messageecho "Publishing master branch!" >&2# Remove the old `my-website` directory (if necessary)rm -rf ../my-website# Create a new `my-website` directorymkdir ../my-website# Archive the `master` branch //注意所有git archive 支持的placeholder都在git help log
under the--pretty-format
section可以看到git archive master --format=tar --output=../my-website.tar# Uncompress the archive into the `my-website` directorytar -xf ../my-website.tar -C ../my-websiteexit 0
git merge feature --squash :在上面的merge中,由于有所谓3-way merge,这个实际上是一个空的commit,会污染history,一个可行的方法是使用--squash选项,这样将简化历史信息
$ git branch --mergediss53 =>说明iss53的fix已经包含,可以删除该branch了* master$ git branch --no-mergedtesting =>说明testing branch未合入,需要适当时候合入。如果这时你想删除,GIT是不允许你这么做的。如果需要强行删除,可以执行:$git branch -D testing
zhenghuz@CV0005366N0 /d/devenv/Code/gitplayground/mary (master)$ git log --graph --left-right --cherry-pick --oneline master...fmary
> e1ad8b2 mary added again to test unmerged //这条也是最新还未merge过的
> e474cbb mary added again //这条是最新还未merge过的< 56490e2 merge fmary again< 20eb4ac squash fjohn 9/10 line merged to master< bc413ae solve merge conflicts for master and fmary< fd3c3a8 john on fjohn add 7th and 8th line< 30952a7 Merge branch 'fmary'
上述命令貌似显示的不是很准确,最好使用下面的一条命令可以一次性列出所有没有被merge到master或者develop分支的commits:
$ git rev-list --all --not master --no-merges | xargs -L1 git name-rev | grep -E '[0-9a-f]{40}'54119bb150f22d720a781adca2305d85c9de9190 developc8bc641b087a24bfce7c9b3b2adadc9d308955e6 remotes/origin/develop
vagrant@homestead:~/Code/kidsit (develop)$ git log --follow bower.jsoncommit b494ada331f7ee042e103ab098077d1178ce2ac0Author: cnweibo <matiascx@163.com>Date: Tue Jul 28 04:22:34 2015 +0000 add angular-timer componentcommit 1b613522447dd16a70bc693dc558eeab511cd725Author: 1372921435 <1372921435@qq.com>Date: Sat Jul 18 09:36:20 2015 -0400 intial non lib version
* 9eb6e93 (HEAD, master) Merge branch 'feature'|\| * 523e2ac (feature) tres| * 6d3cc0f dos| * 1bc0b2e uno* | d39734b three* | 779d37b two* | facbcbf one|/* 58848f4 Initial commit.
但是如果你delete了那个feature分支,则从此不会再展示这个分支了。
* 9eb6e93 (HEAD, master) Merge branch 'feature'|\| * 523e2ac tres| * 6d3cc0f dos| * 1bc0b2e uno* | d39734b three* | 779d37b two* | facbcbf one|/* 58848f4 Initial commit.
原因是在GIT中branch实际上只是一个对最后的commit引用的指针而已,它会随着这个branch上的一个一个commit而不断移动。也就是说一旦这个指针被删除了,那么就将没有内存保存这个branch信息了。那么如何能够找到branch历史呢?有以下几个建议:要么不要删除分支,因为分支本身并没有什么坏处,除非分支太多,屏幕都无法看了;要么在删除branch之前将那个commit做一下Tag, Tag本身实际上就是一个不能移动的branch,你甚至可以将Tag命名为你将删除的branch名称;或者使用GIT系统默认给的merge commit comments,因为在merge时,系统会自动加上是将哪个branch上的commits merge过来的,这样你就可以查看到是从哪个branch上来的。
$ git log -pcommit f67b73e86513d841dd68dd38f4de7327045f620aAuthor: cnweibo <matiascx@163.com>Date: Mon Mar 28 11:21:25 2016 +0800 remove unused tab inactive stylediff --git a/style.less b/style.lessindex 6dd816a..1c42159 100644--- a/style.less+++ b/style.less@@ -609,7 +609,6 @@ input,textarea,select,button{ &.inactive{ color: #666666; background-color: @component-background;- border-bottom: ; box-shadow: 1px 1px 0 #d4d3d3; } }commit 3c88937a145f115002340369216145c5b6d4360bAuthor: cnweibo <matiascx@163.com>Date: Sun Mar 27 17:17:27 2016 +0800 test for archivediff --git a/LAST_COMMIT b/LAST_COMMITindex eb0be20..33416cc 100644--- a/LAST_COMMIT+++ b/LAST_COMMIT@@ -1 +1 @@-$Format:Last commit: %h by %aN at %cd%n%+w(76,6,9)%B$\ No newline at end of file+$Format:Last commit: %d : %h by %aN at %cd%n%+w(76,6,9)%B$\ No newline at end of filecommit 48f8866f2ff43482f10345aa8b42ffea75c6c81fAuthor: cnweibo <matiascx@163.com>Date: Sun Mar 27 16:57:04 2016 +0800 exclude json file in the archivediff --git a/.gitattributes b/.gitattributesindex 79c4bc0..a8e7392 100644--- a/.gitattributes+++ b/.gitattributes@@ -6,6 +6,6 @@ gulp.config.js export-ignore style.less export-ignore .idea export-ignore .git export-ignore-.json export-ignore+*.json export-ignore LAST_COMMIT export-subst\ No newline at end of filecommit 472e2479de544ba38ecea0fc6a43835bc0e4923eAuthor: cnweibo <matiascx@163.com>Date: Sun Mar 27 16:54:42 2016 +0800 add archive.sh script for deploymentdiff --git a/.gitattributes b/.gitattributesindex 75fbf24..79c4bc0 100644--- a/.gitattributes+++ b/.gitattributes@@ -6,4 +6,6 @@ gulp.config.js export-ignore style.less export-ignore .idea export-ignore .git export-ignore+.json export-ignore+ LAST_COMMIT export-subst\ No newline at end of filediff --git a/archive.sh b/archive.shnew file mode 100644index 0000000..d4e3bd6--- /dev/null+++ b/archive.sh
$ git show --pretty="format:" --name-only bd61ad98index.htmljavascript/application.jsjavascript/ie6.js$ git diff-tree --no-commit-id --name-only -r bd61ad98index.htmljavascript/application.jsjavascript/ie6.js
vagrant@homestead:~/Code/kidsit$ git show bea6234 app/Http/Controllers/Admin/AdminUsersController.phpcommit bea6234678079f7d43f3291dedac55e7766cc0f1Author: cnweibo <matiascx@163.com>Date: Mon Jul 20 09:40:16 2015 +0000 admin user update userdiff --git a/app/Http/Controllers/Admin/AdminUsersController.php b/app/Http/Controllers/Admin/AdminUsersController.phpindex 4334ad9..2a948aa 100644--- a/app/Http/Controllers/Admin/AdminUsersController.php+++ b/app/Http/Controllers/Admin/AdminUsersController.php@@ -11,7 +11,9 @@ use Kidsit\Role; use Kidsit\Permission; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\View;-+use Illuminate\Support\Facades\Validator;+use Illuminate\Support\Facades\Input;+use Illuminate\Support\Facades\Redirect; use Datatables; class AdminUsersController extends Controller {@@ -180,7 +182,12 @@ class AdminUsersController extends Controller public function postEdit($user) { // Validate the inputs- $validator = Validator::make(Input::all(), $user->getUpdateRules());+ $validator = Validator::make(Input::all(), array(+ 'username' => 'required|alpha_dash',
vagrant@homestead:~/Code/kidsit (develop)$ git blame -w bower.json^1b61352 (1372921435 2015-07-18 09:36:20 -0400 1) {^1b61352 (1372921435 2015-07-18 09:36:20 -0400 2) "name": "kidsit-fe",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 3) "description": "kidsit front end vendor components",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 4) "version": "0.0.1",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 5) "license": "MIT",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 6) "private": true,^1b61352 (1372921435 2015-07-18 09:36:20 -0400 7) "dependencies": {^1b61352 (1372921435 2015-07-18 09:36:20 -0400 8) "angular": "1.3.x",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 9) "angular-route": "1.3.x",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 10) "angular-loader": "1.3.x",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 11) "angular-mocks": "~1.3.x",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 12) "angular-animate": "~1.3.3",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 13) "jquery": "~2.1.1",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 14) "jquery-colorbox": "~1.5.14",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 15) "angular-bootstrap": "~0.12.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 16) "jquery-color": "~2.1.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 17) "bootstrap": "~3.3.1",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 18) "angular-toastr": "~0.5.1",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 19) "angular-busy": "~4.1.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 20) "ng-pageslide": "~0.1.8",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 21) "PACE": "https://github.com/HubSpot/pace.git#~1.0.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 22) "highcharts": "http://code.highcharts.com/highcharts.js",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 23) "highcharts-ng": "~0.0.7",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 24) "underscore": "~1.7.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 25) "angular-underscore": "~0.5.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 26) "bootstrap-hover-dropdown": "~2.0.11",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 27) "angular-filter": "~0.5.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 28) "angular-validate-directive": "~0.1.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 29) "angular-messages": "1.3.3",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 30) "ui-router": "~0.2.13",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 31) "html5shiv": "^3.7.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 32) "respondJs": "~1.4.2",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 33) "jquery-placeholder": "~2.1.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 34) "greensock": "~1.17.0",^1b61352 (1372921435 2015-07-18 09:36:20 -0400 35) "code-prettify": "*",b494ada3 (cnweibo 2015-07-28 04:22:34 +0000 36) "bootstrap-wysihtml5": "*",b494ada3 (cnweibo 2015-07-28 04:22:34 +0000 37) "datatables": "~1.10.7",b494ada3 (cnweibo 2015-07-28 04:22:34 +0000 38) "angular-timer": "~1.3.3"^1b61352 (1372921435 2015-07-18 09:36:20 -0400 39) }^1b61352 (1372921435 2015-07-18 09:36:20 -0400 40) }
(master)$ git show v1.2 commit 7bddbdc2a1f8d9c23205707e74455d74684e3031 Merge: c822db1 3f63840 Author: Taylor Otwell <taylorotwell@gmail.com> Date: Tue Mar 24 16:06:56 2015 -0500 Merge pull request #3318 from TheShiftExchange/patch-1 Update services.php
(master)$ git describe --all e01c173 tags/v5.0.0-1-ge01c173
$ git describe --tags HEADv1.0-1-g3c88937 //注意这里的意思是离HEAD最近的是前1个commit打了V1.0的tag!!Administrator@USER-20151001BU MINGW64 ~/devenvironment/Code/fedevbp (master)$ git lg* 3c88937 (HEAD -> master) test for archive* 48f8866 (tag: v1.0) exclude json file in the archive
cabox@box-codeanywhere:~/workspace/gitfattest$ git reflog 91fb73d HEAD@{0}: clone: from https://github.com/cnweibo/gitfattest.git
git reflog也列出所有你已经remove掉的commit。注意:git reflog对于branch也有记录
cabox@box-codeanywhere:~/workspace/kidsit$ git reflog develop 6ea08d0 develop@{0}: branch: Created from refs/remotes/origin/develop
$ git reflog9fbb460 HEAD@{0}: reset: moving to HEAD^1ecfc3dd HEAD@{1}: reset: moving to HEAD^1c62a928 HEAD@{2}: commit: add yinbiao/showecfc3dd HEAD@{3}: commit (merge): strange files9fbb460 HEAD@{4}: commit: some strange file deletedd7ba3b7 HEAD@{5}: commit: migrations added6d89fa2 HEAD@{6}: commit: custom.js456d2e7 HEAD@{7}: commit: add custom.js in resourcecd4f2be HEAD@{8}: pull: Fast-forward8b72bf8 HEAD@{9}: commit: update gulpfile38afa34 HEAD@{10}: pull: Merge made by the 'recursive' strategy.4a22d6f HEAD@{11}: commit: add some trace script044456b HEAD@{12}: pull: Fast-forward4744f4a HEAD@{13}: commit: let contact-us work with cn languagec5e78bf HEAD@{14}: commit: add contact-us page for test4783e3d HEAD@{15}: checkout: moving from master to master4783e3d HEAD@{21}: checkout: moving from master to develop4783e3d HEAD@{22}: clone: from https://github.com/yyyy/xxxxx.gitzhenghuz@CV0005366N0 /d/devenv/code/xxxx (master)$ git reset --hard c62a928Checking out files: 100% (2247/2247), done.HEAD is now at c62a928 add yinbiao/show
Tracking branches are local branches that have a direct relationship to a remote
branch. If you’re on a tracking branch and type git pull, Git automatically
knows which server to fetch from and branch to merge into.
$ git branch -vviss53 7e424c3 [origin/iss53: ahead 2] forgot the bracketsmaster 1ae2a45 [origin/master] deploying index fix* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do ittesting 5ea463a trying something new
上述命令显示我们的local branch iss53实际上tracking了origin/iss53,并且本地有2个commit没有push,local master branch tracking了origin/master分支并且是up-to-date状态;serverfix分支tracking了teamone/server-fix-good分支,并且本地有了3个commit没有push,同时server上有1个commit我们没有fetch;testing是纯粹的local branch. 注意:本命令看到的是最近一次fetch后的结果,它本身并不会主动和remote联络更新信息。因此最好的使用方式是: git fetch --all; git branch -vv -a
$ git remote show origin * remote origin Fetch URL: https://github.com/cnweibo/githubtest.git Push URL: https://github.com/cnweibo/githubtest.git HEAD branch: master Remote branches: kidsitbranch tracked master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (local out of date)
注意:在上面的输出信息中,其中 HEAD branch: master 指示你的remote origin它的默认分支是master,这样你一旦从那个repo clone过来时,git自动创建一个local master to track that remote repo master branch!!!
git branch --track branch-name origin/branch-name,以便将local的branch-name指向origin/branch-name并且track它。可以使用git remote show origin来查看是否有local branch track这个remote了。该命令和下面的命令可能等效。
git branch --set-upstream local-branch-name origin/remote-branch-name、
git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name] 上面几个命令都创建了能够track remote的branch。所谓track是指在local branch上git pull,git就知道从哪个remote上去取并且merge到哪个local branch上,这时你可以通过git remote show命令来显示详细信息
);git rebase;这个动作有三个小步骤:(首先move all changes to master which are not in origin/master(别人已经push的改动)to a temprary area,随后run all origin/master commits on the master branch;最后执行暂存于临时区域的所有commit)
在.git/config中[branch "master"] remote = origin merge = refs/heads/master rebase = true或者全局性地:~/.gitconfig[branch] autosetuprebase = always
rebase local changes
you’ve made but haven’t shared yet before you push them in order to clean up
your story, but never rebase anything you’ve pushed somewhere
Check out the client branch, figure out the patches from
the common ancestor of the client and server branches, and then replay
them onto master git rebase master server :replay server work on top of master
git ls-remote --heads upstream :仅列出upstream repo中所有的branch
$ git ls-remote upstream4783e3d9a4d7e9c7ea117b4277b859674651dbb4 HEAD4783e3d9a4d7e9c7ea117b4277b859674651dbb4 refs/heads/develop4783e3d9a4d7e9c7ea117b4277b859674651dbb4 refs/heads/f_migration4783e3d9a4d7e9c7ea117b4277b859674651dbb4 refs/heads/master98aa6d8c2801c9639db39466f12804285eda6eec refs/tags/v0.0.1
git check-ignore -v . 查看本文件夹是由哪个gitignore rule而导致被忽略掉的
vagrant@homestead:~/code/kidsit/resources/assets/bower_components$ git check-ignore -v ..gitignore:7:resources/assets/bower_components/ .
vagrant@homestead:~/Code/kidsit$ git fetch --allFetching originFrom https://github.com/cnweibo/kidsit * [new branch] develop -> origin/develop * [new branch] f_migration -> origin/f_migration$ git branch -vv -a* master 4783e3d [origin/master] initial runnable version based on laravel5.1 remotes/origin/HEAD -> origin/master remotes/origin/develop 4783e3d initial runnable version based on laravel5.1 remotes/origin/f_migration 4783e3d initial runnable version based on laravel5.1 remotes/origin/master 4783e3d initial runnable version based on laravel5.1
vagrant@homestead:~/code/codeanywhereconsoleenv (master)$ git ls-files.bash_profile.bashrc.gitconfiggit-aware-prompt/LICENSEgit-aware-prompt/README.mdgit-aware-prompt/colors.shgit-aware-prompt/main.shgit-aware-prompt/preview.pnggit-aware-prompt/prompt.sh
vagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git ls-files -m :只列出working directory和index不同的文件(也就是修改过的) -d:列出删除了的
.gitconfig
vagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git ls-files -s : -s参数将输出blob的sha100644 cbd26e0815ae1f66f6658281e93f1e0be6eb579c 0 .bash_profile100644 71d2312c4ec072698a905cf94e8ee480b7bdb589 0 .bashrc100644 fab46aec849e85527d018878d878f2cd82ecd439 0 .gitconfig100644 3bbbc1ee92562e6a2eebfc6a366f4309d06c7d54 0 git-aware-prompt/LICENSE100644 f4f240652a9ec7d698554e0fa81465b885389453 0 git-aware-prompt/README.md100644 448ad10727d1a84dd6e2572c033b42f1e775f847 0 git-aware-prompt/colors.sh100644 620d7ed172b4dc64226fb60f6d542c63d7429cd5 0 git-aware-prompt/main.sh100644 2bafa65cb58464475bcc9861547ad679e1159a71 0 git-aware-prompt/preview.png100644 6a737b848df0a8bd52a1eb0a7cb4c67b71e4b258 0 git-aware-prompt/prompt.shvagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git cat-file blob fab46aec849e85527d018878d878f2cd82ecd439
vagrant@homestead:~/Code/kidsit (develop)$ git log --diff-filter=A -- public/favicon.icocommit 1b613522447dd16a70bc693dc558eeab511cd725Author: 1372921435 <1372921435@qq.com>Date: Sat Jul 18 09:36:20 2015 -0400 intial non lib version
git vagrant@homestead:~/Code/kidsit (develop)$ git log -- public/favicon.icocommit 60ba5a064962db5fa05aa0bbe20038c4fef47677Author: cnweibo <matiascx@163.com>Date: Fri Jul 31 04:24:57 2015 +0000 admin grade primary workcommit 1b613522447dd16a70bc693dc558eeab511cd725Author: 1372921435 <1372921435@qq.com>Date: Sat Jul 18 09:36:20 2015 -0400 intial non lib version
vagrant@homestead:~/Code/kidsit (develop)$ git show 60ba5a064962db5fa05aa0bbe20038c4fef47677:public/favicon.ico #$# git-fat da39a3ee5e6b4b0d3255bfef95601890afd80709 0
git checkout commitSHA file/to/restore
vagrant@homestead:~/Code/kidsit (detached*)*$ git diff --name-only --diff-filter=Upublic/favicon.ico
$ git checkout --track origin/developM vendor/mockery/mockery/.styleci.ymlM vendor/monolog/monolog/.php_csM vendor/psy/psysh/.styleci.ymlM vendor/swiftmailer/swiftmailer/tests/_samples/charsets/iso-2022-jp/one.txtBranch develop set up to track remote branch develop from origin.Switched to a new branch 'develop'vagrant@homestead:~/Code/kidsit$ git checkout -- .
常见问题解决方法:
出现一大片可能不是你希望看到的“modified”,比如:
modified: vendor/zizaco/entrust/src/Entrust/Traits/EntrustUserTrait.php modified: vendor/zizaco/entrust/src/commands/MigrationCommand.php modified: vendor/zizaco/entrust/src/config/config.php modified: vendor/zizaco/entrust/src/views/generators/migration.blade.phpno changes added to commit (use "git add" and/or "git commit -a")vagrant@homestead:~/code/kidsit$ git diff app/Console/Commands/Inspire.phpdiff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.phpold mode 100644new mode 100755
这些奇怪的变动,可能是由Unix文件权限模式的变更,老的模式包含+x,新的模式不含这个x,
That looks like unix file permissions modes to me (755
=rwxrw_rw_
, 644
=rw_r__r__
) - the old mode included the +x (executable) flag, the new mode doesn't.
This msysgit issue's replies suggests setting core.filemode to false in order to get rid of the issue:
解决的方法就是:
git config core.filemode false
git status出现一大片不是你希望看到的“改动”
可能的原因:当你在多个操作系统中协同使用git来工作的时候,由于不同OS对于回车键的处理有不同,所以有可能在你做git status时出现大片由于enter键区别导致的改动。解决方法就是:
git config --global core.autocrlf true
偶尔出现明明是你新install的component(比如使用composer require),当你git status时,git却总是说Nothing to add
这个可能的原因是:要么该目录被.gitignore了,你可以使用git check-ignore -v directory来检查,也可能是由于第三方库配置了submodule的缘故。比如我在require一个bllim/datatables时,就出现这种怪异现象。
cabox@box-codeanywhere:~/workspace/temp/tmp/vendor$ git check-ignore bllim/datatables/README.md fatal: Pathspec 'bllim/datatables/README.md' is in submodule 'bllim/datatables'
解决办法: 1,删除.git目录(我不需要submodule),2,git rm --cached directory 3.
git add directory
这时,再执行git status, git就能发现vendors/bllim/datatables了!
- 如何一次性删除所有untracked file??
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)*$ git clean -d --dry-run
Would remove app/Http/Controllers/Admin/AdminMathskillcatController.php
Would remove public/htmlapp/math/
Would remove public/htmlapp/system/
Would remove public/preparebuild/assets/libs/angular-ui-event/
Would remove public/preparebuild/assets/libs/angular-ui-indeterminate/
Would remove public/preparebuild/assets/libs/angular-ui-mask/
Would remove public/preparebuild/assets/libs/angular-ui-scroll/
Would remove public/preparebuild/assets/libs/angular-ui-scrollpoint/
Would remove public/preparebuild/assets/libs/angular-ui-uploader/
Would remove public/preparebuild/assets/libs/angular-ui-utils/
Would remove public/preparebuild/assets/libs/angular-ui-validate/
Would remove public/preparebuild/assets/libs/angular-xeditable/
Would remove resources/views/admin/mathskillcats/
Would remove resources/views/admin/partials/csrf_token.blade.php
Would remove resources/views/admin/partials/indicatorcontainer.blade.phpgit clean -d -fRemoving public/htmlapp/math/Removing public/htmlapp/system/Removing public/preparebuild/assets/libs/angular-ui-event/Removing public/preparebuild/assets/libs/angular-ui-indeterminate/Removing public/preparebuild/assets/libs/angular-ui-mask/Removing public/preparebuild/assets/libs/angular-ui-scroll/Removing public/preparebuild/assets/libs/angular-ui-scrollpoint/Removing public/preparebuild/assets/libs/angular-ui-uploader/Removing public/preparebuild/assets/libs/angular-ui-utils/Removing public/preparebuild/assets/libs/angular-ui-validate/Removing public/preparebuild/assets/libs/angular-xeditable/Removing resources/views/admin/mathskillcats/
如何一次性清除所有的改动?包括unstaged(modified),untracked? 随后也就将git flow中创建的新feature delete掉?一种可行的方法是:
git stash save --keep-index 和git stash drop
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)*$ git stash save --keep-index Saved working directory and index state WIP on feature/mathskillcatmanage: 417f2ec printmath.cssHEAD is now at 417f2ec printmath.cssvagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git statusOn branch feature/mathskillcatmanagenothing to commit, working directory cleanvagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git stash liststash@{0}: WIP on feature/mathskillcatmanage: 417f2ec printmath.cssvagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git stash dropDropped refs/stash@{0} (a5df6ceb236bd864cf182cca3685559a3406ce27)vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git statusOn branch feature/mathskillcatmanagenothing to commit, working directory cleanvagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git flow feature delete mathskillcatmanageSwitched to branch 'develop'Deleted branch feature/mathskillcatmanage (was 417f2ec).Summary of actions:- Feature branch 'feature/mathskillcatmanage' has been deleted.- You are now on branch 'develop'
- 如果你使用git fat来管理你的大文件时,当clone一个repo后第一件事儿你可能需要git fat init初始化git fat meta数据,随后你需要执行git fat pull操作,随后如果你执行git pull --rebase这个动作时,你就可能会因为变更了你的repo中的fat文件(即便是file mode之类的变更!),所以当执行git pull --rebase时出现unstaged files, can not commit,而同时奇怪的是当你git status时又无任何输出。这时将会是一团雾水。你可以执行git diff-files来找到git 认为的曾经被变更的文件集,给你一些解决问题的思路。之所以想到这个主意是因为发现git rebase实际上是
/usr/libexec/git-core/git-rebase这个shell脚本,
搜索"have unstaged changes"发现只有以下地方有打印:require_clean_work_tree () { git rev-parse --verify HEAD >/dev/null || exit 1 git update-index -q --ignore-submodules --refresh err=0 if ! git diff-files --quiet --ignore-submodules then echo >&2 "Cannot $1: You have unstaged changes." err=1 fi[ ... ]
所以从上面的代码就可以看出一定是git diff-files命令有文件输出故而才会打印have unstaged changes
cabox@box-codeanywhere:~/workspace/kidsit$ git pull --rebase Cannot pull with rebase: You have unstaged changes. Please commit or stash them.
cabox@box-codeanywhere:~/workspace/ktest/kidsit$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory cleanabox@box-codeanywhere:~/workspace/kidsit$ git diff-files :100644 100644 565a0e6b45a0fde1ccd1f39ee2bc6b0ba81ac0b0 0000000000000000000000000000000000000000 M storage/uploaded/yinbiaomp3/1409927256_10.mp3 :100644 100644 a08e9d3b68da37f2e233d0d103e2a3a90405ab3e 0000000000000000000000000000000000000000 M storage/uploaded/yinbiaomp3/1409927276_10.mp3 :100644 100644 baad2e2763847dded441f59776b7e9a544f0cea8 0000000000000000000000000000000000000000 M storage/uploaded/yinbiaomp3/1409927297_5.mp3 :100644 100644 a694caba87b3b4d4a57f9070a9d983e368ad8a14 0000000000000000000000000000000000000000 M storage/uploaded/yinbiaomp3/1409927315_8.mp3 cabox@box-codeanywhere:~/workspace/kidsit$ git diff storage/uploaded/yinbiaomp3/1409927276_10.mp3 warning: LF will be replaced by CRLF in storage/uploaded/yinbiaomp3/1409927276_10.mp3. The file will have its original line endings in your working directory. diff --git a/storage/uploaded/yinbiaomp3/1409927276_10.mp3 b/storage/uploaded/yinbiaomp3/1409927276_10.mp3 index a08e9d3..373f7b6 100644 --- a/storage/uploaded/yinbiaomp3/1409927276_10.mp3 +++ b/storage/uploaded/yinbiaomp3/1409927276_10.mp3 @@ -1 +1 @@ -#$# git-fat c3ea5a9a54d0809b60de406d4951905b9c6625bc 9040 +#$# git-fat d20e15ce5c6b8ecd2fd2f0d2fd2a498dddfee7d8 75
- 如何实现当git push origin master时,自动触发deployment过程?有什么好的策略吗?一般来说,github是一个非常好用的central repo hosting service。在github中保存着项目的所有历史信息,master branch作为稳定可靠的可以deployment的分支。在depolyment server上一般也会从github上clone一个repo,当我们在自己的开发主机上完成编码和测试工作后,打一个tag,git push origin master将会把所有工作上传到github中分享,这时我们如何自动触发production server来部署呢?一种方法是登陆到production server上,随后git pull,git archive, copy到我们的部署目录中去;另外一种可行的方法是:将部署服务器上被clone的repo作为我们开发主机repo的一个remote,我们push到github上面的同时,push到deployment server上去,同时在deployment server上利用git的server侧hook机制,触发git archive,并且部署到相关目录中。具体方法是:git push gituser@productionserverIP:/directory/to/deployed/repo/ master:production 注意这个命令中master是开发主机的branch,production是depoyment这个机器的branch。一般地,在开发流程中,我们还应该再创建一个remote repo作为staging的环境,这样我们在开发过程中,一旦develop上的开发工作有了阶段性的成果,会拉出一个pre-release的分支,打上标签,push到staging主机上去,供QA测试
- 如何将已经tracked文件从git tracking中删除(并不删除文件)?
# Do this on all machinesecho "FILE_NAME" >> .gitignoregit rm --cached FILE_NAMEgit add -ugit commit -m "removing files from version control"# Sync with your git server, pull to sync and push to register your local changegit pullgit push
- 如何搭建自己的git server?以下资源可供参考:
https://git-scm.com/book/it/v2/Git-on-the-Server-Setting-Up-the-Server
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000
https://github.com/progit/progit/blob/master/zh/04-git-server/01-chapter4.markdown
http://www.centoscn.com/CentosServer/ftp/2014/0414/2789.html
http://freeloda.blog.51cto.com/2033581/1410562
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-private-git-server-on-a-vps
https://www.linux.com/learn/tutorials/824358-how-to-run-your-own-git-server
http://www.linux-magazine.com/Online/Features/Install-Your-Own-Git-Server
http://blog.csdn.net/csfreebird/article/details/7204345
http://wiki.ubuntu.org.cn/Git%E6%9C%8D%E5%8A%A1%E5%99%A8Gitosis%E5%AE%89%E8%A3%85%E8%AE%BE%E7%BD%AE
- 为了确保master分支的稳定,必须对master分支做好控制(甚至develop分支也要控制),那如何控制向master分支的push操作呢?如果使用Github,最好的策略是: 除了原始master repo分配给deployment manager外,先fork出一个staging repo分配给system integration manager,每一个开发人员都从这个staging repository(从master repo fork来的)(注意:如果有大的feature team,则可以再增加一级:feature team leader fork一个repo作为整个team协同工作的repo)来fork出自己的repo,每一个人都在本地开发后push到自己fork出来的自己有写权限的repo,开发人员在自己的repo中开发测试完毕后,pull request提交到staging repo,这时system integration manager召集专家和相关team member来做review(如果可以fast forward的,则无需review),随后确认接受后就放到staging repo的develop分支,正式宣布接受开发人员完成的一个feature变更。一旦有feature落地,则应该触发QA过程,QA过程中发现的问题提交bug给team leader。具体怎么知道应该给哪些team leader呢?QA人员应该使用git bisxx功能,给出问题是一直存在的还是后来引入的,如果是新代码引入的,那么是哪个feature落地带来的呢?依据这些信息就能找到应该找谁来进一步处理。如上面所说,如果feature较大,需要至少2个人人以上来开发,那么就需要指定feature team leader,同时将staging repo的collabarator为每个team leader开放权限?team leader可以往staging repo的dev下有Push权限。QA完成后,可以打tag,由集成测试经理执行merge到master的动作,并且向master repo发pull request。最后由信息集成官确认ok,部署代码,负责运维。
- 如果在上面描述的工作流中,我们不使用github,而是自建git server,则可以在服务端通过hook来实现这个权限的控制。
- 如何既能在feature分支上保留详细的历史信息又能在master分支上只保持一个一个feature粒度的commit?
在feature开发中,我们鼓励及时commit,这个时候变更管理的粒度是很小的。但是对于一个产品级别的管理者来说,他们可能只关心一个个feature级别的变更,因此当需要将feature落地到master/develop分支上时,我们可以通过下面的策略来执行(先在featureX分支上和master merge并且测试完成,结果Ok后,再在master分支上做git merge --squash完成代码落地):
git checkout featureX
git merge master //首先在featureX分支上将featureX分支和master分支做merge并且做好测试工作,之后再在master分支上执行git merge --squash featureX只保留一个历史
git checkout mastergit merge --squash featureX//通过这条命令将featureX的所有commit都合成一条放到master分支
对于小的feature,最好使用git pull --rebase, 大的feature,最好使用git merge --no-ff这样保留一个commit节点能够描绘出merge这个重要动作。
或者说: 本地开发 相同的分支总是 git pull --rebase(比如master,develop分支总是这个策略), 在本地合并分支时总是 git checkout master, 对于小的feature,则git merge --squash featureX :主要作用是featureX的历史细节只在local repo中保留,中央库中只保留feature级别的commit;对于大的feature我们则使用git checkout master, git merge featureX --no-ff 这样的好处是既能保留大feature的历史信息,又能使official库能大体看到版本变迁历史的大粒度视图;还有也可以对featureX的commit使用git rebase -i 来重写历史梳理干净
git merge --squash featureX
Hosted GIT repository: Github, BitBucket;
Selft Managed: Gitosis, Gitorious
- git clone --recursive git://github.com/nvie/gitflow.git :使用--recursive参数会自动将repo中的submodules一起clone下来
- 如何免密码push远程repo? a)创建ssh私钥和公钥,将公钥放到github/codingnet的配置页面上去;b)将remote repo的connection中的url修改为ssh方式的, 比如git@git.coding.net:xxxx/yyyy.git
当git checkout到不同的branch时,相应目录看到的东西是不一样的。这个是文件系统的功能,比较神奇啊
git checkout -- fileToDropChange 如果有文件被修改但是又还没有staging,上述命令则彻底丢弃local的变更;git checkout -- . 丢弃当前目录中所有的local change(从index区域获取fileToDropChange的内容覆盖工作区)
http://eagain.net/articles/git-for-computer-scientists/ :Git internal
http://www.vogella.com/tutorials/Git/article.html#gitremotebranch_overview Git commands
http://blog.csdn.net/ithomer/article/details/7529841
如何找回偶然被删除的commit?
1. get reflog列出所有操作历史;
2. 找到对应的commit,创建一个branch : git checkout -b urgentbranch xxxcommit
3. git co dev/git merge urgentbranch
以上三步就能恢复你想要的历史commit了!
如何强制pull远端的branch?
有时候一个branch发生冲突,我们已经解决并且push了,这时可能希望强制pull成远端分支的内容,解决办法:
1. git fetch --all //获取所有远端内容
2. git co yourbranch
3. git reset --hard origin/yourbranch
上述三步解决问题!
如何列出两个branch之间的commits区别?
git show-branch stock-information staging/master master :列出两个分支之间的详细区别列表
! [stock-information] WIP: Link to data series ! [staging/master] Add a description to Stock ! [master] Display Stocks---+ [stock-information] WIP: Link to data series+ [stock-information~1] Create DataSeries for Stocks.++ [staging/master] Add a description to Stock++ [staging/master~1] Import external Stock information+++ [master] Display Stocks
git log --oneline --no-merges feature/xx..dev 列出现在的从feature/xx分支到dev分支之间的未被merged的commits列表(一般的fast forward merge commit不会列出来!),也就是说在dev上但是却未能出现在feature/xx分支上的所有commit
解读:
http://openwares.net/linux/git_show_branch_output.html
输出分为上下两部分,使用短划线”-“分隔。两个分支使用两个短划线”–“,三个分支使用三个短划线”—“,依次类推。
上半部分为层次缩进的分支列表,下半部分为commit列表。
上半部分的分支列表中,使用*标识当前分支,其他分支使用!标识。分支前的标识符*或者!一直垂直贯通到下半部分,这一垂直列的符号都是属于这个分支的。
下半部分的commit列表中,前导的符号有*和+号。*表示这一列上的分支(当前分支)有此commit。而+表示这一列上的分支(非当前分支)有此commit。
标识符的颜色只是用于容易区分列,一列的颜色是一致的,看起来更清楚。
- git workflow常用命令
- Git Workflow
- Git workflow
- Git workflow
- Git Workflow
- Understanding the Git Workflow
- Git Workflow工作流示意图
- Git workflow 选型分析
- Git Workflow最佳实践
- Git/Github工作流 (workflow)
- git简单个人工作流(workflow)
- 学习Git--lesson 1_Fundamental Git workflow
- git 常用命令
- Git常用命令
- git 常用命令
- git 常用命令
- git 常用命令
- git 常用命令
- sed: -e expression #1, unknown option to `s'解决办法
- 开发人员菜鸟实用设计资源
- 机房重构之存储过程
- 使用Less color函数创建专业网站配色方案
- JDBC学习入门
- git workflow常用命令
- github概念和实战
- Spring事务传播机制和数据库隔离级别
- Unity 可用预处理表
- 剑指offer-数组中只出现一次的数
- design pattern及其使用
- 王道霸道
- layer-list -- layer-list的基本使用介绍
- website architecture