Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, 4 July 2018

Create and using a Git repo into a SVN environment

If you want to use Git as your versioning system, you shouldn't only migrate every repository from SVN to Git, but you should also use Git locally. The Git-svn command will help you do this. It so happens that your team doesn't want to change its versioning system, or a project is way too big to migrate on a new versioning system. So, Git has a solution for you; how about using Git features without anyone knowing or caring?
The following diagram explains how to use Git inside an SVN environment. When executing a Git command, the SVN environment will not notice it because the Git-svn command will convert all your commands.

Setting up your repository

You assume that you already have an SVN repository and you want to use Git locally. As a first step, clone the SVN repository using this command:
$ git svn clone -s http://mysvnrepo/svn/myproject myproject_gitsvn_local
The -s option stands for standard layout, which means that your subversion layout has three directories (trunk, branches, and tags). You can, of course, forget this option if your repository does not have a standard layout.
This creates a Git repository under the myproject_gitsvn_local directory that is mapped to the trunk folder of your subversion repository.
As Git doesn't track empty directories, the empty directories under the trunk won't appear inside your Git repository.
Sometimes you might have to clone a big repository. In this case, checking out the commit history will be lengthy because the repository is too big. There is a way to clone it without waiting for a long time. You can do this by cloning the repository with the earlier version of the repository:
$ git svn clone -s -r625:HEAD http://mysvnrepo/svn/myproject myproject_gitsvn_local
There is one last thing to set up. Every file ignored by SVN has to be ignored by Git too. To do this, you have to transfer them into the .gitignore file by using this:
$ git svn show-ignore > .gitignore
There is an alternative method that uses the update-index command:
$ git update-index --assume-unchanged filesToIgnore

Working with Git SVN

Once your repository is ready, you can work on it and start executing Git commands as we saw earlier. Of course, there are some commands to execute when you want to push or pull from the SVN repository. When you want to update your local Git repository, just type this:
$ git svn rebase
To commit back to SVN, use the following command:
$ git svn dcommit
Sooner or later, you will add the .svn folder to the staging area in Git. Hopefully, there is a way to delete it:
$ git status -s | grep .svn | awk "'print $3'} | xargs git rm -cached

Tuesday, 3 July 2018

Managing Git submodules

A git submodule is helpful for a project that requires dependency on another project. For example, this can be a library that was developed by you or another team. It can be hard to manage when the library is updated and you made some custom code inside your project.
Git handles this by using submodules. It allows you to manage a Git repository as a subfolder of another Git repository, which in turn lets you clone a repository isolated from the commits of the current repository.
The following points will be illustrated in below:
  • Adding a submodule.
  • Cloning a project with submodules.
  • Removing a submodule.
  • Using a subtree instead of a submodule.
  • Adding a subproject with a subtree.
  • Contributing on a subtree.

Adding a submodule

Let's imagine you want to add the "myutil" library that helps you development of your feature. The first thing to do is to clone the library's Git repository inside your subfolder:
$ git submodule add https://mygitserver/scm/myutil.git myutil
You now have the myutil project inside the myutil folder.
You can do everything you want inside it, such as add modifications, change the remote repository, push your changes in the remote repository, and so on.
While you add the Git submodule, Git adds two files, myutil and .gitmodules.
Let's see this with the git status command:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>…" to unstage)
# new file: .gitmodules
# new file: myutil
Git sees the myutil folder as a submodule, so it won't track the changes if you're not in this folder. An important fact is that Git saves this addition as a nonregular commit of this repository, so if someone clones your website, Git can recreate the same environment.

Cloning a project with submodules

If you clone a project using submodules Git will add all files other than the submodule files:
$ git clone https://mygitserver/scm/myproject.git
$ ls
myfile.txt myutil
$ cd myutil
$ ls
$
The myutil folder is created, but it is empty. You will have to execute these two commands to initialize the import submodule:
$ git submodule init
$ git submodule update
Your repository is now up to date. Using submodules can be interesting if you want to separate and isolate some parts of your code.
If you execute a git pull command on your project, you will not have the last version of the submodule. To do this, you have to execute git submodule update every time you want to update your submodule.

Removing a submodule

To remove a submodule from your project, you have to execute these steps:
  1. Delete the lines of the submodule from the .gitmodules file.
  2. Delete the submodule part from .git/config.
  3. Delete the submodule from Git by executing this command:
    $ git rm -cached submodule_path
  4. Commit and delete the untracked files.

Using a subtree instead of a submodule

The use of git module is not a best practice because you have to use git submodule update every time, and you will probably forget to do this. The second problem is that Git doesn't really handle merging into a submodule. It detects SHA conflicts, but this is all. It's left to you to find out what should be done.
Thankfully, there is the subtree, which is better in a few ways:
  • Easy to manage for light workflow.
  • While you clone a superproject, the subproject's code is available too.
  • Subtree doesn't use files such as .gitmodules. 
  • The most important point is that contents can be modified inside your project without having a copy of the dependency elsewhere.
Git subtree is available since version 1.7.11 of Git delivered in May 2012.

Adding a subproject with a subtree

Firstly, we need to tell Git that we want to include a project as a subtree. We use the git remote command to specify where the remote repository of this subtree is:
$ git remote add –f myutil_remote https://mygitserver/scm/myutil.git
Now, you can add the subtree inside your project using the remote repository:
$ git subtree add --prefix myutil myutil_remote master --squash
This will create the subproject. If you want to update it later, you will have to use the fetch and subtree pull commands:
$ git fetch myutil_remote master
$ git subtree pull myutil myutil_remote master --squash 

Contributing on a subtree

