SCM

Git recipes

From Forge Wiki

Jump to: navigation, search

This page is made to give useful command or succession of commands to perform Git tasks in the context of the CamiTK Git workflow.

See also the starter guide for using git in CamiTK and the Git Reference page


Be sure to follow both:


Contents

Source Code management

File:CamiTKGitStream.png

Clone the CamiTK repository and switch to develop branch

Let's say that we want to clone the remote repository in a local directory named "src". If you want to push back your work, you need to identify yourself with your forge account.

# R/W clone
# replace "developer" with the name of your account on the forge:
$ git clone git+ssh://developer@scm.forge.imag.fr/var/lib/gforge/chroot/scmrepos/git/camitk/camitk.git ./src
# Now you are on master branch so switch to develop one with tracking from origin/develop
$ git checkout -b develop origin/develop


or the clone can be done anonymously, in this case you will not be able to push back your work:


# anonymous clone
$ git clone -b develop --single-branch https://forge.imag.fr/anonscm/git/camitk/camitk.git ./src
# You only have the develop branch, there is no need to switch

Visualize Git work tree under Linux

Under Linux use qGit or git-cola

get qgit:

$ sudo apt-get install qgit
$ qgit

If your distrib does not have the package yet, get the source code at https://github.com/feinstaub/qgit .

Otherwise you can do this for git-cola :

$ sudo apt-get install git-cola
$ git-cola

Visualize the CamiTK git origin on the web

You can check the latest push on origin here:


Set VIM as default message editor

$ git config --global core.editor "vim"
$ export GIT_EDITOR=vim

List all local branches and display last modified dates

From stackoverflow

