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)
-
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
-
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)
- 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,
- java -jar d:\tools\jgit fetch
- git diff master origin/master
If diff yields no difference, do git push directly, otherwise do the following
- git merge origin/master
- fix any merge conflicts (files with UU when you do git status -s)
- git add .
- git commit -m "your merge message goes here"
- 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
Troubleshooting
- git commit gets fatal: cannot do a partial commit during a merge
try git commit -i -m "your commit message" (see http://stackoverflow.com/questions/5827944/git-error-on-commit-after-merge-fatal-cannot-do-a-partial-commit-during-a-mer for more details) - jgit push failed with rejected non-fast forward error: it means you are behind the head revision, and needs to catch up by fast-forwarding to head revision, in other words, someone had pushed something after your last push.
to fix this, you will need to fetch and merge. Make sure you git merge origin/master after fetch, and use git diff master origin/master to check whether you have fetched properly.
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
-
http://gitref.org/ : for basic GIT operations