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

Thursday, November 20, 2014

Git Filter-Branch Saves The Day Again

Disclaimer

You can really mess things up, make sure you back up your files/repositories before you wield this axe.

Removing a large file from a Git repository

Recently I committed and pushed a commit to my remote repository. The push took an unusually long time. Looking at the commit, I discovered that I had committed/pushed a vagrant box into the repository. Doh! Now the repository was ~500MB. Here is what I did to clean up the repository and remove the large file.

I can't remember where I found this command (I may have assembled it from many locations). It is supposed to remove all references and the file from the repository.
git filter-branch --prune-empty -d /dev/shm/scratch --index-filter "git rm --cached -f --ignore-unmatch ubuntu-precise32-intpon.box" --tag-name-filter cat -- --all
This seemed to clean up the git tree, but didn't actually remove the file from the repository. So, on we go...

I ran across an Atlassian page which detailed several new steps to remove a large file. I skipped the first 3 steps because the above command seemed to do the same thing.

The next step was to prune all of the reflog references from now on back.
git reflog expire --expire=now --all

Then repack the repository by running the garbage collector and pruning old objects.
git gc --prune=now

Finally, push all your changes back to the remote repository.
git push origin master --force

Looking at my repository, it was back at ~45MB. I can pretend it never happened and life is good again. As long as I don't tell anyone about it.

Changing the author information

If you are committing to a public repository, you may not want your private email address exposed to the world. GitHub's change author info page has an excellent script that can fix that issue if you accidentally commit with the wrong email address. In case the page changes or disappears, here is the script:
#!/bin/sh
 
git filter-branch --env-filter '
 
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
 
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Once it completes, you need to push the changes to the remote repository.
git push --force --tags origin 'refs/heads/*'
Your email is now changed.

Thursday, October 3, 2013

XCode development with Team Foundation Server

Occasionally, I do work on multiple platforms and historically mixed development has been an issue when your environment requires TFS. However, with TFS supporting Git, it should make that development much easier. I just ran across a couple posts that with instructions on how to use TFS w/ XCode and thought I would share.

Microsoft has a page dedicated to making this happen: (UPDATE: 2016-05-05, Microsoft moved the page) https://www.visualstudio.com/get-started/code/share-your-code-in-tfvc-xcode http://tfs.visualstudio.com/en-us/learn/use-git-and-xcode-with-tfs.aspx . I don't have a Team Foundation Server at my disposal, but it looks like you should be able to do this with onsite Team Foundation servers.

In projects with .Net code and iOS (and/or potentially Android), it might be a good idea to keep the code in separate repositories. It appears that Team Foundation Server 2012 supports multiple Git repositories with a single TFS team project.

http://stackoverflow.com/questions/17591461/can-you-add-multiple-git-repositories-to-a-team-project-in-tfs-tfs-service
You can create multiple git repositories under a single Team Project. Navigate to the Code Explorer, and locate the repository chooser in the web interface and select Manage Repositories...

<repository chooser image>

From the repository manager, you can add a new repository:

<team Project Version Control tab image>

This, of course, is provided that the Team Project uses Git as the version control provider - you can't mix and match Git repositories and Team Foundation Version Control in a single Team Project.

If you have an existing project w/ TF source control, you are out of luck. You can't do multiple TF SC repositories or have a TF and Git repository, so the only option is Git for this.

It is nice to see Microsoft embracing an incredibly powerful tool like Git. Just need to be weary about them reverting back to the days of the 3 E's.

Tuesday, March 26, 2013

Create Remote Git Repository From Existing Local Repository

I ran across a situation where I needed source control in the absence of a TFS. So off to my favorite DVCS, Git. It worked great except I want to have a backup off of my computer. I know that Git can have remote repositories and I know how to pull from them, however I had never had to create a new remote repository on a network share from the a local repository. Below how I did it...

First, create the directory where the new repositorty will be created, then create a bare Git repository.
$ cd /n/Home
$ mkdir Demo.git
$ cd Demo.git/
$ git init --bare
Initialized empty Git repository in n:/Home/Demo.git/
Then change back to the local repository location and add a remote repository to your local repository. I'll name mine backup, you might want to name it origin if you don't have any remote repositories yet or potentially something more descriptive if you already have an origin remote repository.
$ cd /c/TFS/Demo/
$ git remote add backup /n/Home/Demo.git/
Lets check to see that the backup remote was added to the local repository.
$ git remote
backup
Now, push your branch to the backup remote repository.
$ git push backup master
Counting objects: 390, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (378/378), done.
Writing objects: 100% (390/390), 4.56 MiB | 4.87 MiB/s, done.
Total 390 (delta 255), reused 0 (delta 0)
To n:/Home/Demo.git
 * [new branch]      master -> master
The output will be a bit different every time, but the end result is the same... the files have been pushed to the remote repository.

Tuesday, September 11, 2012

Add Git Bash Option to Context Menu

I found a nice article, Adding “Open in Git Bash” to the Context Menu, which gives describes the process for adding the Git Bash command to the context menu. I am not a fan of requiring a batch file, we should be able to do it without the batch file.