Obviously, you can commit your fixes to the subproject in the local directory, but when you push back upstream, you will have to use another remote repository:
$ git remote add info-myutil https://mygitserver/scm/myutil.git
$ git subtree push --prefix=myutil info-myutil master
Git push using: info-myutil master
Counting objects: 1, done.
Delta compression using up to 1 thread.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (1/1), 170 bytes, done.
Total 1 (delta 1), reused 0 (delta 0)
To https://mygitserver/scm/myutil.git
.................................................... -} master
Git subtree can be an easy way if you have to frequently update your subproject and want to contribute to it with less effort.

Monday, 2 October 2017

How to exclude files from git using gitignore

During the development phase of the software in addition to the sources there are files that you want to exclude from git as binary files, eclipse settings files etc...
All these files should not be edited, so we can exclude them through a suitable configuration.

Create a local .gitignore 

A soluntion a this issue is the creation of a file with name ".gitignore" in main directory of your repositoriy.

Example

An example of gitignore file is the following:
# Compiled source #
###################
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
*.7z
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs #
########
*.log

# OS generated files #
######################
.DS_Store*
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Refresh cache about gitignore

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:
 git rm --cached <FILENAME>

Create a global .gitingore

Alternatively you can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer. For example create a file like local .gitignore in home with name ".gitignore_global" and run the following command into your shell:
 git config --global core.excludesfile ~/.gitignore_global

Monday, 25 September 2017

How to store your git credentials

Use the following command:
 git config --global credential.helper store
Next time when you will be prompted again your credentials, It will be created ".git-credentials" file in your home.
Now you never have to enter your credentials.

Storage format

The .git-credentials file is stored in plaintext. Each credential is stored on its own line as a URL like:
 https://<user>:<password>@<hostname>

Example

Store your password in your home.

1
2
3
4
5
6
7
8
$ git config --global credential.helper store
$ git push http://yourserver.com/repo.git
Username: <type your username>
Password: <type your password>

[several days later]
$ git push http://yourserver.com/repo.git
[your credentials are used automatically]

Monday, 11 September 2017

Creating a simple server Git repository

As precondition, it is important clarify the following definition:
A server repository, also called "bare repository", is a Git repository without a working copy.
Git can use four protocols to transport data:
  • Local;
  • Secure Shell (SSH);
  • Git;
  • HTTPS.
For all protocols, we have to create the bare repository by executing these lines on the server's command lines. With the following commands, you can create a directory myproject and initialize an empty Git bare repository. Create the directory and go inside it:
$ mkdir myproject
$ cd myproject 
Initialized empty Git repository in /home/user/myproject:
$ git init --bare

Local protocol

The local protocol is the basic protocol; the remote repository is a local directory. This protocol is used if all members have access to the remote repository. Now, every programmer has to clone it in local:
$ git clone /opt/git/myproject.git
For example one of the programmers, has already written some code lines. He has to initialize a local Git repository inside the directory and set a remote location for the bare repository:
develper@local$ git init
develper@local$ git remote add origin /opt/git/myproject.git
This example will be reported for each protocol. To identify it will be called "example of developer".
The following are the pros of the local protocol:
  • Easy to share with other members.
  • Fast access on the repository.
And the cons are:
  • Hard to set up a shared network.
  • Fast only if the file access is fast.

SSH

Secure Shell (SSH) is the most used protocol, especially if the remote repository is on a remote server. Now, every programmer has to first clone it in local: 
$ git clone ssh://username@server/myproject.git
Using the SSH protocol, programmers have to install their SSH keys on the remote repository in order to push to and pull from it. Otherwise, they have to specify the password on each remote command.
The example of developer in this scenario:
develper@local$ git init
develper@local$ git remote add origin ssh://username@server/myproject.git
The following are the pros of the ssh protocol:
  • Easy to share using a remote server.
  • SSH compresses data while transport, which makes it fast.
And the con is:
  • No anonymous access.

Git

The Git transport is similar to SSH, but without any security. You can't push data on it by default, but you can activate this feature. Like in all cases, the programmer has to clone it in local, as follows:
$ git clone git://username@server/myproject.git
The example of developer in this scenario:
develper@local$ git init 
develper@local$ git remote add origin git://username@server/myproject.git
The following is the pro of the git:
  • Faster than the others.
And the con is:
  • No security because the Git transport is the same as SSH but without the security layer.

HTTPS

The HTTPS protocol is the easiest to set up. Anyone who has access to the web server can clone it. The programmers start to clone it in local:
$ git clone https://server/myproject.git
The example of developer in this scenario:
develper@local$ git init .
develper@local$ git remote add origin http://server/myproject.git
The following is the pro of https:
  • Easy to set up.
And the con is:
  • Very slow data transport.

Internal Links

May be of interest to you:

  1. GIT - getting started

Friday, 8 September 2017

GIT - getting started

Git is a version control system (VCS) for code. It is used to keep track of revisions and allow a development team to work together on a project through branches. This fast guide has the scope of show an overview of git using the command line interface (CLI). 

To begin with a clone a remote repository in your directory:
$ git clone http://<username>@<repository_url>
From your directory you can switch branch with:
$ git checkout <branch_name>
If you want create a new local branch:
$ git checkout -b <branch_name>
To show the current branch:
$ git branch
The command shows the branch list and the current branch is that with *. For the status about the files changed
$ git status
When a file has some changes, you can revert your changes with:
$ git checkout -- <file_with_changes>
or you can add it to commit file list:
$ git add <file_with_changes>
now you can commit the file in your local repository:
$ git commit -m "your comment"
To update the local repository with the remote repository:
$ git pull
For merge from a branch with the current branch:
$ git merge <branch_target>
To update the branch on remote repository
$ git push origin <your_branch>
To create a local tag
$ git tag <tag_name>
To add a local tag in remote repository
$ git push origin <tag_name>

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...