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

Friday, 9 May 2014

git common commands

git init  --- initialise git repository on the local machine
git status --- view the current files changes (add,remove or untracked...)
git add   --- add untrack files to repository (wildcast supported)
git add   --- remove files to repository and disk as wll (wildcast supported)
git commit --- commit the change to the repository
git log -- show git commit history
git remote add origin [github or git server URL]  --- link the local repository to the
git push origin master    --- push the local commits to remote repository
git pull origin master  --- pull down the remote repository
git reset --- reset the stage
git branch [branchname] --- create a new branch
git checkout [branchname] --- switch to another branch
git merge [branchname] --- merge the branch with the current branch
git branch -d [branchname] --- delete the branch

Thursday, 24 April 2014

Git Commit Golden Rules

commit is an essential activity in Git usage. here are some important rules for commit

only commit one thing in a single commit

don’t mix things together in one single commit. Always commit just one function, one bug-fix for the issue.

write good commit message

commit message is like the comment to the program. Make sure you commit the change with a suitable commit message for further reference.

commit a completed work.

Never commit something that is half-done. If you need to save your current work temporarily in something like a clipboard, you can use Git's "Stash" feature

only commit tested work

Related to the point above, you shouldn't commit code that you think is working. Test it well - and before you commit it to the repository.


Saturday, 15 March 2014

Git tab management


Git provides a snapshot function called tag. usually tag is used to refer to some milestone version.

Make a tag

#git tab –a tagname –m “comment”
eg: #git tag –a V1.0 –m “this is the version 1.0”
make an tag for previous commit
#git tab –a tagname –m “comment” [commit id]

Show how many tags in the current branch

#show tag
v1.0
v2.0

Delete the tag

#git tag –d tagname
If you want to delete the tag in Git server (eg github), we need to push it to remote server
#git push origin :refs/tags/[tagname]

Checkout the tag

Same as checkout the branch
#git checkout tagname

Push the tage to remote Git server

Push one tag
#git push origin gitname
Push all tags

#git push origin --tags 

Friday, 14 March 2014

Git branch management


Branch management is very important especially when git project is shared by team. Project can set branches, when a programmer works on the branch, it won’t interrupt others work. When the sub-tasks finished, the programmer can merge the branch so that it can be visible to others.

Usually the project should have a default branch called master as below



    















create an branch



command:

git branch test          # create an branch called test
git checkout test        # switch to the branch called test

or:
git checkout –b test    # create a test branch and switch to it.

now git repository is like:















 modify the test branch and commit it




merge the master and test branch



git checkout master       #you need switch to the branch you want to merge
git merge test            #merge the branch now

now it is like:


















delete the branch if not used any more


commands:      git branch –d test



















a short summary of basic branch management commands:
  • check the branch status            #git branch
  • create an branch                   #git branch bname
  • switch to a branch:               #git checkout bname
  • create and switch to the branch:  #git checkout -b bname
  • merge the branch to current branch:#git merge bname
  • delete the branch:                 #git branch -d bname

branch conflicts management

when the two branches are both made changes, they can’t be merged directly.


You will have to manually fix the conflicts then merge the file

there are some practical rules for branch management

  1. project has a stable and release branch called master
  2. project should have other sub-main branches as test, dev, uat
  3. team members should create their own branches from test, dev. They should not create branch from master directly
  4. team members should merge their codes to dev or test
  5. master can only be merged from one single branch, eg dev.

Thursday, 6 March 2014

Github usage introduction

Github is a free (if visible to public) internet based software respository and there are lots of open source software on the site. In this blog, we are going to introduce how to use github to push and pull and clone the respository.

create trust relationship between your git client and github.

After you created the github account, you need to add your public key to the github key store.
You need to run the below command as any linux account.
ssh-keygen -t rsa -C “youremail@example.com
it will then create an .ssh under your home directory. Please don’t change the permission of the directory and files in it.
Then go to you github account and choose “edit your profiles” -> SSH Keys -> Add SSH key

Then choose a title and put your public key (the content of  id_rsa.pub) then save it.

create the remote repository

click the ‘+’ near your account
and give a sound name and discription and probablly choose license type of your repository.

