I wanted to publish this post couple of months earlier, but forgot about it 🙂 I hope that informations below are not very outdated and you can use it for something.
When I started using git, I was just unable to use other source version control system, because of their lack of functionality or performance (comparing to other I used to work with during my professional career: svn, cvs, cmvc, perforce). At one of my previous companies I used to work for, they have all projects configured under perforce system. I just had to find the way to use git as my local repository. After reading couple of blog posts and searching through forums I found really simple method to do this.
First of all, you have to install p4 command line tools (I assume, you already have p4 gui client installed). You can find them here: http://www.perforce.com/downloads/complete_list
For Mac OSX, you can also use MacPort or Homebrew.
Next step is to configure environment variables (I assume again, that you already have access to p4 server and you have configured your workspace). Place following variables (with values matching your settings) within your ~/.bashrc file, or similar if you are using shell different than bash:
export P4CLIENT=my.cool.workspace #workspace name export P4PORT=10.0.0.5:1666 #p4 server ip and port export P4USER=user.name #p4 user name #export P4PASSWD= #your p4 password export P4EDITOR=vim #command line text editor you like
It’s better not to place password here. Without P4PASSWD variable, from time to time you have to use “p4 login” command to authenticate to p4 server. It’s more secure.
to verify if the workspace mapping is correct. For other settings verification use this command:
You have p4 command line client configured, now it’s time to install p4-git. It’s located in git repository on github, so clone latest git repository to your favourite location with command:
git clone https://github.com/git/git.git
One more step. create symbolic link to the git-p4.py script:
sudo ln -s <cloned_git_repository_location>/git-p4.py /use/bin/git-p4
Now it’s time to get p4 repo and start using it! To clone p4 repository, use command:
git p4 clone //path/to/project/at/p4
of course using your path to project. Adding “@all” at the end of the path will get all the project’s history into your local git repository:
git p4 clone //path/to/project/at/p4@all
That’s it! You have full featured local git repository and you can do all that nice rebase, stash, merge, branch things.
To download latest changes from the p4 server (like git pull), you have to run command:
git p4 rebase
Pushing your commits (like git push) to p4 server its done with:
git p4 submit
Have a nice giting! 🙂
I’m working with git in termial only. Git command line client is fast, reliable and have all the features in comparison with any gui git client.
On the other hand, it’s nice to have some colors to quick distinguish between different type of information, i.e. when looking at git log. Following git log command nicely outputs history of commits with most important informations together with branch information:
git log --pretty=format:'%C(yellow)%h%Cred%d%Creset - %C(cyan)%an %Creset: %s %Cgreen(%cr)'
Following one prints commit history as colored graph:
git log --pretty=oneline --decorate --graph
You can either alias these commands in bash or any other shell you use or alias as git commands in .gitconfig file in [alias] section.
Git is my favorite SCV system up to now and most probably will be in the future. Besides of many great features is has pretty good automatic merge tool, that is able to solve conflicts, but not all of them. I found p4merge tool much better in case of automatic merging and manual solving the most hard conflicts. Is is very simple to mix together those two things to have the best from both of them and now I will show you how to do that.
First of all, you have to install p4merge tool. Go to p4merge tool download page, choose “Browse by platform” tab, pick your platform (in my case it was Macintosh) and download P4V: Visual Client. After downloading, open dmg file and copy p4merge tool to Applications folder in your Dock.
Second step is to create two simple bash scripts: p4merge and p4diff, that will be executed when git needs merge or diff tool. Create these scripts in directory that is in your’s system PATH (i.e. /usr/local/bin/).
#!/bin/sh /Applications/p4merge.app/Contents/MacOS/p4merge $*
#!/bin/sh [ $# -eq 7 ] && /usr/local/bin/p4merge "$2" "$PWD/$5"
Don’t forget to add executable mode/flag to these scripts.
After p4merge and scripts are ready it’s time to configure global git settings. Edit ~/.gitconfig file and put following configuration there for merging:
[merge] tool = p4merge keepBackup = false [mergetool "p4merge"] cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED" keepTemporaries = false trustExitCode = false keepBackup = false [diff] external = p4diff
If you don’t want to use p4merge as diff tool, just comment (using # character) [diff] section. Sometimes it’s faster for me to use command line diff instead of visual one.
Now, when you type “git diff” or “git mergetool” in your’s project directory, p4merge tool will be started.
I like to work in text console and I’m usually using bash and git for my projects. When I have to switch between branches couple of times during the day and made changes I have to be careful which brach I have currently checked out. It can be done quickly by just typing “git branch”, but I don’t like to type it every time I need to do something.
One of the solution is displaying current git branch in bash prompt. My fouvrite one is as follows:
PS1='\[\e[32m\]\w `git branch 2>/dev/null | grep -e '\''^*'\'' | sed -E '\''s/^\* (.+)$/(\1) /'\''`\n\$\[\e[0m\] '
The interesting part is between “`” marks. This command lists all git branches, greps only the current one (with asterisk) and creates string with sed.
This prompt will not print anything about git branches if you are currently in the folder not handled by git repository. Normally above prompt will look like this:
If I will modify my prompt in the future, you can find the latest version here.
I have to work with SVN at my current job, but my favorite source control is Git. To enjoy my work I’m using locally Git repository and remotely centralized SVN one. There is many guides how to do it, here is one of them: Effectively Using Git With Subversion.
We have to follow pre-commit code review rule, so we are doing it by publishing patches. Unfortunately, merge tool from Windows’ Tortoise SVN does not recognize patches created by Git, wich on the other hand are properly recognized by unix tools like patch. To make it all work, I created simple Ruby script to convert Git patch to SVN patch standard.
Link to this script can be found here.
Usage is as follows:
During my first project created in X-Code which I wanted to store in public GitHub repository I encountered issue with showing content of my source files on GitHub web page. After contacting with GitHub support (btw, they answer really fast and are helpful) they pointed out my line endings.
Quick recon brought surpassing results. I realized that even modifying single line of code in the source file, git shows modification in the whole file, because it sees this file as a single line (Mac line endings was shown as ^M). Why I didn’t noticed it earlier? Don’t know. I’m a noobie on Mac.
The first solution for GitHub web page was to set following git config attribute:
git config --global core.autocrlf input
It should commit files with Unix line endings. I know it works in Windows, but somehow it hadn’t worked for me on my Mac. Creating new project from scratch didn’t help either and even if it would help for files committed to the repository, my local git problem is still not resolved.
The solution was to set in X-Code configuration (Command + “,”) line ending for new and existing files to Unix (LF) instead of Mac (CR):
It solved issues with newly created files, but old ones was still invalid (maybe someone will explain me how this “For existing files” option works). Following command executed on all current files, which simple swaps line endings, solved the issue:
tr '\r' '\n' < <old_file> > <new_file>