Search This Blog

Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, May 15, 2013

Add an existing project to a newly created git repository

I recently created a project on my local machine using maven archetype.  See my previous post on how to generate a new project from installed maven catalog.  I then got github.com created a repository for this project, for example: git@github.com:zhentao/test1.git

How can I associate my existing project with the newly created git repo?  There are 2 ways to do it:

First option:

1. clone the git repo:
git clone git@github.com:zhentao/test1.git

It will clone the repo to test1 folder.

2. copy your existing project to test1 folder

cp -r /path/to/project /path/to/test1

3.  commit and push all files

git add .
git commit -m "first commit"
git push -u origin master

Second option (more git style)

1. go to your project folder

cd /path/to/project

2. run the following commands:

git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:zhentao/test1.git

  
The above commands associate your project to git remote repo.

3. run the following commands to to merge the conflict:

git fetch
git merge origin/master
 --run it if conflicts exist
git mergetool
git commit

git push -u origin master

Thursday, December 23, 2010

Git cheat sheet

I have been using Git for more than a year. Comparing with SVN, the big difference is how to commit the changes to remote repository. Git requires two steps to do it:
  1. commit the changes to local repository (need to add new files explicitly)
  2. push the commit to remote repository
When I first used Git, I often forgot to add new files (git add new_file_name) to let Git track new files, and after commit the changes, I didn't push my changes to remote server.

Here are some commands I would like to share:

Create a new local branch from an existing branch and switch to the new branch:
git checkout -b new_branch existing_branch
Push a newly created branch
git push -u origin new_branch_name
-u will make your branch tracked

Push a tag
         git push origin tag_name

Push all tags not in remote server
        git push origin --tags

Delete a local branch
git branch -D branch_name
Delete a remote branch
git push origin :remote_branch_name
Delete a local tag
git tag -d tag_name
Delete a remote tag
git push origin :refs/tags/tag_name
Remove the references to the deleted remote branches
git remote prune origin
Delete last commit (not pushed yet)
git reset --hard HEAD~1
List all key/value pairs for git config
git config -l
Get a value for a key in git config (for example, get git repository url):
git config --get remote.origin.url
Show all file names changed in a single commit:
git show --pretty="format:" --name-only commit_hash
Cherry Pick
git cherry-pick commit-id-from-other-branch