与混蛋一起在同一条船上:11个高级Git技巧

*“混蛋”是“ git”一词的松散翻译-“一个不愉快或可鄙的人”,“一个不愉快或可鄙的人”。





15 Git . , git , -, , -, - .



, , . , git , , , git rebase --merge --autostash.



1. git



, git? SourceTree, , .



, , vim VS Code, , , .



:





  • .
  • ( ) , git status.
  • .
  • … , / .


git , .



  • , , git , GUI — .
  • " ".




  • . " " , — - . .
  • , . git pull --ff-only — :



    > git pull origin master --ff-only
    From ../habr2
    * branch            master     -> FETCH_HEAD
    error: Your local changes to the following files would be overwritten by merge:
        .gitignore
    Please commit your changes or stash them before you merge.
    Aborting
    Updating 6d1c088..a113bf7


, , , , .



VS Code GitLens .



  • gitlens.diffWithBranch — .
  • - , — . . , , , .


2. , , .



, : (--system), (--global) (--local). , , — , — , — . , .



(Upd. , , worktree. worktree . .)



? , core.eol, , user.name user.email, . , , , (== ) user.name/email.



, , . , user.name/email , , .



3. stash



, , , . , git stash ( . ""), "" . :



  • --amend , stash . git stash save ( save ). — git stash apply ( ) git stash pop ( ). , , , .
  • stash- , , stash, , .
  • stash (modified) (untracked). , , , , , -.


, stash? — WIP ( "Work In Progress"). , , .



stash ? , . , , . , "" , . - - :



git config --global alias.sshow "!f() { git stash show stash^{/$*} -p; }; f"
git config --global alias.sapply "!f() { git stash apply stash^{/$*}; }; f"

# 
git stash save "hack"
# 
git sshow "hack"
# 
git sapply "hack"


4. "-"



, , , "-":



git checkout -


Windows , Linux bash:



cd /some/long/path
...
cd -


5. ,



, — , , ( Unity).



. , . . worktree: . :



$ git worktree add -b emergency-fix ../temp master
$ pushd ../temp
# ... hack hack hack ...
$ git commit -a -m 'emergency fix for boss'
$ popd
$ git worktree remove ../temp


, , .



6. pull fast-forward



, , pull fetch ( ) merge ( ), fast-forward — , "" . , -.



git pull --rebase, , , origin master pull (, , rebase).



, , pull- , --ff-only :



git config --global pull.ff only


?



  • , .
  • , , ( ).
  • , pull — , git pull origin master upstream my_feature.
  • , .


7. git exclude



.gitignore, , .



.git/info/exclude. :



git config --global alias.exclude '!f() { vim .git/info/exclude; }; f'


( .)



  • .git/info/exclude , .gitignore.
  • .
  • , , .gitignore (untracked) . "" . ( IDE, , .vscode/settings.json), git rm <path> --cached — , , exclude.
  • - , :



    git config --global core.excludesfile <path to global .gitignore>


8. , ""



, . : , , . ( , .editorconfig) / (.vscode/tasks.json). , - , "" "" .



: , . :



git update-index --assume-unchanged <path to file>


" " . pull- — , . , , no:



git update-index --no-assume-unchanged <path to file>


9.



. , , , , , . , . , . stash , , .



, . , , — . . ./out — , .



, , , , .git. , gitdir. … git init --separate-git-dir=.git_dev , . : , .



? , ! .git_dev? . , :



git config --global alias.dev '!git --git-dir=\"./.git_dev\"'


:



> git status -s
?? .git_dev/

> git dev status -s
?? .git_dev/
?? .gitignore
?? Program.cs
?? habr.csproj


. , , - checkout , (, , ).



.git/ , , , . — .gitignore , , , -f, — .git_dev/info/exclude. :



# ignore all files
/*
# ignore all folders
*/


, git , .vimrc, .bashrc, ~ ( Windows C:\Users\%USERNAME%\).



10.



, , . git bash Unix-like Windows, , - , . , , :



  • / ;
  • ;
  • .


, - , Hamster, . .git_dev , "" .



11.



最后,我想说的很平庸:自动完成功能可以显着改善生活质量。在大多数Unix系统上,它都是开箱即用的,但是如果您设法在Windows基础结构中找到了自己,我强烈建议您切换到Powershell(如果尚未安装)并安装posh-git,该命令可为大多数命令提供自动完成功能,并在提示符下提供一个简约的摘要:








感谢您的关注; 祝大家工作愉快愉快。



应用

. :



[alias]
    # `git sshow hack` -     ,  "hack".    
    sshow = "!f() { git stash show stash^{/$*} -p; }; f"

    # `git sapply hack` -   "hack"
    sapply = "!f() { git stash apply stash^{/$*}; }; f"

    #   `.git_dev`
    dev = !git --git-dir=\"./.git_dev\"

    #    `.git/`  `.git_dev/` 
    statys = "!f() { git status ; echo \"\n\" ; git dev status ; }; f"

    #     
    findb = "!f(){ git branch -ra | grep $1; }; f"

    #     .    `git hist -n 10`,  10
    hist = log --pretty=format:\"%ad | %h | %an: \t %s%d\" --date=short -n5

    # `git dist branch-name`          branch-name 
    dist = "!git log --pretty=format:\"%ad | %h | %an: \t %s%d\" --date=short \"$(git rev-parse --abbrev-ref HEAD)\" --not "

    #   `exclude`
    exclude = "!f() { vim .git/info/exclude; }; f"

    #   ,   `--assume-unchanged`
    ignored = !git ls-files -v | grep "^[[:lower:]]"

    #     - ,  `git reset HEAD~1`
    forward = "!f() { git log --pretty=oneline --all | grep -B1 `git rev-parse HEAD` | head -n1 | egrep -o '[a-f0-9]{20,}' | xargs git checkout ; }; f"



All Articles