Pagine

Wednesday 31 May 2017

Learning maven classifier with examples

The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.
A common use case for classifiers is the need to attach secondary artifacts to the project's main artifact. If you browse the Maven central repository, you will notice that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files.

Example:

The following dependency refers to my-artifact-1.0.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>
The following dependency refers to my-artifact-1.0.source.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>sources</classifier>
</dependency>
The following dependency refers to my-artifact-1.0.javadoc.jar
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>javadoc</classifier>
</dependency>
Another example, we suppose we have two jars with the same artifact id (my-artifact) but different group id as below:
<dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>

<dependency>
    <groupId>com.othercompany</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
</dependency>
Both jars have following name: my-artifact-1.0.jar. The use of classifier can be helpful:
 <dependency>
    <groupId>it.blogspot.informaticaamodomio</groupId>
    <artifactId>my-artifact</artifactId>
    <version>1.0</version>
    <classifier>info</classifier>
</dependency>
So in this case the jar name is "my-artifact-1.0.info.jar" in this way simplifying the distinction between the two jars.

Monday 29 May 2017

How to use the git reset command

The git reset command will allow you to go back to a previous state (for example, commit). The git reset command has three options (soft, hard, or mixed, by default). In general, the git reset command's aim is to take the current branch, reset it to point somewhere else, and possibly bring the index and work tree along. 
This means that
 git reset <paths>
is the opposite of
 git add <paths>
More concretely, if the master branch (currently checked out) looks like the first row (in the following figure) and you want it to point to B and not C, you will use this command:
 git reset B
The more options that you can provide on the reset command can be easily explained:
  • --hard: This option is the simplest. It will restore the content to the given commit. All the local changes will be erased. The git reset --hard command means git reset --hard HEAD, which will reset your files to the previous version and erase your local changes.
  • --mixed: This option resets the index, but not the work tree. It will reset your local files, but the differences found during the process will be marked as local modifications if you analyze them using git status. It's very helpful if you make some bugs on previous commits and want to keep your local changes.
  • --soft: This option will keep all your files, such as mixed, intact. If you use git status, it will appear as changes to commit. You can use this option when you have not committed files as expected, but your work is correct.
  • --merge: this option resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted. In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries.
  • --keep: this options resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is aborted.
The git reset command doesn't remove untracked files; use git clean instead.

Examples

1) Undo add:
 $ git add myfile.txt
 $ git reset
2) Undo a commit and redo:
 $ git commit -m "a comment"
 $ git reset --soft HEAD^
 $ git commit -a -c ORIG_HEAD
3) Undo a commit, making it a topic branch:
 $ git branch atopicbranch    
 $ git reset --hard HEAD~3  
 $ git checkout atopicbranch
4) Undo commits permanently:
 $ git commit ...
 $ git reset --hard HEAD~3
5) Undo a merge or pull:
 $ git pull
 Auto-merging ...
 CONFLICT (content): Merge conflict in ...
 Automatic merge failed; fix conflicts and then commit the result.
 $ git reset --hard
 $ git pull . atopicbranch
 Updating from 81683... to 43984...
 Fast-forward
 $ git reset --hard ORIG_HEAD
6) Undo a merge or pull inside a dirty working tree:
 $ git pull
 Auto-merging ....
 Merge made by recursive.
  ...                |   20 +++++----
  ...
 $ git reset --merge ORIG_HEAD
7) Interrupted workflow:
 $ git checkout feature ;
...developement...
 $ git commit -a -m "snapshot"
 $ git checkout master
...bug fixing...
 $ git commit 
 $ git checkout feature
 $ git reset --soft HEAD^ ;# go back to before bug fixing 
 $ git reset
8) Keep changes in working tree while discarding some previous commits:
 $ git tag 1.0
 $ git checkout -b branch1
... edit one o more files...
 $ git commit -m "a comment"
... edit one o more files...
 $ git checkout -b branch2
 $ git reset --keep 1.0
9) Split a commit apart into a sequence of commits:
 $ git reset -N HEAD^
 $ git add -p
 $ git diff --cached
 $ git commit -c HEAD@{1}
