In the same boat as the bastard: 11 advanced Git tips

* "bastard" is a loose translation of the word "git" - "an unpleasant or contemptible person", "an unpleasant or despicable person".





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.



Finally, I want to say a rather banal thing - autocompletion significantly improves the quality of life. On most Unix systems it comes out of the box, but if you have managed to find yourself in the Windows infrastructure, I strongly recommend switching to Powershell (if not already) and installing posh-git , which provides auto-completion for most commands and gives a minimalistic summary in the prompt:








Thank you for attention; I wish you all a pleasant and efficient daily work.



application

. :



[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