15 Essential Git Tips to Work Effectively Every Day

Hi, my name is Sergeyev Sergey aka gurugray... Now I am “Mentor FrontEnd Community” at ManyChat. You could have seen my lectures on the release cycle and the rules for working with version control systems at the Yandex School of Interface Development (SRI).



I am often asked what life-hacks or best-practices I use when working with Git and project repositories.



This post is an attempt to explain the basic settings and techniques I use every day. Recipes do not pretend to be know-how, but can help with mastering the daily hygiene of working with the repository.





1. Use fresh



I prefer to work in the console and most of the commands and tips in this post will be about the console client. This is kind of a first recommendation - use the console client for your daily work with the repository and update it regularly. The console client primarily introduces new features and bug fixes.



> git --version
git version 2.27.0


2. Tune the instrument



Set up yourself a public email that is not affiliated with the current company. This will allow you to fine-tune identification for different repositories and not think about the default settings, and GitHub will show your activity even if you change your place of work.



> git config --global user.email "gurugray@yandex.ru"

> cd company_project
> git config user.email "gurugray@manychat.com"


vim' , :



> git config --global core.editor "code --wait"


3. autocomplete



, . , .



Git , shell, , — .



4.



(aliases) . - , ssh — , ( , ). — , , .



Git SVN, , "b" — .



> git config --global alias.st "status -sb"
> git config --global alias.co "checkout"
> git config --global alias.ci "commit -a"
> git config --global alias.b "branch"


5.



— , , . . Git :



> git log --oneline --graph --branches


, , --pretty=format::



> git log --graph --date=short --branches --pretty=format:'%C(yellow)%h%C(reset) %ad | %C(75)%s%C(reset) %C(yellow)%d%C(reset) [%an]'


6.



, git+ssh , http :



> git clone git@github.com:gurugray/gurugray


7.



«» ( , pull-request' ) upstream push' origin. , , ssh-, :



> git clone git://github.com/gurugray/gurugray -o upstream


8.



pull, . , , . , :



> git fetch --all
> git rebase my_feature upstream/master


9.



, , :



> git fetch --all
> git checkout -b my_feature upstream/master

> git branch -D master


10.



, --fixup=



> git commit -m "Feature A is done"
> ...
> git commit -m "Feature B is done"
> git commit --fixup=fe2f857


> git log --oneline
a5050e7 fixup! Feature A is done
633e33f Feature B is done
...
fe2f857 Feature A is done
cb5db88 Previous commit


11.



— , «» , :



> git config --global alias.fixlast "commit --all --amend --no-edit"


12.



rebase --autosquash --fixup , , guidline', pull-request' .



> git rebase -i --autosquash cb5db88
pick fe2f857 Feature A is done
fixup a5050e7 fixup! Feature A is done
pick 733e2ff Feature B is done


> git log --oneline
633e33f Feature B is done
...
5778cbb Feature A is done
cb5db88 Previous commit


13.



reset , - «» :



> git reset @~4
> git commit --all


14. push



, --force -f, :



> git push origin +feature


15.



reflog - — , :



> git reflog
> ...
> git reset --hard FOUND_HASH


, .



Git' , .




All Articles