git使用

Set up GIT

  • follow the instructions at http://help.github.com/win-set-up-git/ to install msysgit (GIT for Windows): complete step 1 (you can ignore next step and anything following it) 

  • (Optional) add git bin folder to your PATH if you select git bash only, this will allow you running git in DOS window.

  • add your name email to global config

    git config --global user.name "Put your name here"
    git config --global user.email youremailaddress @xyz .com

Set up jgit 

We use jgit to access shared GIT repositories on Amazon S3 (Note that jgit.sh requires Java 1.6) 

  1. Create .jgit with the contents below and store in your home folder (e.g. c:\Users\jdoe. You can use touch .jgit if Windows doesn't allow you to create such a file. touch is in the bin folder of your GIT installation.)

    accesskey: AKIAJ5CU7TZ42QQNGQPA
    secretkey: mm+Wiw4/d5jioZuI9QG0ISvOdpRR4lAzxso+zlai

         

  2. Download jgit.sh from http://www.eclipse.org/jgit/download/ 

    • rename it to jgit and copy it to the folder of your choice (e.g. d:\tools\jgit) 
  3. Create a folder for storing your GIT projects (e.g. d:\dev\git_proj)  

 

Common Operations

Clone (check out) Project

cd d:\dev\git_proj
java -jar d:\tools\jgit clone amazon-s3: //.jgit@tclgit/tcloud.git
java -jar d:\tools\jgit clone amazon-s3: //.jgit@tclgit/tcloud-server.git

A folder tcloud is created.  

Pull Changes

cd d:\dev\git_proj\tcloud
java -jar d:\tools\jgit fetch
git merge origin/master

Note: you may see some error during fetch operation, but those can be ignored.  

 

Push Changes

Once you have git commit your changes, you can push the changes to the central repository   

cd d:\dev\git_proj\tcloud
git commit . -m "added something"
java -jar d:\tools\jgit push origin master

You should follow fetch-merge-push sequence to publishing your changes to the central repository to avoid non-fast-forward error during push. In other words, do the following,

  1. java -jar d:\tools\jgit fetch
  2. git diff master origin/master

If diff yields no difference, do git push directly, otherwise do the following

  1. git merge origin/master 
  2. fix any merge conflicts (files with UU when you do git status -s)
  3. git add .
  4. git commit -m "your merge message goes here"
  5. java -jar d:\tools\jgit push origin master

Commit Deletions

Add -A to git add to include any deletions.

git add . - A
git commit . -m "...."

Undo Changes

Soft reset to unstage changes   

git reset

Hard reset to restore everything before all changes since last commit   

git reset --hard

 

Undo changes to a file (check out the latest version from GIT see http://stackoverflow.com/questions/692246/how-do-i-revert-one-file-to-the-last-commit-in-git)   

git checkout -- file_name

Diff

Configure a visual diff tool like Beyond Compare first (see the See Also section at the bottom of this page for more details)

cd src\java\com\tcl\lct\controller
git difftool RecommendationController.java   ( this will diff the head version with what you have locally)
git difftool head~ 1 RecommendationController.java  ( this diff with previous version, will be useful when don't have any changes to local file)

Create a new repository from existing project 

Assuming you have a project at c:\dev\git_proj\myproj   

cd c:\dev\git_proj\myproj
git init
git add .
git commit -m "created a new project called myproj"
git remote add origin amazon-s3: //.jgit@tclgit/myproj.git
java -jar d:\tools\jgit push origin master

Branches

List out the local branches that you have, the branch you are currently working on will have a star next to it and if you have coloring turned on, will show the current branch in green

git branch
 
  * master

List out the remote branches information

git branch -r
 
   origin/branch1
   origin/master

Checkout the other branch, you will create a new local branch and link it to the specified remote branch

git checkout -b branch1 origin/branch1
 
   Branch branch1 set up to track remote branch branch1 from origin.
   Switched to a new branch 'branch1'

 

And now you can see what is the current working branch, then you can change some code in new branch and push it the specified remote branch

git branch
 
* branch1
   master

By the way, you can create a new branch and switch to it without link it to remote branch, too

git branch newBranch
git branch
 
* newBranch
   branch1
   master

In Eclipse, this work is easier.

  • At first you clone repositories in "Git Repositories" perspective, then import the maven project and share it with Git, you will find that you are defaultly working on "master" branch
  • Then you can right click on the maven project, select "Team-Switch to-New Branch..."
  • In the "Create Branch" window, you can select the other branch as source ref and checkout new branch
  • Now, you can right click on the maven project, select "Team-Switch to", you will find that the new branch is in the menu, so you can easily switch to it in seconds
  • If you just want to create a new branch, right click the maven project and select "Team-Switch to-New Branch...", then select the source ref and name you new branch, finish

If you have some uncommitted changes in the current branch, you will need to either commit it or dispose it before you switch to the new branch.

 

Troubleshooting 

Tips

  • add shortcut to jgit: you can make jgit executable in linux easily, and you can do the following to simplify the same in Windows,
    • create a file jgit.cmd with the following,

      java -jar d:\tools\jgit %*
    • put the file in one of your PATH folder, e.g. c:\bin
    • you can now just do jgit fetch or jgit clone amazon-s3://.jgit@.....

JGit on Linux and Mac OS

INSTALL JAVA
First, confirm whether you have Java installed already:

java -version

 

Download JGit, install in the /usr/bin directory, and make it executable by all users:

sudo wget --output-document=/usr/bin/jgit http: //download.eclipse.org/jgit/maven/org/eclipse/jgit/org.eclipse.jgit.pgm/0.10.1/org.eclipse.jgit.pgm-0.10.1.sh
sudo chmod ugo+x /usr/bin/jgit

 

Confirm JGit is successfully installed:

jgit version



CONFIGURE JGIT S3 CONNECTION
Create a JGit configuration file:

touch ~/.jgit
chmod 600 ~/.jgit
nano ~/.jgit



Into the ~/.jgit file, insert your Amazon S3 account access key and secret key in the format:

echo "accesskey: AKIAJ5CU7TZ42QQNGQPA" >~/.jgit
echo "secretkey: mm+Wiw4/d5jioZuI9QG0ISvOdpRR4lAzxso+zlai" >> ~/.jgit
 
jgit clone amazon-s3: // .jgit@tclgit /tcloud .git

 

 

Go back

  • Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

    git stash

    use git stash list to list the stashes that you currently have. 
    use git stash pop to Remove a single stashed state from the stash list and apply it on top of the current working tree state.
    see more information by git stash --help.

See also 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值