-
Git: merge two repositories
Today I had to merge changes from one repository into another. Let’s assume you want to merge
betaintoalpha.Operations are performed in repo alpha:
git remote add beta_repo git@rosipov.com:beta.git git fetch beta_repo git merge beta_repo/masterIn this case,
beta_repois the name you pick for remote.If you just need to cherry-pick a certain commit from
betayou can omit the last step and replace it with the cherry-pick.More on the topic of remotes: http://git-scm.com/book/ch2-5.html.
-
GPG Usage
To encrypt and decrypt files in Linux there is a utility called
gpg(Gnu Privacy Guard). This is a short GPG tutorial.Quick usage example
gpg -c foo.txtIt will prompt you for the passphrase and a confirmation. Now you will have the encrypted
foo.txt.gpgfile. To decrypt a file:gpg -d foo.txt.gpgThis will forward the output to the console. You can output it into a file:
gpg -d foo.txt.gpg > foo.txt
GPG keyring
This is all secure, but not quite enough if you are paranoid. Keys are what makes
gpggreat. Let’s generate a private key:gpg --gen-keyAnd create an ASCII version of a public key:
gpg --armor --export "John Doe" --output johndoe.txtPublic key
johndoe.txtcan be freely distributed. Now you can encrypt files for yourself only:gpg -e -r "John Doe" foo.txtNow if you decrypt a file it will require the passphrase you specified while generating a key. To encrypt a file for someone else you should have this person’s public key.
Let’s assume Stan Smith sent you a key,
stansmith.txt. You import it using:gpg --import stansmith.txtAnd encrypt the file:
gpg -e -r "Stan Smith" foo.txt -
C strtok usage example
I had some issues with the
strtokfunction. Here’s a detailed explanation and usage for it.The
strtokfunction is used to tokenize a string and thus separates it into multiple strings divided by a delimiter.#include <stdio.h> #include <string.h> int main() { int j, i = 0; char delim[4]; char user_input[81], *token[80]; user_input[0] = 0; // set first byte to 0 to detect empty string // validate the string length while (strlen(user_input) <= 1 || strlen(user_input) > 82) { printf("Feed me a string to tokenize: "); fgets(user_input, sizeof(user_input), stdin); } printf("And a delimiter (up to 4 chars): "); fgets(delim, sizeof(delim), stdin); token[0] = strtok(user_input, delim); // first call returns pointer // to first part of user_input // separated by delim while (token[i] != NULL) { i++; token[i] = strtok(NULL, delim); // every call with NULL uses // saved user_input value and // returns next substring } for (j=0; j<=i-1; j++) { printf("%sn", token[j]); } return 0; }Let’s compile and execute it:
Feed me a string to tokenize: foo/bar/baz And a delimiter: / foo bar bazThe first call to
strtokreturns the pointer to the first substring. All the next calls with the first argument beingNULLuse the string passed at the first call and return the next substring. The function returnsNULLif no more substrings are available. -
Create gitolite repository
A reminder on how to initialize a fresh gitolite repository, assuming that gitolite has already been set up.
All actions are performed on a local machine. In this case:
~/gitolite-adminis admin repository,~/foois desired repository,rosipov.comis gitolite hostname. Commandvistands for the text editor, but you may use whichever editor you prefer.cd ~/gitolite-admin vi conf/gitolite.confAdd lines (obviously you may want to use individual users instead of @all):
repo foo RW+ = @allSave it. Next:
git add conf/gitolite.conf git commit -m "Add foo repo for @all" git pull --rebase && git push mkdir ~/foo cd ~/foo git init git remote add origin git@rosipov.com:foo.gitAdd some files at this point. In this example, only
.gitkeepis added.git add .gitkeep git commit -m "Initialize repo" git push origin masterThe new repository is all set up now.
-
GUI git difftool for Windows
A quick note on how to set up GUI difftool to use with git on Windows (Git Bash, Cygwin, etc…).
Download and install GUI diff tool of your choice, get the path to executable.
Create
difftool.shin directory included in your path (for exampleC:\Users\{username}\binin Git Bash). Let’s take SourceGear’s DiffMerge as an example.#!/bin/sh "C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe" "$1" "$2" | catAnd in your ~/.gitconfig:
[diff] tool = diffmerge [difftool "diffmerge"] difftool.sh "$LOCAL" "$REMOTE"And difftool is available via
git difftoolcommand now.