Over the past few years git has vastly grown in popularity to become one of the most used version control systems. It is used by developers coding in various languages and teams of all sizes, from small open-source projects to huge codebases like the linux kernel.
In this article we are going to share with you a few tips that could improve your git experience and workflow.
This git command shows the whole commit history but skips commits that merged two branches together or solve a merge conflict. This allows you to quickly see all the changes done to the project, without having merge commits cluttering the git history.
$git log --no-merges commit e75fe8bf2c5c46dbd9e1bc20d2f8b2ede81f2d93 Author: John Date: Mon Jul 10 18:04:50 2017 +0300 Add new branch. commit 080dfd342ab0dbdf69858e3b01e18584d4eade34 Author: John Date: Mon Jul 11 15:40:56 2017 +0300 Added index.php. commit 2965803c0deeac1f2427ec2f5394493ed4211655 Author: John Date: Mon Jul 13 12:14:50 2017 +0300 Added css files.
Git revert generates a new commit that undoes the changes made by existing commits and generates a new commit with the resulting content. If you want to to revert the named commits and avoid the automatic commits, you can use the flag –no-commit or the shorthand -n.
Git diff shows the changes between two commits, two working trees or two files on disk. When multiple people work on the same project, often there are changes due to text editor’s tab and space setting. In order to ignore differences caused by whitespaces when comparing lines, you can use it with the -w flag.
Shows how each file has been changed over time. You can add 3 parameters: width to override the default output width, name-width to set the width of the filename and count to limit the output to the first number of lines.
Reset the head to a certain commit without touching the index file and the working tree. All changes made after this commit are moved to “staged for commit” stage. After that you just need to run
git commit to add them back in.
This command creates a new branch named branch-name and check it out, then applies the changes from the given stash to it and drops the stash. If no stash is given, it uses the latest one. This allows you to apply any stashed changes into a safer environment, that can later be merged into master.
It shows a list of all remote-tracking and local branches. You can use the –merged flag to see only the branches that are fully merged to the master branch. This way you can track your branches and find out which ones aren’t used anymore and can be deleted.
git commit --amend you can change your previous commit, instead of making a new one. If you haven’t pushed your changes to a remote branch, you can use this command to amend the most recent commit, adding your latest changes and even changing your commit message.
Git pull –rebase forces git to first pull the changes and then rebase the unpushed commits on top of the latest version of the remote branch. The –rebase option can be used to ensure a linear history by preventing unnecessary merge commits.
When you use this command, instead of immediately adding all the changed to the index, it goes through each change and asks what you want to do with it. This way, it allows you to interactively choose exactly what you want to be committed.