...
 $ git add file.txt
 $ git diff --cached
 $ git commit -m "a comment"

Friday 26 May 2017

How to revert uncommitted changes with git

Let's suppose you edited a file on the production working directory, but didn't commit it. On your last push, you edited it, and the changes in production aren't needed anymore. So, your goal is to erase changes on this file and reset the file to the last committed version:
$ git checkout myfile.txt
This command is really nice if you want to restore a deleted file. You can also specify a commit pointer to use (useful if you stash [1] your changes):
$ rm myfile.txt
$ git checkout HEAD myfile.txt

Link

[1] Stashing your changes with git

Thursday 25 May 2017

Stashing your changes with git

Git has a command that allows you to save the current state of the local working repository and go back to the last committed revision with git stash. This is really helpful when you have to develop an urgent fix. After this, you can restore the stashed changes and continue with your development.
To use this command, just execute the following command snippet:
$ git stash
Do your fix and then unstash edit:
$ git stash pop
Of course, you can do more with this command, such as save a list of stashes:
$ git stash
See the list of available stashes
$ git stash list
Apply the second stash
$ git stash apply stash@"1}
Delete a stash
$ git stash drop stash@"1}
Or delete all stashes
$ git stash clear
Let's suppose that your stash concerns a feature and you haven't created a dedicated branch for it. You can simply create a branch executing this command:
$ git stash branch mynewbranch
Remember this command is only for urgent fixes. If you want to add a new feature, you should use a new branch.

Wednesday 24 May 2017

How to use the Cherry-pick git command

The cherry-pick git command lets you select a commit from a branch to apply it to another. The patch will be considered as a new commit in the selected branch.
The synopsis is:
git cherry-pick <id_commit>

Example

Let's try to understand this by exploring the following example: Developer1 creates the deve1 branch from master and adds a new file in it:
$ touch file.txt
$ git add file.txt
$ git commit -m 'add file.txt'
$ echo "new line" > file.txt
$ git commit -a -m 'updated file.txt'
As you can see, the developer creates the file.txt, adds it into Git, and commits it.Then he edits it and commits again. Now, let's see the commit history for this branch.
$ git log --oneline
5f8ed47 add file.txt
44c67b9 updated file.txt
Now, the developer will apply the first commit to the master branch:
$ git checkout master
$ git cherry-pick 44c67b9 
Let's imagine that the cherry-pick went wrong and the developer wants to abort it:
$ git cherry-pick --abort 
However, if you want to roll back a cherry-pick, you have two ways to do it:
  • If it's in a private branch, you can use the git rebase command.
  • If it's already in a public branch, use the git revert command.

Tuesday 23 May 2017

Git - rebase vs merge

The merge command combines the changes from two branches. Instead the rebase command will take the changes from the first branch and apply them to the other branch.

The rebase command can be used if you want to get a feature (commits) from one branch to another and be sure to be close to the tip of the upstream branch. The command applies the changes from the branch called branch to the master branch.

The following examples show the usage of these two commands.

Example of merge:
$ git checkout mybranch
$ git merge master
Example of rebase:
$ git checkout mybranch
$ git rebase master
Note: In the rebase command do not merge commit.

Monday 22 May 2017

How to create a patch with git

When a developer to make small fixes in a part of the project, but he didn't want to give him access to the repository. In this scenario the developer can create a git patch and send it via mail.
The following command will create .patch files per commit:
$ git format-patch origin patch-myproject.patch
Later you can send the patch by mail:
$ git send-email patch-myproject.patch
The patch can be imported with by executing this:
$ git apply /tmp/patch-myproject.patch
This command will apply the patch, but it doesn't create commits. So, to generate a series of commits, use the git am command:
$ git am /tmp/patch-myproject.patch

Friday 19 May 2017

How to removing a file with git

If you don't want a file anymore, there are two ways to remove it. First mode: delete the file manually and commit the changes. This will delete the file locally and on the repository. Use the following command line:
$ rm myfile.txt
$ git commit -m 'delete the file with name: myfile.txt'
Second mode: delete the file only through git:
$ git rm --cached myfile.txt

