2011年(4)
分类: 嵌入式
2011-03-09 08:38:36
Git Study
As we will use this version control system---git , this page introduce some common commands in common operations , and we can know more about git.
Environment:
This page based on Ubuntu10.04. The tools we should install as below:
# sudo apt-get install git-core
Operations:
our git server address is git@10.120.120.162 (“Server” instead),and project's name is “Project”. we need sure we have the permission for it ,and that's mean whether we send our id_rsa.pub key to administrator . Generate the key using command :
$ ssh-keygen
and we will find that file in ~/.ssh
Now we assume that we have a server as below :
Example :
current working directory: Study
project name : Test
branch master:
file name : Hello
file content : welcome
branch second :
file name: Hi
file content : OK
file name: Hello
file name :welcome
Get source code from git server:
git clone Server → project named Project will be loaded from Server in current directory, and create a branch named master by default, it is the same as branch of master on Server. Now server address can be instead by “origin” by default.
If there are many branch on Server, we may want work on other branch B,one method as below:
* git branch -a : it will show all branch on the Server
* git checkout -t remotes/origin/B : this will create the branch B ,and we now on branch B the same as Server.
Example:
git clone git@10.120.120.162:testPro/Test→ project directory is Study/Test
git branch -a → * master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/second
git checkout -t remotes/origin/second→ now we are working on branch second
Remote repository
git remote : show all remote repositories, and they are just the name we give or system give ,and they stand the remote repositories. A remote repository is just a git repository server address.
git remote add name server-address : it will create a name stand the server ,and we can use it instead of server-address in future. System give a name with “origin” by default ,and it will use “origin” instead of server-address on this page
git remote rm remote-name : delete the remote name
Example :
git remote → origin
git remote add origin1 any-path → origin1 will be created ,and it stand server-address : any-path
git remote rm origin1 → delete remote name :origin1
Operations on branch
git branch : show all branch we create ,and branch with “*” is the current working branch
git branch A : create the new branch A
git checkout A : switch current working branch to branch A
git branch -d A : delete branch A if A is not current working branch
git merge branch1 : merge branch1 to current working branch, and if exist conflicts , we should modify them by ourselves .
Example :
git branch → master
* second
git branch third → branch third will be created,and you see it with command up
git checkout third → branch third now is the current working branch
git branch -d second → delete branch second
git merge master → git branch fourth
git checkout fourth
echo “good” >>Fine
git add Fine
git commit -m “fifth”
git checkout master
git merge fourth
Commit to git repository
git add files : if we add or modify some files ,use this commands to add them to staging area to wait.
git commit : commit the files in staging area to git repository,and we will write the commit message then.
-a : if we just modify them, we can skip the “add”,commit them with -a directory.
-m “message”: if we do not want to write message then, we use this.
Example:
just like “git merge master” up
Push local host git to Server
git pull origin branch2: get latest date on Server as data already updated by others on branch2, and merge branch2 to current working branch on local git .
git push origin : push our repository to Server with all branch.,it will merge all branch with the same name.
git push origin branch1: only push branch1 to branch 1 on the server, if branch1 is not exist on server ,it will create it named with branch1
git push origin branch1:branch2 : only push our branch1 to branch2 on the Server, if branch2 is not exist on server , it will create it named with branch2.
git push origin :branch2 : this will delete the branch2 on Server ,so please notice it.
Example:
git pull origin second → for there is no one change data,nothing change
git push origin → for the situation up, it will just push branch master to
the server .
git push origin fourth → it will create branch fourth on the server
git push origin :fourth → delete branch fourth on the server
Operations on tag
git tag : show all tag already exist
git tag tag-name :create a common tag
git tag -d tag-name: delete a tag
git push origin :refs/tags/v1.1 : delete a remote tag
git tag -a tag-name -m “message” : create a new tag named tag-name ,and add some message.
git show tag-name : show version information on this tag.
git push origin tag-name : push the tag to server . We need push it as tag is not pushed with branch .
Example :
git tag → nothing show as there is no tag
git tag v1.0 → create a tag named v1.0
git tag -a v1.1 -m “just test” → create a tag named v1.1 with message “just test”
git show v1.1 → it will show “just test” and commit information this tag point
git push origin v1.1 → push the tag to server
Recover data
If we add file but still not commit and want cancel it,operations like this:
git status : show status in staging area (contain file name )
git reset HEAD file : remove file from staging area.
git checkout file : load file before modify from git .
If we want to go back to status before a commit ,operations like this:
git reset --soft HEAD^ : go back to status before this commit,and we can modify
now as we like. We can do this any times ,and go to the version we like
git commit -a -c ORIG_HEAD : recover that commit just cancel
Example:
git status → if nothing will be committed ,it show nothing to commit
git reset HEAD file → git add file-changed
git reset HEAD file-changed
git checkout file
git reset --soft HEAD^ → git add file-changed
git commit -am “new”
git show
git reset --soft HEAD^
git show
the commit version “new” will be cancel ,you can modify again,then commit . If you want recover commit just cancel:
git commit -a -c ORIG_HEAD
Commit history
git log : show commit history.
git log -- filename : show a file's version history
Git repository status
git status : show commit version (not contain commit before loading from server)
git log : show all commit version
git diff : show different between content in git repository with current content (not committed)
Example :
git diff → echo “bye bye”>>Hi
git diff : it will show the difference between content in git and content not committed
Client:
every version control system have its client, include git .this introduce one of these named qgit .
Install qgit
#sudo apt-get install qgit
Use qgit
open qgit : Applications → Programming → qgit
open git repository : File → open → path/to/git
get more buttons : Action → Setup actions → New → OK →
If our commands need parameters , we need select “Ask for command line arguments” ,and it will pop a window for your parameters input.
another :teamgit
Install teamgit
# echo "deb http://ppa.launchpad.net/bain-devslashzero/ubuntu intrepid main" >>/etc/apt/sources.list.d/teamgit.list
#apt-get update
# apt-get install teamgit