Git Tips (Rough Draft)

24 Feb 2015

Git Tips (Rough Draft)

Creating keys

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

$ ssh-keygen -t rsa -C ""
# Creates a new ssh key, using the provided email as a label

Generating public/private rsa key pair.
# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]

Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]

Your identification has been saved in /c/Users/you/.ssh/id_rsa.
# Your public key has been saved in /c/Users/you/.ssh/
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db

# start the ssh-agent in the background
$ ssh-agent -s
# Agent pid 59566
$ ssh-add ~/.ssh/id_rsa

On Windows to run ssh-agent

$ eval $(ssh-agent) > /dev/null
$ ssh-add ~/ssh/id_rsa
Enter passphrase for /c/Users/Torrijos/.ssh/id_rsa:
Identity added: /c/Users/Torrijos/.ssh/id_rsa (/c/Users/Torrijos/.ssh/id_rsa)

This will allow you to perform remote operations with Bitbucket, or any other remote service, without having to enter your private key passphrase everytime.


$ git config --list Chacon
# Shows a list of config settings
Set name and email
$ git config --global "John Doe"
$ git config --global
Configure Line Ending




View remotes

$ git remote

View remote urls

$ git config --get remote.origin.url

$ git config --get remote.upstream.url

Add remote url

$ git remote add origin
# Set a new remote

$ git remote -v
# Verify new remote
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)

Change remote url

$ git remote rename origin destination
# Change remote name from 'origin' to 'destination' 

$ git remote set-url origin
# Change the url for an existing remote

… other topics here

Updating Local branch

From local branch

$ git pull --rebase upstream develop
# Rewrites current branch to match remote and replay your local changes on top
$ git pull --rebase -s recursive -X theirs upstream develop
# Does the same except any conflicts favors your local branch's changes
# Thus you may lose changes from the remote using this.

Sometimes you may need to abort a rebase:

$ git rebase --abort

or a merge

$ git merge --abort

When merging another branch onto your current branch, it may be good to merge all changes into one commit

$ git merge --squash otherbranch


$ git commit -m 'Consolidated commits from other branch'

In case of merge conflicts you can also use:

$ git merge -s recursive -X ours otherbranch or 

$ git merge --squash -s recursive -X ours otherbranch
# This favors your current branch's changes rather than the other branch.

For actually resolving merge and rebase conflicts, open the repository with a git GUI application, i.e. Git Extensions, Atlassian SourceTree, etc.

Launch the merge tool, KDiff or Perforce P4 Merge to resolve conflicts.

Update Remote Branch List On Local

To get a copy of branch list on remote

$ git fetch or $ git fetch upstream

If you are sure that remote server repo contains more branches and they are not shown when you type

$ git branch -a


$ git branch -r

Then you have to update your remote list, by:

$ git remote update upstream --prune

Assuming your remote is named as upstream (This is true most of times).

for brname in `git branch -r | grep upstream | grep -v master | grep -v HEAD | sed -e 's/upstream\///'`; do echo $brname upstream/$brname; git branch --track $brname upstream/$brname; done

Once you have created a local branch corresponding to each upstream branch. Update your fork (origin)

$ git push --all origin

(origin being your fork): that supposes that: you have all the local branches tracking all the upstream branches (see previous step). Otherwise, you would push only one local branch by default, since a clone would create only one local branch (the default one) you haven’t pushed commits of your own on those branches.

$ git push --prune origin