wiki:Git
Last modified 5 years ago Last modified on 05/22/14 15:36:18

Git

Installation

sudo apt-get install git gitk

Config your personal information:

# To set it globally for all repos
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"

# To work with different repos
cd my_other_repo
git config user.name "Different Name"
git config user.email "your_other@email.com"

Clone a repo (first fork the project from the site if needed):

git clone https://github.com/user_name/project_name.git                # From github
git clone https://user_name@bitbucket.org/user_name/project_name.git   # From bitbucket
git checkout develop                                                   # Change to branch develop (and develop there)

An example of clone, remote, branch, fetch, merge.

git clone https://rcarranza@bitbucket.org/rcarranza/socialmedia.git
git remote add upstream https://rcarranza@bitbucket.org/jnorris/socialmedia.git
git checkout develop
git fetch upstream
git merge upstream/develop

Push:

git add file             # Adds file to the files to be commited
git commit -m"msg"       # Commits to local repo
git push                 # Push all commits since last push to your repository, hosted in a server

After you've made some changes (e.g. fixed a ticket) make a Pull Request in the site.

Merging sources from your upstream repository (make sure to push changes to your repository first):

git remote add upstream remote_repository  # Set upstream to remote_repository (e.g., https://company_name@bitbucket.org/company_name/project_name.git
git fetch upstream                         # Get all files from upstream
git merge upstream/develop                 # Merge all changes. If there are any conflicts, you'll have fix them manually and execute this command again.
git branch -lv             # Show branches with verbose option
git remote -v              # Show remotes with verbose option
git remote -rm this_remote # Remove this_remote

Additional Help:

Advanced Git Configuration

Put this in your .gitconfig

[user]
	email = your@email.com
	name = Your Name

[alias]
        st = status
        ci = commit
        br = branch
        co = checkout
        df = diff
        # Pretty log with branch
        lg = log --color --graph --pretty=format:'%h %C(blue)(%cr) %C(bold blue)<%an>%C(reset)%C(red)%d%C(reset) %s' --abbrev-commit --
 
[color "branch"]
        current = green bold
        local = yellow bold
        remote = red bold

[color "diff"]
        meta = blue bold
        frag = magenta bold
        old = red bold
        new = green bold
        whitespace = red reverse

[color "status"]
        added = yellow bold
        changed = green bold
        untracked = red bold

[color]
        ui = true

Tips and Tricks

Really Blame It

git blame -C

More Specific Blaming

Let's say the piece of text you are investigating is this:

a = get_something(some_var)

and it is located in the file some_folder/some_file.py

Then, try this command:

git log -S "a = get_something(some_var)" some_folder/some_file.py

Then, GIT will look for the string we specified within the commit history and output something like this:

commit <a commit hash>
Author: Some Dude <some.dude@mail.com>
Date:   Fri Feb 15 14:09:18 2013 -0300
    The commit message of this commit.

Individually review and stage changes within each file

git add --interactive

Diff after add

git diff --cached