Using git by yourself

Post date: Oct 24, 2010 2:15:39 AM

Some basics I've learned after using git for a while. Once I gain some experience using git collaboratively, I'll write a tutorial on that :-)

Start using git on a directory tree

Change to the directory you want to control, and run "git init"

  • cd project

  • git init

For each file and subdirectory you want to include, run "git add"

  • git add filename1 filename2 directory ...

Commit the changes using "git commit -m"

  • git commit -m "Started git repository for project"

Find out which files have changed since you last committed

List the files that have changed since your last commit, and files not tracked by your repository:

  • git status

For each "modified" or "untracked" file:

  • git add filename

For each "deleted" file:

  • git rm filename

git rm will delete the actual file if it's still around!

If you don't want to add a file to your repository or delete it, permanently ignore it using .gitignore.

  • vi .gitignore

      • *.o

      • *.log

      • *.a

      • *.exe

      • *.dll

Clone GitHub repository

There are several ways to clone a git repository. Whatever method you use for "git clone", your remote "origin" will be assigned that method.

If you are using something besides GitHub, replace github.com with the location.

mintty will not display prompts from git properly.

If you are running git clone with SSH for the first time, or using HTTPS, go to "Start" > "Programs" > "Git" > "Git bash".

To clone using the read/write SSH method:

  • Be sure GitHub has your SSH public key associated at https://github.com/account, SSH keys

  • git clone git@github.com:user/project.git

  • Answer "yes" to add github.com to your known hosts. You can check the RSA fingerprint.

    • If it appears stuck, make sure you aren't using mintty!

To use the read-only HTTPS method (behind a corporate firewall, for example):

  • git clone https://user@github.com/user/project.git

  • Enter "password", literally. I'm not sure why it asks for this.

    • If it appears stuck, make sure you aren't using mintty!

There's also a method to clone read-only via git: port, which is blocked by firewalls.

To rename the remote repository "origin" to "github":

  • git remote rename origin github

To list the current remote repository methods:

  • git remote -v

To remove a remote repository method "github":

  • git remote rm github

Working with remote repository

If you want to get rid of any unused changes on the local copy:

  • git reset --hard HEAD^

To update the local copy of "master" branch from the remote repository "github":

  • git pull github master

If you try to pull a commit that is not descended from your current github commit, you will need to go through a merge process. Run "git status" to list merge problems. Those files will have a copy of your version, and the remote version. Remove whichever version is older, save the file, and run "git commit". Now, run "git pull github master" again.

Now you can continue editing, testing, and making commits locally.

After you are done, put your commits back on the remote repository:

  • git push github master

Tracking differences with remote repository

Normally when you clone a repository and make changes, running git status will show the number of commits you've made ahead of the remote branch, in a message like "Your branch is ahead of 'origin/master' by 1 commit.". If you have a copy of the repository that wasn't made using git clone, you won't see this message by default. Use these commands to set the default remote branch for origin:

  • git config branch.master.remote origin

  • git config branch.master.merge refs/heads/master

Include an empty directory in repository

You can't track an empty directory. You can create a .gitignore file in the empty directory, then it won't be empty anymore. Example:

  • touch build/.gitignore

  • git add build

Search repository for matching text

  • git grep pattern

Remove file from history

  • git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

Undo last commit:

To remove the last commit you made locally:

  • git reset --hard HEAD^

To remove the last commit you pushed to remote repository "origin" master branch:

  • git push -f origin HEAD^:master

Create a release branch

Maybe you think you've squished all the bugs, and want to have an official version. Branch it off, and continue your future hacking in "master".

Create new branch, based on today's date

  • date +%Y%m%d

  • git branch newbranch

List existing local branches. "Current" branch will have an asterisk:

  • git branch

List all branches, including remote ones

  • git branch -a

Push the branch to remote repository

  • git push github newbranch

Now, any changes you make to the main branch won't affect that one.

Change the "Current" branch, the one that git commands operate on:

  • git checkout branchname

Download a remote branch into a new directory

How to clone a particular branch:

  • git clone -b branchname git://giturl.git

Get the latest commit from a particular branch, and put it into a directory name of your choosing:

  • git clone -b branchname --depth 1 git://giturl.git directoryname

More info

Basic Git tutorial:

http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html

StackOverflow question: Definitive Git guide:

http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide

Intermediate user tips:

http://andyjeffries.co.uk/articles/25-tips-for-intermediate-git-users

Tough Git questions:

https://git.wiki.kernel.org/index.php/GitFaq