Archive

Posts Tagged ‘git’

Git – deleting the current branch is denied

August 1st, 2012 No comments

Here’s a solution if you ever get the following error when trying to delete the master branch in a remote repository:

remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To /somewhere/in/the/cloud/repo.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to '/somewhere/in/the/cloud/repo.git'

The reason you’re seeing the error is that HEAD on the remote repository by default contains something like this (see also line 9 in the error message above):

ref: refs/heads/master

If you have access to the remote repository, simply modify the HEAD file to point to a different branch and then you’ll be able to delete the remote master branch. There are some significant implications to modifying the remote HEAD, especially if other users are tracking that branch, so make sure you know what you’re doing if you choose to point it to some other arbitrary branch.

Categories: Uncategorized Tags:

Git Quick Reference Card

September 24th, 2011 No comments

Git has a lot of commands. Well over 100 of them. Most of the times you only use a handful of them, but if you’re a power-user, every once in a while you’re looking for that elusive command that you remember reading about. This Git quick reference card sums them all up for you so you can look up the details using Git’s help system (git help <command>).

Git Quick Reference Card - Page 1

Git Quick Reference Card - Page 2

The reference card is available as a PDF so you can print it out. The PDF version is derived from the TeX file which is hosted at http://github.com/gburca/git-qrc so anyone can update it.

Categories: Uncategorized Tags:

Git file transition diagram

November 5th, 2009 No comments

Here’s a Git cheat sheet on how to move content between the working directory, the staged area (the .git/index file) and the committed Git repository. It shows what happens to the file F (or more correctly, to its content) in response to various git commands and how to undo/revert an edit, or a git add.

F’ represents the first modification to F, and F” the second modification to F.

File transitions in Git

File transitions in Git

Categories: Uncategorized Tags:

Git filter-branch incantations

October 15th, 2009 No comments

Here’s an example of how to use the git filter-branch command with the –env-filter option:

git checkout mybranch
git filter-branch --env-filter "export \
    GIT_AUTHOR_NAME='Your Name' \
    GIT_AUTHOR_EMAIL='email@example.com' \
    GIT_COMMITTER_NAME='Your Name' \
    GIT_COMMITTER_EMAIL='email@example.com'" \
    HEAD~4..HEAD

Notice that on line 7, the first reference (HEAD~4 in this example) needs to be the parent of the first (oldest) commit you want to change.

Verify that your changes are good by comparing the rewritten commits with the original/refs/heads/mybranch reference that filter-branch saves. If all is good, delete the original reference:

git update-ref -d refs/original/refs/heads/mybranch

In one of my old subversion projects I did some branch merges before SVN started tracking merges with meta-data. When the project was converted to GIT, what I ended up with were some branches that seemed to lead to nowhere (they looked like they were abandoned and never merged back into the trunk). Git had no way of knowing at what point the side-branches became integrated into the trunk because in early versions of SVN there was no meta-data to track merges. I have no plans to go back to SVN, so I decided to fix this “cosmetic” issue.

Git filter-branch to the rescue. The –parent-filter allows you to rewrite the parents of a commit. All I need to do is find the trunk commit that the branches were merged into and make the side-branch tip a second parent of that commit.

git filter-branch --parent-filter \
    'test $GIT_COMMIT = 9d2104a09307b44a3640ae368916edd80e966290 && \
    echo "-p a3f23400f469a327310513e9a20a0d717e7bc04f \
          -p 512e8f229bba27bbec1df49607e7717db760edec" \
    || cat' \
    --tag-name-filter 'cat' \
    a3f23400f469a327310513e9a20a0d717e7bc04f..master

Explanation: Line 2 contains the SHA1 (9d210…) of the SVN revision that is missing a parent. This is the revision into which the dangling head (512e8…) was actually merged in SVN, but which git does not show as containing 512e8.

Line 3 has the first parent (a3f23…) which was there before the filter-branch. We need to retain this parent. Line 4 has the SHA1 (512e8…) of the missing parent. Both of these will become parents of 9d210.

The cat is there because we want to keep all commits that don’t match 9d210… unchanged.

Lastly, line 6 is there to retain the existing tags, and line 7 covers the range we want to modify (notice that this extends from the parent of 9d210 to the tip of the master branch).

Categories: Uncategorized Tags: , ,

Git on Windows

October 9th, 2009 No comments

One of the problems with using git on Windows is that it’s difficult to integrate it with other native (Windows) tools. That’s because the environment git runs in (cygwin or msysgit) refers to files by their UNIX name, but the Windows tools have no notion of where /tmp is for example (and that location can be different between cygwin and msysgit).

When doing an interactive rebase, the editor might be asked to edit a file such as /cygdrive/c/some/path/to/a/file. When doing a diff in git, one or both of the files to be diff’d might be placed by git in /tmp. Windows tools don’t understand such path names.

Fortunately, cygwin ships with a utility called cygpath that can be used to convert between UNIX and Windows paths. Msysgit doesn’t have this utility, but if cygwin’s /bin directory is in your Windows path, msysgit will pick it up and the scripts below will be usable for both cygwin and msysgit.

Here’s a GitEditor.sh script that can be used as a wrapper around your editor:

#!/bin/sh

# Author: Gabriel Burca
#
# Script arguments:
# file-to-edit

FILE=`cygpath -w "${1}"`
"C:/Program Files/Vim/vim72/gvim.exe" "$FILE"
This GitExtDiff.sh script can be used as a wrapper around your diff tool:
#!/bin/sh

# Author: Gabriel Burca
#
# Script arguments:
# path old-file old-hex old-mode new-file new-hex new-mode

# The old-file path is typically "/tmp/.diff_something" and windows programs
# don't know how to get to "/tmp"
# We use cygpath to convert to something windows programs understand.

OLD_FILE=`cygpath -w "${2}"`
NEW_FILE=`cygpath -w "${5}"`
"C:/Program Files/WinMerge/WinMergeU.exe" -e -ub "$OLD_FILE" "$NEW_FILE"
Finally, you'll need to modify ~/.gitconfig to use these scripts by adding something like this to it:
[diff]
external = "C:/Utility/GitExtDiff.sh"
[core]
editor = "C:/Utility/GitEditor.sh"
Categories: Uncategorized Tags: , , , ,