View Source

{info:title=Useful Information}
This page has been generated by [maven-confluence-plugin|https://github.com/bsorrentino/maven-confluence-plugin]
{info}

h1. Table Of Contents
{toc:maxLevel=1}

h1. How to: Sync local repo with remote one
{noformat}
git fetch --verbose --prune
{noformat}
{info:title=-p, --prune}
After fetching, remove any remote-tracking branches which no longer exist on the remote.
From [Stack Overflow|http://stackoverflow.com/questions/6373277/git-sync-local-repo-with-remote-one]
{info}

h1. How to: Clone

h2. Clone a project

{noformat}
git clone <git_url> [directory]
{noformat}

h2. Clone a single branch

{noformat}
git clone -b <branch name> --single-branch <git_url> [directory]
{noformat}

h1. How to: Submodules

h2. Add a Submodule

{noformat}
git submodule add <git_url>
{noformat}

h2. Pull latest of all submodules

{info:title=from ver 1.7.3}
{noformat}
git pull --recurse-submodules
{noformat}
{info}

{info:title=from ver 1.6.1}
{noformat}
git submodule foreach git pull origin master
{noformat}
{info}

h2. Tutorial
bq. [Git Tools Submodules Tutorial|http://git-scm.com/book/en/Git-Tools-Submodules]

h1. How to: Patch

h2. Simulate patch
{noformat}
git apply --stat fix_file.patch
{noformat}

h2. Check patch
{noformat}
git apply --check fix_file.patch
{noformat}

h2. Commit patch
{noformat}
git apply fix_file.patch
{noformat}

h1. How to: Diff

h2. Show only changed files
{noformat}
git diff --name-only <branch>
git diff --name-only <commit1> <commit2>
{noformat}


h1. How to: Ignore after commit

{noformat}git rm --cached <files pattern>
{noformat}

h1. How to: +Revert+

h2. Revert All Uncommitted Changes in Working Tree and Index
{noformat}git reset --hard
{noformat}

h2. Remove all Untracked Files and Directories
{noformat}git clean -fd
{noformat}

h1. How to: +Branches+

h2. Create a new branch from current

{noformat}
git branch -b NEWBRANCH
{noformat}

h2. Create a new empty branch

{noformat}
git checkout --orphan NEWBRANCH
# clear the working directory with:
git rm --cached -r .
{noformat}

h2. List branch

{noformat}
git branch ' list local
git branch -r ' list remote
git branch -a ' list local + remote
{noformat}

h2. Comparing Git Branches

h3. Display diff change(s)
{noformat}git diff branch1..branch2
{noformat}

h3. Display diff file(s)
{noformat}git diff --name-status branch1..branch2
{noformat}

h3. Display diff detail(s)
{noformat}git diff --stat --color master..branchName
{noformat}

h2. Push local branch to remote repository

{noformat}git push -u origin _local_branch_
{noformat}

h2. Remove local/remote branch

To remove a local branch from your machine:

{noformat}git branch -d the_local_branch
{noformat}To remove a remote branch:

{noformat}git push origin :the_remote_branch
{noformat}

h2. Pull a remote branch

You could pull a *remote branch* to a *local branch* with the following command:

{noformat}git pull <repo> <remotebranchname>:<localbranchname>
{noformat}{_}Example_:

{noformat}git pull origin rxyz:lxyz
{noformat}When you are on the master branch you also could first checkout a branch like:

{noformat}git checkout -b lxyz
git pull origin rxyz
{noformat}This creates a new local branch "lxyz" from the master and directly checks it out. Than you do: git pull origin rxyz that pulls the remote branch to your local one.


h1. How To: +Tags+

h2. Create a new branch from a tag

{noformat}
git checkout -b newbranch tagname
{noformat}

h2. Delete a remote Git tag

Probably you don’t need to do this often (if ever at all) but just in case, here is how to delete a tag from a remote Git repository.

If you have a tag named ‘mytag’ then you would just do this:

{noformat}git tag -d mytag
git push origin :refs/tags/mytag
{noformat}That will remove ‘mytag’ from the remote repository (e.g. Github).

h2. Create the tag:

{noformat}git tag -a 1.0 -m'release 1.0'
{noformat}

h2. push your tag to the remote repository:

{noformat}git push --tags
{noformat}

h2. Download tag as zip archive

{noformat}git archive --format=zip tag_name > output_file
{noformat}

h1. Branching, Merging & Development strategy (from _+[StackOverflow|http://stackoverflow.com/questions/21848/switch-branch-names-in-git]+_)

{tip}One good strategy during development *is to never works directly on master branch*.{tip}Always we have to create a new branch for new code:
{noformat}git checkout -b topic/topic_name master
{noformat}From there, I can push out the changes to public repositories:
{noformat}git push <public git url> topic/topic_name
{noformat}Or eventually just merge it back in master branch:
{noformat}git checkout master && git merge topic/topic_name
{noformat}If you truly need to go back to an older point in time and set that as your master, you can rename the current branch to something else and then check out an older version to be your master:
{noformat}git branch -m master junk
git co -b master old_sha1_value
{noformat}To remove local & remote branch

{noformat}git branch -d the_local_branch
git push origin :the_remote_branch
{noformat}








bq. Taken from [StackOverflow|http://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-in-github].
h1. How To: Switch “origin” of your GIT repository

{noformat}git remote set-url <remote name eg:origin> <new git url>
{noformat}

h1. How To: Stashing your changes (_+[original post|http://book.git-scm.com/4_stashing.html]+_)

While you are in the middle of working on something complicated, you find an unrelated but obvious and trivial bug. You would like to fix it before continuing. You can use *git stash* to save the current state of your work, and after fixing the bug (or, optionally after doing so on a different branch and then coming back), *unstash* the work-in-progress changes.

{noformat}git stash save "work in progress"
{noformat}This command will save your changes away to the stash, and reset your working tree and the index to match the tip of your current branch. Then you can make your fix as usual.

{noformat}... edit and test ...
git commit -a -m "your comment"
{noformat}After that, you can go back to what you were working on with git stash apply:

{noformat}git stash apply
{noformat}

h2. Stash Queue

You can also use stashing to queue up stashed changes.

{noformat}git stash list
stash@{0}: WIP on book: 51bea1d... fixed images
stash@{1}: WIP on master: 9705ae6... changed the browse code to the official repo
{noformat}Then you can apply them individually with

{noformat}git stash apply stash@{1}
{noformat}Clear out the list with ''.
{noformat}git stash clear
{noformat}

h1. How to: svn project as git repository

h2. Clone

{panel}
{noformat}
git svn clone [-s] <svn repo url>
{noformat}
{tip:title=standard layout}The \-s is there to signify that my Subversion repository has a standard layout (trunk/, branches/, and tags/.) If your repository doesn’t have a standard layout, you can leave that off.
{tip}
{panel}

{panel}
{noformat:title=clone svn revision}
git svn clone -r<revision number> <svn repo url>
{noformat}

{noformat:title=show revision history}
> svn log --stop-on-copy <svn repo url>
{noformat}
{panel}

{panel}
{noformat:title=set svn layout}
git svn clone http://foo.net/code/design --trunk=trunk/proj1 --tags=tags/forProj1 --branches=branches/forProj1 localProjName
{noformat}

{noformat}

git config -l

[svn-remote "svn"]
url = http://foo.net/code/design
fetch = trunk/proj1:refs/remotes/trunk
tags = tags/forProj1/*:refs/remotes/tags/*
branches = branches/forProj1/*:refs/remotes/branches/*

{noformat}
{panel}

h2. Commit your changes
{noformat}git svn dcommit
{noformat}

h2. Update your working copy

{noformat}git svn rebase
{noformat}

h2. Tag

{noformat}git svn tag -m "test tag" test_v1
{noformat}

h2. Branch

{noformat}git svn branch -m "test branch" branch_v1
{noformat}

h1. How to: Keeping a git fork in sync with the forked repo.

When you fork a git repo, probably you would like to keep in sync the forked repo with the original one. This is how I do it.

{noformat}git remote add <repo name> <git url>
{noformat}

h2. Fetch

{noformat}git fetch <repo name>
{noformat}

h2. This will create a branch, so then you just have to merge back:
{noformat}git checkout master
git merge <repo name>/master
{noformat}

h2. Commit those new changes:
{noformat}git commit -a -m "Sync to fork master"
{noformat}

h1. How to: Convert from Subversion to Git

* _+[Article from paul dowman blog|http://pauldowman.com/2008/07/26/how-to-convert-from-subversion-to-git/]+_