That’s it. Very easy.

link the remote repository with local repository

although usually we will create the remote repository then clone it to the local respository. Sometimes we do it reverse.
Run the below command in your local repository


git remote add origin git@github.com:aaaaatoz/mygit.git

this will link your local repository with the repository called origin (the traditional repository name)
then you can push the content from your local repository to remote repository.

git push -u origin master

it will create the master branch and linked it with your local branch.
Then any modification in your local repository, you can easily push it by

git push origin master

clone the existing repository.

If you have already got a remote repository and want to clone it to the local repository,
There is an easy command:
You can create an empty directory in your Linux box and run:
git clone git@github.com:aaaaatoz/learngit.git
then it will download all the repository stuff to your local box and you can control it easily.
After you modify the content locally you can push it back to github repository by git push origin master

Tuesday, 4 March 2014

using local repository git



Git is one of the most powerful (probably the most one) distributed software version control tool. And github (https://github.com/), an internet based git version, has been the largest online software repository.

Here are some basic tutorials about how to use git as a local repository. Although as a distributed version control system, git is more powerful when it is used online. But we can easily get the basic idea how git is working by local repository.

understand basic items in git

To understand how git is working, we need to know the below:
The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
The working directory is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit.



Create local repository.

Local repository is very handy and easy to use. And it is a good way to practise git.
Create a directory for git working area.
#mkdir testgit; cd testgit
#git init – it will create the initial git repository under the directory called .git. never manually modify the content in the directory as it will destroy the repository You can modify the files in the testgit directory (add, modify, delete) and see the difference between your currect directory and the repository

file modification, add and commit

After you created the file, you can see the changes via command
#git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mytestfile.txt

nothing added to commit but untracked files present (use "git add" to track)

It shows there is a file but untracked. That mean the change is made on your working directory but not to staging area nor git directory yet.

Now let’s add it to staging area by add command:
[root@X001 testgit]# git add mytestfile.txt
[root@X001 testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   mytestfile.txt

Now it is recorded in staging area and ready for commit to the repository.

Let’s commit it.

[root@X001 testgit]# git commit -m "my first commit with a new file"
[master (root-commit) c53f1ce] my first commit with a new file
 Committer: root <root@X001.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 mytestfile.txt
 [root@X001 testgit]# git log
commit c53f1ce7756990e78910190a8d1f6a312f61b004
Author: root <root@X001.(none)>
Date:   Tue Mar 4 18:34:43 2014 +1100

my first commit with a new file

after you committed it (with your comment), you will see the commit log shows the commit id and the comment.

Let’s edit the file then use git diff to check the difference between the current file and the file in the staging area (not the repository).

[root@X001 testgit]# git diff -- mytestfile.txt
diff --git a/mytestfile.txt b/mytestfile.txt
index 493021b..0b2939c 100644
--- a/mytestfile.txt
+++ b/mytestfile.txt
@@ -1 +1,2 @@
 this is a test file
+add a new line here                     --             this is my new line

Now add and commit it. See git tracks the commit changes.
[root@X001 testgit]# git log
commit bc07516ed8342b947ad5e0deff4aa7ff040c4416
Author: rafa <root@X001.(none)>
Date:   Tue Mar 4 18:41:34 2014 +1100

    my second commit with a new line

commit c53f1ce7756990e78910190a8d1f6a312f61b004
Author: root <root@X001.(none)>
Date:   Tue Mar 4 18:34:43 2014 +1100

    my first commit with a new file

version reverse

if you want to reverse your change to previous version. There are three ways.
Let modify the file again and add a new line below
  • If the modification has not been added to staging area. Using the command: git checkout – <file> to recover

  • If the file has been added to the staging area. Using the commands:
"git reset HEAD <file>..." to unstage to unstage
“git checkout -- <file>”  to recover

  • If the file has already been submitted. Using the command: git reset –hard [commit id] to rollbak to previous version. You may use git log or git reflog to view the      commit id

delete the file

                If you want just delete the file in repository, you may need to
1.       Delete the original file in working area
2.       git rm [file]
3.       git commit
here is the basic instructions about how to use a local repository we will talk about using remote (internet based) repository soon.