for k in `git branch | sed s/^..//`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k --`\\t"$k";done | sort

Develop

Two types of development process are explained in this section:

  • develop something new: you need to create a new feature
  • fix a bug on the current development branch

The only thing that will change is how you name the local branch. Be sure to follow these rules for naming your branch.


Develop a new feature during the develop process

You need to create a specific local branch (see below, Create a branch). Following the rules for naming your branch.

Two cases:

    • if the features was declared as a story in CamiTK agile management system: the branch should be named feature#<artifactID> (feature#321 for example). Note: if the new feature was opened on bugzilla, first create an artifact in the CamiTK agile management system.
    • If the new feature was not declared as a story, the branch should be named feature/XXX where XXX is a comprehensible (although rather short) description (e.g., feature/connect-to-apple-store).


Fix a Bug during the develop process

Just proceed like for a new feature, but be careful to follow these rules for naming your branch.

Two cases:

    • if the bug was opened on bugzilla, the branch should be named bug/# followed directly by the bugzilla bug id (e.g., bug/#181). Note: an artifact in the CamiTK agile management system should also has been created.
    • if the bug was not opened on bugzilla, the branch should be named bug/XXX where XXX is a comprehensible (although rather short) description (e.g., bug/parallel-build).


Create a branch (locally)

As you are on the develop branch, the most usual case to work with Git is to do the job on a separate branch called feature branch creates from develop branch.

  1. Choose a proper name for your branch (see here )
  2. Create the new branch and switch to the new branch:
$ git checkout develop
$ git branch feature/#42
$ git checkout feature/#42

This can be done in a single operation with checkout command and the -b option:

$ git checkout -b feature/#42 develop

You can add a detailed description to your branch with:

git branch --edit-description

(note that you can custom "git branch" to show your description, see this stackover flow entry).

Merge local (finished) feature on origin develop

Verify that all work is on a versioned state on your feature branch. If everything is ok do the following:

# Step #1 update local develop branch
$ git checkout develop
$ git pull origin develop
 
# Step #2 rebase your feature to the upstream develop branch
$ git checkout feature/#42
$ git rebase develop
 
# Step #3 go back to the develop branch...
$ git checkout develop
 
# Step #4 ...to merge your work
$ git merge --no-ff feature/#42
$ git push origin develop

You can check on the CamiTK git develop branch web page that your branch was merged. Don't forget to follow the continuous integration result.

git rebase essentially reworks your branch so that it looks as if you'd started with the current upstream develop branch instead of an older version (the one you had when you started your branch), thus eliminating merge noise.

Do not forget to delete the feature/#42 after your merge (see below)

Delete branch after a merge on develop

$ git branch -d bug/#21

Note: if you did not commit all your modification, there will be a message (you can then decide if you want to remove all and force the deletion using the -D option instead).


Work on my feature branch

The coding work you will do has to be versioned. To do so, your branch is at your disposal.

Updating the git local index (staging new content)

You don't need to perform an "add" command if the only thing you have done is to modify an already versionned file(s). To add a new single document you created.

$ git add myDocument.cpp

To add a set of documents created in the current directory. For example, let's say that you have created a file called "myFile.cpp" plus a dir called "testDir" and files in it. This command will stage all the new files and dir in the current dir.

$ git add .

You can replace the "." with a relative or absolute path.

Committing changes and adds

To commit all these changes loccaly, we perform a commit:

$ git commit -m "meaningful commit message"





Release

Create a specific release branch

We work only on the shaping of the release, only bug corrections, no new feature.

$ git checkout develop
$ git branch release-4.0.0

Release a new CamiTK version

Once the release is ready, follow the three following steps. 1) push release code on master branch and add tag:

$ git checkout master
$ git pull origin master
$ git merge --no-ff release-4.0.0
$ git push origin master
# Add a new tag for this release, the message should follow this example template:
$ git tag -a 4.0.0 -m "RELEASE 4.0.0 With a message that explain what are the main new cool features of this release"
$ git push origin 4.0.0

2) Push modifications back to develop branch from the new release branch:

$ git checkout develop
$ git merge --no-ff release-4.0.0
$ git push origin develop
# do not forget to increment the CamiTK version number in the top level CMakeLists.txt

3) Delete release branch:

$ git branch -d release-4.0.0
$ git push origin --delete release-4.0.0

Fix a Bug

You are in the release process and your are correcting a bug: just follow the branch naming convention.

If you fixed an important bug that you want to commit immediately to the develop branch, follow the steps below to make your corrections available in the develop branch without waiting the end of release process

Verify that all work is commited on your feature branch. If everything is ok do the following:

$ git checkout develop
$ git pull
$ git merge --no-ff release-4.0.0 # You will have to give a merge message
$ git push origin develop
$ git checkout release-4.0.0 # come back to release branch and continu your work

Try a release

If a release is getting prepared on origin (that is if you are working with some other developer on a specific release and this developer already pushed some changes on the release branch on origin), this is how you get a local copy to try it on:

git pull
git checkout -b release-4.0.0 origin/release-4.0.0






Hotfix

Create a specific release branch

From the master branch create a new hotfix branch by respecting the branch naming convention.

$ git checkout -b hotfix-4.0.1 master # Switched to a new branch "hotfix-4.0.1"

Release a new CamiTK hotfix version

Make your strategic corrections, committing your modifications as you go.

Once the release is ready, follow the three following steps.

1) push release code on master branch and add tag:

$ git checkout master
$ git pull origin master
$ git merge --no-ff hotfix-4.0.1
$ git push origin master
# Add a new tag for this hotfix release, the message should follow this example template:
$ git tag -a 4.0.1 -m "HOTFIX 4.0.1 With a message that explain in what the hotfix correct the release"
$ git push origin 4.0.1

2) Push modifications back to develop branch from the new hotfix branch:

$ git checkout develop
$ git pull origin develop
# rebase your hotfix to the upstream develop branch
$ git checkout hotfix-4.0.1
$ git rebase develop
# merge on develop
$ git checkout develop
$ git merge --no-ff hotfix-4.0.1
$ git push origin develop

3) Delete hotfix branch:

$ git branch -d hotfix-4.0.1
$ git push origin --delete hotfix-4.0.1

Try a hotfix

If a hotfix needs to be verified/tested/modified by more than one developer (that is one need more than one developer to work on a specific hotfix), the main hotfix developer should first push the hotfix branch on origin:

git push origin hotfix-4.0.1

Once the hotfix branch is on origin, this is how to get a local copy to verify/test/modify it:

git pull
git checkout -b hotfix-4.0.1 origin/hotfix-4.0.1

Git snippets

This is a small collection of little recipes for current git operations.


Undo/Cancel the last commit

Simply use this command to completely undo the last commit and erase history.

$ git reset --hard HEAD~1

Note that ~ replaces the minus (-) sign, so this means reset to HEAD - 1 commit. You can also

Simply use this command to undo the last commit but leave the history (and your files)

$ git reset --soft HEAD~1


Undo (uncommitted) changes

For instance, you have accidentally removed a file. To revert changes to modified files:

# Becareful: It resets the index and working tree. Any changes to tracked files in the working tree since the last commit are discarded.
git reset --hard

This is the same as git reset --hard HEAD.

Clean the current working tree

Use with care

# check first what is going to happen
git clean -nd
# if ok, do it, f=force, d=remove directories
git clean -fd

Rename a branch

$ git branch -m compile-on-sid <newName>

Diff between two branches

There are a lot of different ways to compare branches:

git diff b1..b2
git diff b1...b2
git log b1..b2
git shortlog b1..b2

To get only the filenames that have changed:

git diff --name-status b1..b2

See this stackoverflow answers

Solve a conflict after a pull

Just type:

git mergetool

Running "git mergetool" will prompt the user for each conflict, and propose a default application. On linux, the recommended tool to use is kdiff3.


Copy commits from one branch to the current branch

Just type:

git cherry-pick commit-hash

to apply the commit commit-hash (made on another branch) to your current branch.

It is also possible to copy a range of commits from one branch to the current branch.

Powered By FusionForge