10 Useful Git Tips


Git has been one of the most widely used version control systems over the last few years due to its enormous rise in popularity. Developers working in a range of languages and kinds of teams utilize it, from little open-source initiatives to massive codebases like the linux kernel.

We are going to provide you some advice in this article that should help you get more out of using git and streamline your routine.

git log –no-merges

This git command displays the whole history of commits, excluding those that resolve merge conflicts or combine two branches together. This way, merging commits will not clog the git history and you can easily view any modification made to the project.

$git log --no-merges

commit e75fe8bf2c5c46dbd9e1bc20d2f8b2ede81f2d93
Author: Omar
Date: Mon Jul 10 18:04:50 2017 +0300

Add new branch.

commit 080dfd342ab0dbdf69858e3b01e18584d4eade34
Author: Omar
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 –no-commit [commit]

With Git revert, you can create a new commit with the updated content while undoing the changes done by previous commits. You can use the flag –no-commit or the shortcut -n to undo the named commits and stop the automatic commits.

git diff -w

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.

git diff –stat

Demonstrates the evolution of each file over time. To change the default output width, add the following three parameters: count to limit the output to the first number of lines, name-width to set the filename’s width, and width to override that default width.

$ git diff --stat
 index.php | 83 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 43 insertions(+), 40 deletions(-)

$ git diff --stat-width=10
 index.php | 83 +++---
 1 file changed, 43 insertions(+), 40 deletions(-)

git reset –soft HEAD^

Without affecting the working tree or the index file, reset the head to a certain commit. The “staged for commit” stage is where all modifications made after this commit are placed. To add them back in, all you have to do is execute git commit after that.

git stash branch [branch-name] [stash]

With this command, a new branch called branch-name is created, checked out, and the modifications from the specified stash are applied to it before the stash is dropped. It utilizes the most recent stash if none is provided. This lets you apply any modifications you have stashed in a safer setting, which may then be merged into master.

git branch -a

A list of all local and remote-tracking branches is displayed. To view only the branches that have been completely merged into the master branch, use the –merged flag. You can monitor your branches in this manner and determine which ones can be removed once they are no longer needed.

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master

git commit –amend

Rather than creating a new commit, you can modify your old one with git commit –amend. Using this command, you can add your most recent modifications to the most recent commit and even modify the commit message if you have not sent your changes to a remote branch yet.

git pull –rebase

Git pull –rebase compels git to rebase the unpushed commits on top of the most recent iteration of the remote branch after initially pulling the changes. A linear history can be ensured by using the –rebase option, which stops pointless merging commits.

git add -p

This command runs through each change and asks what you want to do with it, rather than putting all of the changes to the index right away. You are able to actively select what you wish to commit to in this way.

diff --git a/package.json b/package.json
index db78332..a814f7e 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,6 @@
   "devDependencies": {
     "bootstrap-sass": "^3.3.7",
-    "gulp": "^3.9.1",
     "jquery": "^3.1.0",
     "laravel-elixir": "^6.0.0-11",
     "laravel-elixir-vue-2": "^0.2.0",
Stage this hunk [y,n,q,a,d,/,e,?]? 

Leave a Reply

Your email address will not be published. Required fields are marked *