Development Stickies

Development Notes for Web Programmers…

Git in a Nutshell

These are the git commands I found most useful.

Create new local repository

 mkdir target
cd target
git init 

Optional setup commands:
You can configure ignore patterns for your commits:

vi .git/info/exclude

You can add many remote repositories, e.g. one on github

git remote add LOCALNAME git@github.com:/USERNAME/REPONAME.git

Once a remote repo is configured, you can fetch the master branch:

git pull LOCALNAME BRANCHNAME

Local commits

 cd target // go to the root folder of the local git repo
git add -An // OPTIONAL: preview what would be added to commit set
git add -A // add all changes to current commit set
git commit -am 'commit message' // commit all changes in the commit set 

Replace the last commit message:

 git commit --amend -m "your new message" 

Remote commits

Do you want to clone an existing repository locally and have local branches to work on? Do this:

git clone git@github.com:USERNAME/REPOSITORYNAME.git
git checkout -b mybranch remotes/origin/mybranch

Commit current working copy to remote

git push REMOTENAME BRANCHNAME

Pull a remote branch and merge it with local workset:

git pull REMOTENAME BRANCHNAME

Branches

Show all branches (the branch you are currently working on is marked by an *)

git branch

Show also all remote branches

git branch -a

CREATE branch (you are not switching yet to the new branch, just create it!)

git branch BRANCHNAME

DELETE a branch locally and remote:

git branch -d BRANCHNAME
git push origin --delete BRANCHNAME

SWITCH to another branch

git checkout BRANCHNAME

Commit local changes to another branch (e.g. you are on BRANCH1, but you want to commit to BRANCH2)

 git stash
git checkout BRANCH2
git stash pop 

Or if you have multiple stashes:

 git stash
git checkout BRANCH2
git stash list # to check the various stash made in different branch
git stash apply x # to select the right one 

Resolve conflicts

Sometimes when you do merge there are conflicts to be resolved.
The easiest way to resolve the conflicts is:

 EDIT the conflicting file
git add
git commit 

Forking

You have forked from a repository at commit 1 (let’s assume the branch name is master). You have worked on your fork and you realize that important updates have been committed to the original repository in the meanwhile.

This is how you update your fork with the latest changes from the original repo:

 git remote add --track BRANCH_IN_ORIG_REPO LOCAL_REMOTE_NAME
git://github.com/USER_OF_ORIG_REPO/ORIG_REPO_NAME.git
git fetch LOCAL_REMOTE_NAME
git merge LOCAL_REMOTE_NAME/master 

Delete directories

Delete whole directories also in the history:

 git stash
git filter-branch --tree-filter 'rm -rf tools/software' HEAD
git stash pop
git push --force // this is used in order to push the tree-filtered branch! 

Undo stuff

You have deleted a file by accident, uncommitted. Here is how you restore it:

 git reset HEAD PATH_TO_FILE
git checkout PATH_TO_FILE 

You have uncommited changes locally and want to delete them all:

 git reset --hard 

Search

Search changes of a specific file:

 // history of a file:
git log --follow -- discogs-reihe/mo/to/r-/revue.xml
// with all change details:
git log --follow -p -- discogs-reihe/mo/to/r-/revue.xml
// store file of certain revision at a new local file:
git show c92dbb227152cc740bb784f28b0e1165601d5ff6:discogs-reihe/mo/to/r-/revue.xml >> /home/temp/temp.xml 

Search changes in a specific directory:

 git log --oneline --name-only -- path/to/dir 

If you have the actual source OUTSIDE of the git directory (your git repository only contains the .git file), you need to search with absolut path to the directory:

 /home/project1/git // this is the git repo
/home/project1/source // this is the physical location where you have all your project sources
git log --oneline --name-only -- /home/project1/source/path/to/dir 

Show

Display files of a certain commit:

 git show --pretty="format:" --name-only COMMIT-ID 

This will be updated continuously.

Leave a comment