All Distros, Git

Git: two awesome quick commands

Find branches the commit is on

git branch --contains <commit #>

Sample output:

Screen Shot 2017-10-17 at 10.18.41 PM10-17-17
fetch_weather, implement_axios and master all have the commit # 3dea5b6

Ref.: https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from

Show all branches with unmerged commits to <branch>

git branch -a --no-merged <branch>

Sample output:

Screen Shot 2017-10-17 at 10.22.39 PM10-17-17
remotes/origin/error_handler has not yet been merged into master

Ref.: https://stackoverflow.com/questions/3600728/git-list-all-unmerged-changes-in-git

Happy git work!

Cheers!

Advertisements
All Distros, Git

Git history (log) of a line

This is the first answer of this question on Stack Overflow, about how to see the history of a line of code on git.

Since git 1.8.4, there is a more direct way to answer your question.

Assuming that line 110 is the line saying var identifier = "SOME_IDENTIFIER";, then do this:

git log -L110,110:/lib/client.js

This will return every commit which touched that line of code.

[Git Documentation (see the “-L” command line paramenter)]

 

Cheers!

All Distros, Git

Git: find what commit inserted such file

I was in need of finding the commit that added a specific file in my project and “I got lucky” on Google:

Say we need the commit that introduced foo.js in the project, the command would be:

$ git log --diff-filter=A -- foo.js

And a super nice alias would be:

$ git config --global alias.whatadded 'log --diff-filter=A --'

and to use it:

$ git whatadded filename

That’s it.

Reference: https://stackoverflow.com/questions/11533199/find-commit-where-file-was-added

Git

Git basics: create and delete branches remotely

To create a remote branch:

$ git checkout -b <branch-name>
$ git push -u origin <branch-name>

To delete a remote branch:

$ git push origin --delete <branch-name> 
$ git branch -d <branch-name>

If there are unmerged changes which you are confident of deleting:

$ git branch -D <branch-name>

And that is all.

Cheers!

Git

Git diff between branches

There are several ways of displaying the differences between your git branches. The simplest ones being a little fuzzy and, if the diff is big enough, not very helpful. But thre are some tweaks we can make. The one I’ve been using mostly in the past weeks is:

$ git log --oneline --graph --all --decorate

Well, like always, when I see too big a command like this, I create an alias for it.
Anyway, this will print the current layout of all branches of your project, including the remote ones.

For example:

git_tree

It produces a cool graph layout with the commit title only, and it’s a very useful way to understand where things are in your project. However, if it’s really a big project, it might be too overwhelming.

I created some new branches with new commits and got a little more elaborated tree, like this:

git_tree_2.png

For the graph above, I created a new branch “bug883” from “develop” and “sample_branch” from “master”. None of them was pushed remotely, as we can tell by the output.

Well, give it a try and share what you think.

Cheers!

UPDATE: Many other ways of getting the diffs are discussed in this Stack Overflow post.

Git

Git checkout to a remote branch

There are many ways to checkout to a remote branch of a repository. The simplest one is creating a new branch in your local repository with the same name of the remote one, git fetch, and then git checkout to the new branch.

But there is an even simpler way to do so, with the use of a single git flag: -t <remote_branch>

Like this:

14:01:38 ✘ dbkreling:(master)~/my-application/src$ git branch -r
 origin/HEAD -> origin/master
 origin/develop
 origin/master
 origin/release

I want to checkout the develop branch, still not one of my branches. Then:

14:03:44 ✔ dbkreling:(master)~/my-application/src$ git checkout -t origin/develop
 Branch develop set up to track remote branch develop from origin.
 Switched to a new branch 'develop'

 14:06:36 ✔ dbkreling:(develop)~/my-application/src$ git branch
 * develop
 master

Hope it helps.

Cheers!

Git

Creating Git Aliases from Command Line

Creating git aliases is as easy as it is useful to speed your production time in development. The hard way — not so hard, actually — is as simple as adding a string in your /home/$USER/.gitconfig file. Like so:

[alias]
    co = checkout
    st = status
    unstage = reset HEAD --
    wc = whatchanged

Well, if you do not have any aliases configured yet, create an [alias] block there and start appending your aliases.

However, the easiest way is to issue a command line in your terminal, and the alias will be added for you and will be ready to be used. E.g.:

git config --global alias.st status

or

git config --global alias.wc whatchanged

Keep adding them as you wish.

To use them, just issue git <your-alias> [args]  from the command line.

Go ahead and share your most used aliases in the comments.

Thanks for reading.