Thursday 18 May 2017

Git - fatal: refusing to merge unrelated histories

During a merge between two branches, if the following error is returned:
$ git branch
* master
develop
$ git merge develop
fatal: refusing to merge unrelated histories
Error redoing merge <version-id>
Retry with the following option:
$ git merge develop --allow-unrelated-histories
This option is used in a rare event that merges histories of two projects that started their lives independently.

Monday 15 May 2017

How to revert a commint with svn

Yuo can revert one a more commits with the following command:
 svn merge -r <current_version>:<target_version> <folder>
After that you can commit the revert.

Example:

 svn merge -r 305:298 .
 svn commit -m "revert..."

Thursday 11 May 2017

How to migrate a branch in a new git repository

For a reason with almost zero probability but not impossible you may find yourself in the situation of having to migrate a single branch between two git repositories.
Follow these steps for migration:
 git clone <old_repo>
 git checkout <branch_target>
 git remote set-url origin <new_repository>
 git push origin <branch_target>
Now clean your workspace, clone the old repository and remove branch target.
See the previous post "How to delete local and remote branches with git" for more details about delete of a branch.

Tuesday 9 May 2017

How to change svn user in TortoiseSVN client (Windows)


TortoiseSVN is a Subversion Client for Windows. After first svn checkout it requires the credentials to be stored. These credential is used for future operations (update, commit, etc...). If you use a second repository but a different svn account, TortoiseSVN uses automatically the credentials already stored and you will probably have an error access (403 forbidden).
The soluntion is the following:
  1. Right click
  2. TortoiseSVN
  3. Settings
  4. Saved Data
  5. Authentication data - Clear
  6. Svn operation (update, commit, etc...)
  7. Put new credentials

Monday 8 May 2017

Svn Error: E155036 - When it happens

The svn error with code "E155036" is thrown when you use a local copy created before an upgrade of svn server.
A typical scenario is the following:
  1. A developer creates his local copy.
  2. SVN Admin performs the svn server upgrade
  3. After the upgrade, the developer tries to use his local copy and svn throws the error "E155036".
The solution is very easy and it is shown in error description but the focus of this post is on the scenario just described.

> svn status
svn: E155036: Please see the 'svn upgrade' command
svn: E155036: Working copy '/home/user/myproject' is too old (format 10, create
d by Subversion 1.6)
> svn upgrade
Upgraded '.'
Upgraded 'A'
Upgraded 'A/B'
Upgraded 'A/B/C'
.... 

Friday 5 May 2017

How to rename a tag with git

The tag rename operation is not atomic but more steps are needed.
1) Create the tag with new name from old tag:
 git tag <new_tag> <old_tag>
2) Delete the old tag from local repository:
 git tag -d <old_tag>
3) Delete the old tag from remote repository:
 git push origin :refs/tags/<old_tag>
4) Push the local tag in remote repository:
 git push --tags
5) Lastly synchronize the tag list among local and remote repositories:
 git fetch -f

Thursday 4 May 2017

How to create a branch with git

Create a local branch from current branch:
 git checkout -b <new_branch>
Otherwise you can use the following command:
 git checkout -b <new_branch> <start_point>
where the start point can be an other branch or a tag.
Now if the new branch must become a remote branch then use also the following command:
 git push origin <new_branch>

Wednesday 3 May 2017

How to delete local and remote branches with git

Delete a local branch

Use the following command to delete a branch already merged:
 git branch -d <branch_name>
If the branch is not merged, use -D option to force the delete:
 git branch -D <branch_name>

Delete a remote branch

Use one of following commands to delete a remote branch (valid with git version 1.7 or later):
 git push origin --delete <branch_name>
or
 git push origin :<branch_name>

Monday 1 May 2017

Git - How to get the branch remote list

Who knows the basic features of git know that branches can be of two types: local and remote.
Use the following command for the list of local branches:
 git branch
Instead use the option '-r' for remote branches:
 git branch -r
Finally the option '-a' for full list (local and remote branches):
 git branch -a