Mark Pearl

Basics

Add notes to a commit

git notes add  

opens editor to add a note to the last commit

Create branch of remote branch

git fetch origin  
git checkout -b tests origin/tests  

Change back to previous branch

git checkout -

Delete remote branch

git push origin :branchname  
git push origin --delete branchName

Allocate Blame

git blame <filename>

Get rid of annoying Crlf-lf issues

This runs a git reset which kills all that is wip so be careful…

git config --global core.eol lf
git config --global core.autocrlf false
git rm -rf --cached .
git reset --hard HEAD

Merging

Performing an Interative Merge

Avoid having to resolve multiple merge conflicts over and over again when you are doing a git rebase. Use this handy command…

git rebase -i HEAD~<numberOfCommits>

Git Workflows

Git Flow

Rake plugin Atlassians Explanation


More complicated scenarios…

Undelete a deleted file that has been committed

Use git checkout to undelete a deleted file where the delete has already been comitted. You need to include the SHA1 when the file existed as well as the name of the file that was deleted.

git checkout HEAD^ foo.bar

Git and TeamCity

Getting git to play nice with TeamCity on a pull is relatively easy. To get TeamCity to push back to GitHub has been a bit more challenging in the past. Here are some things to keep in mind.

To get the dos/command line runner to work with git, you need git to have a variable defined as $HOME$ so that git can locate the ssh folder. The ssh folder needs to be in a .ssh directory under the home location (read here for an explanation). From the command shell you can define the $HOME$ directory using system parameters. TeamCity has a define Environment Variable where you can specify the HOME value.

To get ssh to check whether it will be able to log in type with verbose details run the following command as a command build step (For more info on ssh GitHub has a great post).

ssh -vT git@github.com

Finally, it seems to be important that if you do this via dos you should include the call command (read more here)

call git push origin

Access Management

Multiple SSH Accounts for GitHub

Step 1 : Create a new SSH Key

ssh-keygen -t rsa -C “your email address”

Don’t overwrite your existing key, save file as id_rsa_COMPANY

Tell SSH about the new unique SSH key

ssh-add ~/.ssh/id_rsa_COMPANY

Step 2 : Attach the new key

Login to GitHub, add the contents of ~/.ssh/id_rsa_COMPANY.pub to GitHub SSH via GitHub Site

Step 3 : Create Config

Create a config file to store your multiple identities

touch ~/.ssh/config
vim config

In config have the following:

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host github-COMPANY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_COMPANY

Step 4 : Try it out

For cloning…

git clone git@github-COMPANY:MyCompany/MyRepo.git

Or add a remote…

git remote add origin git@github-COMPANY:Company/Repo.git

For example:

git remote add origin git@github-markpearl:MarkPearlCoZa/Blog_General.git

Quick Tip: How to Work with GitHub and Multiple Accounts

Credentials Tool for HTTP

git credential winstore

Revert a file back to its state from a previous commit

The following will reset the file “FullFilePathAndFileName.example” to the state it was in when committed in the previous commit.

git checkout HEAD^ -- FullFilePathAndFileName.example

Addtional Resources

P4 Merge as a 3 way merge tool explained Think like a git
Git and GitHub Resources by Phil Haak



blog comments powered by Disqus

Want to get my personal insights on what I learn as I learn it? Subscribe now!


/