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

Find out your gateway IP address

There are two ways of collecting the IP address of your current Gateway:

  1. netstat -r
  2. route -n

The output of both commands are the almost the same:

12:29:25 ✔ dbkreling:~$ sudo netstat -r
[sudo] password for dbkreling:
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.0.1     0.0.0.0         UG        0 0          0 wlan0
172.17.0.0      *               255.255.0.0     U         0 0          0 docker0
192.168.0.0     *               255.255.255.0   U         0 0          0 wlan0

12:29:36 ✔ dbkreling:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.0.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0

In my case above IP 192.168.0.1 is the gateway for my connection. The flag U indicates that route is up and G indicates that it is gateway.

Cheers!

 

IPython (& ipdb) issue with python 2.7

If you ever wanted to use ipdb (from IPython) debugger when using Python 2.7, you might have faced an issue like this:

$ sudo pip install ipython
Downloading/unpacking ipython
  Downloading ipython-6.0.0.tar.gz (5.1MB): 5.1MB downloaded
  Running setup.py (path:/tmp/pip_build_root/ipython/setup.py) egg_info for package ipython
    
    IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.
    When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
    Beginning with IPython 6.0, Python 3.3 and above is required.
...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/ipython

So you gotta go ahead and install IPython==5.0, because it’s the last LTS version. Do:

#pip install IPython==5.0

And then you should get:

ImportError: No module named packaging.version

Hm, that was not expected. Installing setuptools or packaging did not work for me at this moment. To get away with this error I had to install pip3 and then upgrade pip:

  251  sudo apt-get install python3-pip
  252  sudo pip3 install --upgrade pip
  253  sudo pip install ipython==5.0
  254  sudo pip install ipdb

After that, both IPython and ipdb installed with no issues or errors and life was happy again.

ALTERNATIVE SOLUTION:

If the above still does not fix the issue, or if pip and ipython are bound to python 3, you can try this, according described in the packaging.python.org website:

  1. Download get-pip.py
  2. Run python get-pip.py. [2] This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

Let me know if that worked for you too.

Good Luck!

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.

Create a new user on your system

This is a quick checklist on how to create a user from the command line:
1. useradd -m your_username (add a user called your_username with a home directory)
2. passwd your_username (set a password for your_username)
3. usermod -aG wheel your_username  (to include the user in the sudoers list)
4. su - your_username (to change to the your_username account)
NOTE: sudo usermod -a -G sudo username (to include user ‘username’ in the sudo group)