This is set up for the 32-bit Git install. Here is the command that we can use to not use the batch files.
"cmd.exe /c \"cd \\\"%V\\\" && \"C:\\Program Files (x86)\\Git\\bin\\sh.exe\" --login -i\""
If you want to use a different icon, use regedit.exe and navigate to Directory\shell right-click and click "new" and then "expandable string value". Name the new string "Icon" and then set the value to be the location of the icon to use. Export that key and copy the "Icon" entry and past into the appropriate locations below, then delete the "Icon" string that was just created. For a good write-up on this, check out this tutorial http://www.sevenforums.com/tutorials/21878-context-menu-add-shortcuts-icons.html.

Here is my solution:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Directory]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\bash]
@="Open in Git &Bash"

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\bash\command]
@="cmd.exe /c \"cd \"%V\" && \"C:\\Program Files (x86)\\Git\\bin\\sh.exe\" --login -i\""
"Icon"=hex(2):43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,20,\
  00,46,00,69,00,6c,00,65,00,73,00,20,00,28,00,78,00,38,00,36,00,29,00,5c,00,\
  47,00,69,00,74,00,5c,00,65,00,74,00,63,00,5c,00,67,00,69,00,74,00,2e,00,69,\
  00,63,00,6f,00,00,00

[HKEY_CURRENT_USER\Software\Classes\Directory\shell]

[HKEY_CURRENT_USER\Software\Classes\Directory\shell\bash]
@="Open in Git &Bash"

[HKEY_CURRENT_USER\Software\Classes\Directory\shell\bash\command]
@="cmd.exe /c \"cd \"%V\" && \"C:\\Program Files (x86)\\Git\\bin\\sh.exe\" --login -i\""
"Icon"=hex(2):43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,20,\
  00,46,00,69,00,6c,00,65,00,73,00,20,00,28,00,78,00,38,00,36,00,29,00,5c,00,\
  47,00,69,00,74,00,5c,00,65,00,74,00,63,00,5c,00,67,00,69,00,74,00,2e,00,69,\
  00,63,00,6f,00,00,00
I have not figured out how to set the icon on the CMD window to the git icon instead of the basic CMD icon. It is not that important, it works without it. Perhaps one day.

Wednesday, July 25, 2012

Git-Tfs Setup And Development Workflow

I finally got around to testing the git-tfs tool. I originally looked at this several months ago and didn't have time to dive into it and WOW was I missing out.

Those That Came Before Me

There are some excellent blogs out there which have plenty of information on this tool, except they all seem to miss the a major issue that none of them address. I will cover them below and the highlights of the installation process.

The Setup

To set it up, just clone the git-tfs GitHub repository. If you use GitHub for Windows, just click the "Clone in Windows" button.

To build the solution, make sure to use the Visual Studio 2010 Command Line, or at least make sure the Visual Studio environment variables are set.
msbuild GitTfs.sln /p:Configuration=Vs2010_Debug
It appears that there is a hidden step that should immediately follow this, copying the output to the Git\bin directory. From the git-tfs directory where the solution file exists, run the following (update accordingly).
copy /Y GitTfs.Vs2010\bin\Debug\*.dll "C:\Program Files (x86)\Git\bin"
copy /Y GitTfs.Vs2010\bin\Debug\*.exe "C:\Program Files (x86)\Git\bin"
Verify that the Git\bin directory is in your path. Open a git bash shell and run:
git tfs help
If the command is found, it is ready to go. At this point a celebration may be appropriate. There is still plenty of awesomeness to come.

The Simple Development Workflow

The workflow for this is actually pretty simple. Git-tfs is only needed in 3 use cases:
  • Clone the project
  • Pulling updates from TFS
  • Pushing files to TFS
Once the project has been cloned, standard git best practices apply. All check-ins to the git repository are stored with the reposiotry.

Getting The Project Files

There are a couple ways to get files out of TFS. You can clone the entire TFS project or just part of it. Performing a quick-clone will generate a git repository with only the latest code (no history). Using clone will generate a git repository with full history. NOTE: in all examples below, omitting <Local Location> will generate the repository in the current directory.

The following is an example of the commands required:
git tfs clone http://vstfs:8080 $/Team.Project <Local Location>

git tfs quick-clone http://vstfs:8080 $/Team.Project <Local Location>
The following are examples of generating a partial repository.
git tfs clone http://vstfs:8080 $/Team.Project/ <Local Location>

git tfs quick-clone http://vstfs:8080 $/Team.Project/ <Local Location>
This can be very useful for instances where a TFS project has multiple root folders like branches, tags, and trunk. It doesn't make a lot of sense to clone the tags unless you have to have it especially if the trunk is needed.

From this point forward, all local changes can be maintained with Git Bash or GitHub for Windows until the repository needs to be updated or changes checked in.

Updating The Repository From TFS

Updating is straight forward. All merging is accomplished with Git's merging functionality and tools.
git tfs pull

Pushing to TFS

Once the changes are complete, use the following to check in the changes. Checking-in to TFS does a rebase, in that it only checks in the latest version and the other check-ins in the git repository are left as is but not persisted in TFS.
git tfs checkin -m "Add a great comment"

That