📜 ⬆️ ⬇️

Little known Git commands



Git has strict backward-compatibility obligations: many advanced features are hidden behind a variety of options, and are not used as default behavior. Fortunately, Git also supports aliases, so you can create your own commands that do all the magic of Git. Under the cut is a selection of useful (or at least amusing) aliases defined in my .gitconfig .

git please


$ git config --global alias.please 'push --force-with-lease' 

Each developer had to at least once communicate with his teammate on the topic of forced pushing (force pushing) into a common thread (do not do this). Rebasing, editing, and squash are all fun until you rewrite part of the overall story and spread duplicate commits all over the repository. Fortunately, Git will not allow you to unwittingly overwrite the history on the server. You will have to explicitly pass the --force option to git push to prove the seriousness of your intentions. But compulsory pushing is a rough approach: you trample the upstream branch with a local version, and all changes that you didn’t pull up (fetch) by that moment will be erased from the history.


')
The git -option --force-with-lease works much more carefully: it checks that your local copy of the ref is up to date before rolling it. This means that you at least pulled up all the changes that are going to trample. But in order not to write git push --force-with-lease every time, I made an alias for this line: git please

git commend


 $ git config --global alias.commend 'commit --amend --no-edit' 

It so happens that you commit and immediately realized that you forgot to index (stage) file? No more worrying about it! The git commend alias silently attaches all the indexed files to the last commit you created, reusing the commit message you already have.

 $ git add Dockerfile $ git commit -m 'Update Bitbucket pipeline with new Docker image' # (facepalm) $ git add bitbucket-pipelines.yml $ git commend 

git it


 $ git config --global alias.it \ '!git init && git commit -m “root” --allow-empty' 

The first commit in the repository cannot be re-called as usual. Therefore, it is recommended to create an empty commit as the root. The git it alias initializes your repository and in one operation creates an empty root commit. And the next time you run a project, you don’t just need to add it to your version control system: run git it !

 $ cd shiny-new-thing $ git it Initialized empty Git repository in /shiny-new-thing/.git/ [master (root-commit) efc9119] root 

git staaash


 $ git config --global alias.stsh 'stash --keep-index' $ git config --global alias.staash 'stash --include-untracked' $ git config --global alias.staaash 'stash --all' 

git stash is one of the most exciting and useful git commands. It logs all changes made to the monitored file in your working tree, and hides them for later use, and shows you a clean tree so that you can safely work with another part of it. But if you have created new files and have not yet indexed them, then by default git stash will not touch them, so you will have an untidy working tree. Accordingly, the contents of untracked or ignored files are not hidden by default.

I made some convenient aliases for different git stash options, depending on which bits of your desktop tree need to be hidden:

 git stsh #        git stash #       git staash #      git staaash #  ,     

If in doubt, the longest alias ( git staaash ) can always restore the working tree of the state of the fresh clone of your repository.

git shorty


 $ git config --global alias.shorty 'status --short --branch' 

I run git status more often than any other git command. The built-in help in Git in recent years has become much more convenient, which is very good for beginners, but for more experienced users the information is too verbose. For example, git status explains to me in 12 lines that I have a couple of indexed, non-indexed and untraceable changes:

 $ git status On branch master Changes to be committed: (use “git reset HEAD <file>…” to unstage) modified: package.json Changes not staged for commit: (use “git add <file>…” to update what will be committed) (use “git checkout -- <file>…” to discard changes) modified: package.json Untracked files: (use “git add <file>…” to include in what will be committed) index.js 

All the same git shorty tells me in three lines:

 $ git shorty ## master AM test ?? .gitignore 

For brevity, I did this in the form of an alias git st , I could not stop

git merc


 $ git config --global alias.merc 'merge --no-ff' 

If you are using a normal branch workflow without a rebuy, then it would not be the best solution to run a standard git merge to merge branches with features with a master branch. If you do not add options to this command, the default merge strategy will be used --ff , in which a new merge commit will be created only if there are no new changes in the master branch. Otherwise, the master branch simply "rewinds" to the place of the last commit in your branch. Only sometimes , when creating a merge commit, when viewing a Git history, it can be difficult to tell which code was developed in which thread.



git merc uses the --no-ff strategy, which always creates a merge commit.



By the way, --no-ff is always the default when merging pull requests into Bitbucket .

git grog


 $ git config --global alias.grog 'log --graph --abbrev-commit --decorate --all --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)"' 

My alias git grog (or gr aphical l og ) has grown so much in recent years that I'm no longer sure if I know exactly what it is doing. But it looks beautiful:



For comparison, here is the standard git log :



There are available all kinds of convenient formats , so forkayte the above command and use on health!

For GUI fans


If you are a Git GUI fan and work on Mac or Windows, then you may be using our free Atlassian SourceTree Git client . If yes, then apply the aliases described in this article, creating a new custom action - you can assign a key combination - in the SourceTree settings:



This action is launched using the menu ( Actions → Custom Actions ) or the keyboard shortcut:



Have a nice aliasing!

Source: https://habr.com/ru/post/318508/


All Articles