Git – Overview

Git

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenientstaging areas, and multiple workflows.

Git is the corner piece of development. This article is my git memo.

installation

apt-get install git

configuration

Anything can be set at three levels
config targets saves to scope
--system /etc/gitconfig host
--global ~/.gitconfig user
--local .git/config repository

Minimum configuration:

% git config --global user.name "your name"
% git config --global user.email "your email"
% git config --global core.editor "vim"

Querying all entries :

% git config --list

Querying one entry

% git config section.key
% git config section.subsection.key
% git config remote.origin.url

Where you have in your config file

[remote "origin"]
        url = https://github.com/planetrobbie/www.yet.org.git

export https_proxy=http://proxyhost:proxyport to setup your proxy git config --global http.proxy http://user:pass@proxyhost:proxyport another way to setup your proxy

create a new repo

First create some required files: Readme.md, LICENSE and .gitignore.
You can then initialize your repository by creating .git directory using the following command

% git init

You can add a directory name to ask git to create it at the same time.

1st commit

% git add README.md LICENSE .gitignore

If you prefer you can add all of your modified files like this

% git add -A

check status, commit locally

% git status -s
% git commit -m "initial commit"

create a new repo on GitHub and push it there

% git remote add origin https://github.com/USERNAME/REPONAME.git
% git push -u origin master

following up commits

% git add FILENAME
% git commit -m "second commit"
% git push -u origin master

You can also do all in one go with -a which adds all files to working area

% git commit -am 'new commit'

unstage changes

this command unstage hello.rb, -- starts file path argument

% git reset HEAD -- hello.rb

remove files from the staging area, use --cached to leave file on disk.

% git rm FILENAME

revert a file to last checked version

% git checkout FILENAME

diff

show diff of unstaged changes. So where git status will show you what files have changed and/or been staged since your last commit, git diff will show you what those changes actually are, line by line.

% git diff

--cached show diff of staged changes which goes to the next commit snapshot
HEAD difference between your working directory and the last commit
--stat show summary of changes instead of a full diff
branchA...branchB compare two divergent branches

inspection and comparison

look for commits from a specific author

% git log --author

by date authored

% git log --since={2010-04-18}
% git log --before={3.weeks.ago}
% git log --until='5 minutes ago'
% git log --after=

by commit message. Git will logically OR all --grep and --author arguments.
--all-match to match all instead
--format="%h %an %s" to modify output format

% git log --grep

filter by introduced diff. tell Git to look through the diff of each commit for a string.

% git log -Sstring

show patch introduced at each commit. git show [SHA] do the same with a specific commit SHA

% git log -p

show diffstat of changes introduced at each commit. summary of the changes, less verbose then -p

% git log --stat    

branching and merging

list branches

% git branch

create a new branch and switch to it

% git branch BRANCH
% git checkout BRANCH

or you can create and checkout in one go with -b

% git -b branch BRANCH

delete a branch

% git branch -d BRANCH

merge a branch context into your current one

% git merge BRANCH

show commit history of a branch
--oneline for a compact output
--graph for a topology view
^otherbranch to exclude otherbranch from report
--decorate to display tags

% git log BRANCH

tag a point in history as important. To tag last commit (HEAD) as v1.0
If you need to tag another commit specify the commit SHA as last argument.

% git tag -a v1.0

clone a GitHub repo

% git clone git://github.com/schacon/simplegit.git

sharing and updating

list remote repository aliases. Git repositories are all equal and you simply synchronize between them. This command help manage alias or nickname for each remote repository URL to avoid using full URL every time

% git remote -v

-v to display full URLs

create a new alias for a remote repository, alias names are arbitrary

% git remote add ALIAS URL

removing an existing remote alias

% git remote rm ALIAS

download new branches and data from a remote repository.

% git fetch

fetch from a remote repo and try to merge into the current branch. Run a git fetch immediately followed by a git merge of the branch on that remote that is tracked by whatever branch you are currently in. Prefer running it separatly instead.

--all synchronize with all of your remotes

% git pull

push your new branches and data to a remote repository. If someone else has pushed since you last fetched and merged, the Git server will deny your push until you are up to date.

% git push ALIAS BRANCH

push a cloned Repo to your own GitHub account

% git remote add github https://github.com/USERNAME/REPONAME.git
% vi .gitignore
% git add -A
% git commit -m "Initial commit from clone"
% git push github

rename github to origin [avoid name argument while pushing, origin is defaut]

% git remote rename origin upstream
% git remote rename github origin

pull in upstream changes

Fetches any new changes from the upstream repository

git fetch upstream
git merge upstream/master

or if you are in a hurry, you can do it in one step.

git pull upstream

Beware all changes will be merged by default to the current branch.
You can do a Rebase instead, which is a way to re-integrate two branches by simulating each developer taking turns coding. So when Rebasing, your commits will be put on the side while pulling origin commits, your commits will then be re-integrated at the end of the process. Provides simplicity in the code history. It’s like if you were redoing the work in a millisecond after Pulling from origin.

git pull --rebase

git config branch.autosetuprebase always to set this on by default with

pull request

Let’s assume you’ve forked a repository and pushed your changes. You can now initiate a pull request

  1. switch to the branch you want someone else to pull
  2. click pull request
  3. fillout the form and send it

reuse recorded resolution – rerere of conflicted merge

git config --global rerere.enabled true enable rerere
git merge origin/newbranch imagine a conflict happen now, you can now use
git rerere status to list file observed by rerere
git rerere diff to see differences
vi <conflicts> to resolve conflict
git commit -a -m'Resolved the merge conflict' that’s it it’s resolved and recorded by rerere
git rerere gc to clean things a bit, forget older then 15 days recording but shouln’t be necessary.
.git/rr-cache file stored there, not part of push/pull mechanism, stays on your machine
git rerere clean to delete all recorded resolution
git rerere forget path/to/file to delete a recorded resolution, each one of them is attached to a specific file

Advertisements