March 2, 2014

Git - Useful Commands

Commit Changes without Staging Stage : 
$ git commit -a -m "committing without staging"

To Push to a Branch : 

Assuming a branch is created and checked out :
git push origin branch

If pushing to a non-default branch, you need to specify the source ref and the target ref :
git push origin branch1:branch2


Fetch from Original Forked Repository or any other Related Repository : 

First you need to add the desired repository as a remote :

$ git remote add upstream https://github.com/wso2/product-esb

This remote will be referred to as 'upstream' now. Then you can fetch from this upstream. You can verify the added remote repositories as below :

$ git remote -v
origin https://github.com/maheeka/product-esb.git (fetch)
origin https://github.com/maheeka/product-esb.git (push)
upstream https://github.com/wso2/product-esb (fetch)
upstream https://github.com/wso2/product-esb (push)

Now merge the changes to the local branch :
$ git merge upstream/master master

upstream/master is the upstream branch that you need to merge from and master is your local branch. If you are merging to another branch use that branch name instead of master.
$ git merge upstream/branch branch

Now push these commits to your repository :
$ git push origin master


Checkout a Branch from Upstream :

Create a branch tracking upstream branch and checkout :
$ git checkout -b branch1 upstream/branch1
Branch branch1 set up to track remote branch branch1 from upstream.
Switched to a new branch 'branch1'

Now you can commit and push as you would for any other branch.

Edit Committed (and Pushed) Commit Message : 
  1. $git commit --amend -m "new commit message"
  2. $git push origin master -f

Adding an Existing Project to git (bitbucket) :
  1. Create a project in bitbucket - you will get "clone url"
  2. Issue the following commands on git bash :

Initialize git repository : 
leo@LEODEN ~ $ git init Initialized empty Git repository in C:/Users/leo/.git/ 

Navigate to the project location
leo@LEODEN ~ (master) $ cd C:/Users/leo/git/timesheetApp/TimeSheet 

Initialize existing git repository
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git init Reinitialized existing Git repository in c:/Users/leo/git/timesheetApp/TimeSheet /.git/

Add(Stage) all files and folders
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git add * 

Check status (optional) which will list down the files to be committed
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git status 
# On branch master # 
# Initial commit # 
# Changes to be committed: 
# (use "git rm --cached <file>..." to unstage) # 
# new file: .classpath 
# new file: .project 
# new file: .settings/org.eclipse.jdt.core.prefs 
# new file: .settings/org.eclipse.jdt.ui.prefs .....

Commit the staged
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git commit -m "first project add" [master (root-commit) 71447ea] 
first project add 56 files changed, 707 insertions(+) 
create mode 100644 .classpath 
create mode 100644 .project ..... 

Check the status (optional) which will show that all are committed
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git status 
# On branch master nothing to commit, working directory clean 

Push the changes to bitbucket repository
leo@LEODEN ~/git/timesheetApp/TimeSheet (master) $ git push -u origin master 
Password for 'https://maheeka@bitbucket.org': 
Counting objects: 90, done. Delta compression using up to 4 threads. 
Compressing objects: 100% (71/71), done. 
Writing objects: 100% (90/90), 1.47 MiB | 14 KiB/s, done. 
Total 90 (delta 5), reused 0 (delta 0) To https://maheeka@bitbucket.org/maheeka/timetracker.git * [new branch] master -> master Branch master set up to track remote branch master from origin.


Reference : 

No comments:

Post a Comment