How to Recover a Mistakenly Deleted Branch

Workflow

git checkout -b _new_branch_name
# do some work and commit changed
git checkout master
git branch -d _new_branch_name
# doh... i meant to merge first

Fortunately, you can easily recover from this mistake.

git reflog
395b1ea HEAD@{0}: checkout: moving from _master_cleanup_akismet to _master_cleanup
bd7df04 HEAD@{1}: commit: spam handling using akismet for form submissions on contact_submission and applicant models
395b1ea HEAD@{2}: checkout: moving from _master_cleanup to _master_cleanup_akismet
395b1ea HEAD@{3}: commit: cleaning up and adding some basic features
a828ef3 HEAD@{4}: checkout: moving from master to _master_cleanup

Should show you a list of commits across all branches in desc chronological order.

To merge the branch you just deleted you can give merge the sha.

git merge bd7df04

Which would match the commit at HEAD@{1}

Git Feature Branch Naming Strategy

There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton

Typically, we have three main branches at any given time in a project lifecycle.

master, development and staging.

Master is production ready code, development is actively being worked on and staging is pre-flight testing before deploying master.

Features are branched off of development and use underscores to indicate distance from originating branch. For instance

_development_users
__development_users_reset_password

indicates that __development_users_reset_password branched off of _development_users branch.

___development_users_reset_password_experiment

(3 underscores away) would indicate that I was experimenting with a branch off of development users reset password branch.

This provides a nice visual hierarchy when running git branch from the command line. It’s also helpful when you have multiple features being worked on by several people and you want to know where these branches are in relation to each other without needing to ask anyone. Here is a more complex example that illustrates the usefulness in using underscores in naming feature branches

  __development_people_importing_csv
* __development_people_importing_xml
  __development_users_password_reset
  _development_people_importing
  _development_users
  development
  master
  staging

Git Untrack Already Tracked Files

To remove files that are currently being tracked by git, you have to remove them from the “cache”. Note, doing this will NOT delete the file on your local machine. It will still be there but not be tracked.

 git rm -r --cached supersecretpasswords.txt 

You then need to add the file to the .gitignore file in the root of the project so that it isn’t tracked again on your next commit.


vim .gitignore
supersecretpasswords.txt

.gitignore files are tracked so remember to check in these changes.

git commit -am'my super secret passwords are safe!'

If you want to completely delete the file, on your local machine and from git

git rm supersecretpasswords.txt

If you’re working with a directory remember to add the -r flag for recursive removal!

Git: How to Delete a Branch with an Invalid Name

If you’ve named a branch beginning with two dashes “–”, you’re sort of in trouble because git interprets your branch name as a switch/flag. You can skip switches all together
by supplying two dashes before your branch name

git branch -d -- --index_for_suppliers

and your branch will be deleted!

How to Remove Your Last Git Commit

Remove your last commit (if you haven’t pushed yet)

git reset --hard HEAD~1

To see changes that have been committed and their position in HEAD

git reflog 

And to undo your previous reset and advance the cursor to the reference immediately behind the current state

git reset --hard HEAD@{1}

If you have already pushed you can

git revert HEAD

which will reverse your last commit by creating a new commit

Have Git Email Committers After Pushes

You need a Mail Transfer Agent MTA on the server. The easiest way is to install Sendmail, which Git uses by default.

apt-get install sendmail

Remember that /etc/hosts file needs the ip address to map to the domain name your sending mail from

# vim /etc/hosts
127.0.0.1     localhost localhost.localdomain
207.136.202.87    wwwexample.com
 

Sendmail has a tendency to hang when sending mail otherwise. To test sendmail

sendmail email@example.com
this is a test
how are you today world?
.

The period on a line by itself denotes end of message and will terminate the prompt and deliver the message.

Now you need to configure Git to send email after it receives a “push” from a committer. You can add email addresses, or you can set up a mailing list to email all members. Either way, you accomplish this with the following command, just remember to cd into the git repository.

git config --add hooks.mailinglist "mailinglist@example.com"

Next you need to activate the post-receive hook, located in the hooks directory of your repository.

cp post-receive.sample post-receive

And uncomment the last line, which uses sendmail to deliver the commit message

# uncomment the last line but keep the period "."
. /usr/share/doc/git-core/contrib/hooks/post-receive-email

All done. Now just make some changes to your source code, add and commit them and you should receive an email with all the details!

Setting Up Users, Permissions and Groups for SSH Access to a Shared Git Repository

If you are having permission problems using git, such as

error: insufficient permission for adding an object to repository database ./objects

There are a couple thing you can do to remedy the situation, before moving to a full on git server like gitosis.

Create your users and add them to a group. Create (if you haven’t already) your git repo on the server and change permission and ownship and set the git config sharedRepository to true.

Here are all the commands, quick and dirty!

adduser sean
adduser jackson
groupadd developers
adduser sean developers
adduser jackson developers

mkdir -p /git/dev/app.git
cd /git/dev/app.git
git --bare init
vim description  #edit this file (mac os x complains otherwise)
chmod -R g+ws *
chgrp -R developers *
git repo-config core.sharedRepository true

Found from: http://mapopa.blogspot.com/2009/10/git-insufficient-permission-for-adding.html

Working with Branches in Git

Show all the branches

git branch

Create a new branch

git branch my_experimental_feature

Use that branch

git checkout my_experimental_feature

Pushing the new branch to a remote server

git push origin my_experimental_feature

Pulling that branch down on another machine

git pull origin my_experimental_feature

Listing all branches on other machine

git branch -a

Updating other machine

git pull origin my_experimental_feature
6 Sep 2009, 3:25pm
Git:
by

leave a comment

Non Standard Port Number with SSH and Git

Here is an example using the port 4567 to connect with over ssh and git

ssh remote add origin ssh://sean@seanbehan.com:4567/path/to/git
git push origin master