Why you need more than git commit and git push.
Article by Marcin Zarębski
There are developers who use only three Git commands: git add, git commit and git push - and that's enough for them in their everyday work. But Git is so much more than these 3 commands! As we can read in the documentation:
*Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. *
To take advantage of this speed and efficiency you should learn some useful commands. In this blogpost, I'd like to share with you some of the most interesting Git commands - and my personal favorites. Let's explore them!
You may have used git stash command (this command takes modified files in your working directory and saves them in a separate place) and you may have been annoyed that the new files were not stashed. By using -u option you can move your new files to the stash.
Have you ever discovered that there's a big unnecessary file in your repository? You could just remove it from your local directory and push that change to the remote. However, there is one problem with this approach: the file still exists in the repository history. Good news - it's possible to remove such file permanently! See here for details.
Have you ever heard of the wall of pink? It is a situation when someone commited a small change to your repository, but all the files in this change have all the lines changed. In GUI tool, you may see all the lines pink (removed) and right below all the lines green (added). But how did it happen? The answer to this problem may be different line endings (CR LF vs LF). Read more about it here and here.
Did you know that you can customize git log appearance? Read more about possible formatting options here.
This command will show how many commits you and your teammates pushed to the repository.
More polite version of git push --force. Remote branch won't be overwritten if someone pushed some changes to it.
Delete branches that were merged to current branch, except for branches excluded in the filter (master and dev in this example).
Using this command, you can find a log of what you did in the repository. You can use it for example to restore a commit from a branch that was deleted.
If you forgot to commit one extra change, you can use this command to edit your commit. Use it only if you did not push your commit!
Imagine that you find a bug in the current version, and you know that this feature worked in some old commit. There are probably dozens of commits between the current version with the bug and the working one. If you have trouble with identifying what part of code and what commit is responsible for that bug, you can use git-bisect to identify the root cause of the issue. You start by marking two commits as bad and good, and use git-bisect to perform binary search in the repository history. More details can be found in the documentation.
You probably know that you can search your working directory with grep function. Did you know you can do the same in the repository history? If you remember some part of code that was deleted and you need it now, use git grep. Usage is similar to grep function. Find out more here.