rename a branch
$ git branch -m tmp translation
The example above rename tmp branch to translation.
github contribution
- Find a repository you want to contribute.
- Fork the repository on github.
- Clone your forked repository to your local machine:
$ git clone git@github.com:gaku/sinatra.git
git pull aborted with an error message. What to do?
$ git pull
error: Your local changes to 'dot.bashrc.ubuntu' would be overwritten by merge. Aborting.
Please, commit your changes or stash them before you can merge.
git status will take you what to do. Either revert the locally modified file with git checkout -- <file> or git add and git pull will work.
Start working on a project by getting a remote git repository
~/src$ git clone ssh://zarya.gaku.net/home/gaku/git/uklog.git uklog
Create a new remote repository
It takes following steps to create a new remote repository where you can push/pull your check-ins
- create a bare repository on a machine where you want to create a remote repository.
- register the repository you created to your work repository.
- push.
- The working repository with source code is at
~/src/bookmark. - Create a bare repository as
~/git/bookmark.git.
~/git@repository$ git init --bare bookmark.git
~/src/bookmark@work$ git remote add origin ssh://repository.machine.com/home/gaku/git/bookmark.git
~/src/bookmark@work$ git push origin master
Create a new bare repository locally
- Create a bare repository as
~/git/uklog.git. - The working repository is at
~/src/uklog.
~/git$ git clone --bare ~/src/uklog uklog.git
~/src/uklog$ git remote add origin ~/git/uklog.git
It should be up-to-date.
Who changed the file and how?
To see who changed a file and how, following command sequence may help.
% git blame uklog.rss
(this will show a hash at the beginning of lines)
% git show 03506b3f
(it shows a diff 03506b3f is a hash)
(10/12/2010)
How to unstage files?
git rm --cached filename
You can also unstage files recursively by:
git rm -r --cached gen
(2010-05-18) # integration to IDEs EGit - Eclipse plugin for Git.
branch
You are always in a branch in git. The branch created with git init command is called ‘master’.
You can have multiple branches in a workspace. To show all the branches, use git branch. You can switch to a different branch by git checkout command.
To create a new branch, use git branch branchname.
git checkout -b branchname is a shortcut to create a new branch and switch to it.
git branch -d branchname will delete a specified branch.
What is HEAD?
HEAD is the special symbol to refer the current branch. A Branch is referred by a head, which is the tip of a branch.
% git branch
experimental
* master <--- There are two branches and currently master branch is selected.
% cat .git/HEAD
ref: refs/heads/master <--- HEAD is referring the head of master branch.
% git checkout experimental
Switched to branch "experimental"
% cat .git/HEAD
ref: refs/heads/experimental <--- HEAD is now referring the head of experimental branch.
To see which branch you’re using, use git branch.
branch and file modification
When you modify a file in a branch and haven’t commited, you cannot switch to a different branch.
Also, file you created will be carried to a branch you switched.
Revert
git revert is used for a different meaning than other version control systems such as svn.
Revert to the last committed state
git reset --hard HEAD
–hard changes the working tree and index.
Revert a single file to the repository version
git checkout -- lib/casserver/localization.rb
git checkout HEAD -- db/development.sqlite3
git setup for Rails project
Create .gitignore file with following content:
log/*.log
tmp/**/*
doc/api
doc/app
If you already committed some of these files, remove them using git rm.
git rm -f log/development.log
Initialize a repository
% cd target
% git init
This commands create a git repository at target/.git.
.git/config - configuration file for the repository. .git/description - a file used by gitweb utility.
The git index is used as a staging area between a working directory and a git repository.
Files in “Changed but not updated” is not in the index.
Show the list of remote git repositories
% git remote show
origin
upstream
It shows the list of the remote repository names.
To see more details, use -v
% git remote -v show
origin git@github.com:gaku/rubycas-server.git
upstream git://github.com/gunark/rubycas-server.git
Pull a specific version of a file with git
git checkout commit path
git checkout ceb17f9984bd59d2fe30aa9f8834d700b1f59de2 db/development.sqlite3
I got the commit name with git log command.
Is .gitignore not working?
Even though I have the following line in .gitignore file, changes are showing up in git status.
.gitignore doesn’t have effect to already tracked files. Remove it from repository (git rm –cached …) or use git update-index –assume-unchanged. (2009-06-25)
