GIT EXPERIENCE

Useful Information

This page has been generated by maven-confluence-plugin

Table Of Contents

How to: Sync local repo with remote one

git fetch --verbose --prune
-p, --prune

After fetching, remove any remote-tracking branches which no longer exist on the remote.
From Stack Overflow

How to: Clone

Clone a project

git clone <git_url> [directory]

Clone a single branch

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

How to: Submodules

Add a Submodule

git submodule add <git_url>

Pull latest of all submodules

from ver 1.7.3
git pull --recurse-submodules
from ver 1.6.1
git submodule foreach git pull origin master

Tutorial

Git Tools Submodules Tutorial

How to: Patch

Simulate patch

git apply --stat fix_file.patch

Check patch

git apply --check fix_file.patch

Commit patch

git apply fix_file.patch

How to: Diff

Show only changed files

git diff --name-only <branch>
git diff --name-only <commit1> <commit2>

How to: Ignore after commit

git rm --cached <files pattern>

How to: Revert

Revert All Uncommitted Changes in Working Tree and Index

git reset --hard

Remove all Untracked Files and Directories

git clean -fd

How to: Branches

Create a new branch from current

git branch -b NEWBRANCH

Create a new empty branch

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

List branch

git branch    ' list local
git branch -r ' list remote
git branch -a ' list local + remote

Comparing Git Branches

Display diff change(s)

git diff branch1..branch2

Display diff file(s)

git diff --name-status branch1..branch2

Display diff detail(s)

git diff --stat --color master..branchName

Push local branch to remote repository

git push -u origin _local_branch_

Remove local/remote branch

To remove a local branch from your machine:

git branch -d the_local_branch

To remove a remote branch:

git push origin :the_remote_branch

Pull a remote branch

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

git pull <repo> <remotebranchname>:<localbranchname>

Example:

git pull origin rxyz:lxyz

When you are on the master branch you also could first checkout a branch like:

git checkout -b lxyz
git pull origin rxyz

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.

How To: Tags

Create a new branch from a tag

git checkout -b newbranch tagname

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:

git tag -d mytag
git push origin :refs/tags/mytag

That will remove ‘mytag’ from the remote repository (e.g. Github).

Create the tag:

git tag -a 1.0 -m'release 1.0'

push your tag to the remote repository:

git push --tags

Download tag as zip archive

git archive --format=zip  tag_name > output_file

Branching, Merging & Development strategy (from StackOverflow)

One good strategy during development is to never works directly on master branch.

Always we have to create a new branch for new code:

git checkout -b topic/topic_name master

From there, I can push out the changes to public repositories:

git push <public git url> topic/topic_name

Or eventually just merge it back in master branch:

git checkout master && git merge topic/topic_name

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:

git branch -m master junk
git co -b master old_sha1_value

To remove local & remote branch

git branch -d the_local_branch
git push origin :the_remote_branch

Taken from StackOverflow.

How To: Switch “origin” of your GIT repository

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

How To: Stashing your changes (original post)

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.

git stash save "work in progress"

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.

... edit and test ...
git commit -a -m "your comment"

After that, you can go back to what you were working on with git stash apply:

git stash apply

Stash Queue

You can also use stashing to queue up stashed changes.

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

Then you can apply them individually with

git stash apply stash@{1}

Clear out the list with ''.

git stash clear

How to: svn project as git repository

Clone

git svn clone [-s] <svn repo url>
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.
clone svn revision
git svn clone -r<revision number> <svn repo url>
show revision history
> svn log --stop-on-copy <svn repo url>
set svn layout
git svn clone http://foo.net/code/design --trunk=trunk/proj1 --tags=tags/forProj1 --branches=branches/forProj1 localProjName
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/*

Commit your changes

git svn dcommit

Update your working copy

git svn rebase

Tag

git svn tag -m "test tag" test_v1

Branch

git svn branch -m "test branch" branch_v1

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.

git remote add <repo name> <git url>

Fetch

git fetch <repo name>

This will create a branch, so then you just have to merge back:

git checkout master
git merge <repo name>/master

Commit those new changes:

git commit -a -m "Sync to fork master"

How to: Convert from Subversion to Git

Labels

git git Delete
scm scm Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.