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 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 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!

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.

Remove single file from a commit

1. Undo the last patch, without scrapping the changes:

-> $ git reset --soft HEAD^

2. Then reset the unwanted files in order to leave them out from the commit:

-> $ git reset HEAD path/to/unwanted_file

3. Now commit again, you can even re-use the same commit message:

-> $ git commit -c ORIG_HEAD

reference: http://stackoverflow.com/questions/12481639/remove-files-from-git-commit

How to enable git-completion on PS1 variable

A nicer bash prompt I use includes a git completion script that indicates the branch of a repository I may be into. Like this:

git_completion

Green indicates “master” is clean and no modified files are present. Red indicates there is something new in that branch.

In order to configure that, I had to include some instructions on my .bashrc file. It looks like this:

# git completion
source /usr/share/git-core/contrib/completion/git-prompt.sh

(…)

PS1='\[\e[01;30m\]\t`if [ $? = 0 ]; then echo "\[\e[32m\] ✔ "; else echo "\[\e[31m\] ✘ "; fi`\[\e[00;40;37m\]\u\[\e[01;37m\]:`[[ $(git status 2> /dev/null | head -n2 | tail -n1) != "# Changes to be committed:" ]] && echo "\[\e[31m\]" || echo "\[\e[33m\]"[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] || echo "\[\e[32m\]"`$(__git_ps1 "(%s)\[\e[00m\]")\[\e[01;34m\]\w\[\e[00m\]\$ '

Now, sometimes (in a new environment mostly), we may configure the .bashrc just the way we want it and then:

git_2

Boom! You see a big "__git_ps1: command not found"  message.

That is because you don’t have the git-prompt.sh file you included in the directory loaded at the top of you .bashrc file (the /usr/share/git-core/contrib/completion/git-prompt.sh)

So in order to get rid of that, we need to include it. The file is available in contrib/completion/git-prompt.bash from the git repository git://git.kernel.org/pub/scm/git/git.git

So after you clone it, you should simply copy the missing files to your system file system. In my case, I preferred to copy the whole “completion” dir to my file systems, as I did not have it. So:

$ sudo cp -r completion/ /usr/share/git-core/contrib/

Now I have my git-completion.bash file in the correct place. Let’s see if that worked when running a new bash session:

git_3

Great! Worked like a charm.

Let me know if you have a different solution for it or if this did not solve your problem.

Thanks for reading.

Cheers!

Technically Speaking was used as a reference for this post.