Git

 
 
Git is a distributed revision control system designed and developed by Linus Torvalds. Every Git clone is a full fledged repository with a complete history and full revision tracking capabilities, not dependant on network access or a central repository server.

More information can be found at: http://git-scm.com







Git commands.



Information
none

Operating system used
Windows Vista Home Premium SP 2

Software prerequisites
Git 1.8.5.2 or higher

Description Command
Show Git help

There are two ways to get Git help
  • git help <verb>
  • git <verb> --help


Example 1:
git help config

Example 2:
git config --help

Configure git

Git has three configuration files
  1. <git_install_dir>\etc\gitconfig
  2. C:\Users\<Username>\.gitconfig
  3. <local_git_project>\.git\config
The values in 3rd configuration file overrules the same values in the second file, the values in the second file overrules the same values in the first file.
Edit <git_install_dir>\etc\gitconfig file:
git config --system <name> <value>

Edit C:\Users\<Username>\.gitconfig file:
git config --global <name> <value>

Edit the <local_git_project>\.git\config file:
git config <name> <value>

Example 1:
git config user.name "John Smith"
git config user.email [email protected]


Example 2:
git config --global core.editor Uedit32
git config --global merge.tool uc


Note:
In example 2, the UltraEdit (Uedit) and UltraCompare (uc) tools are used.

Show all values in the three configuration files

Git has three configuration files
  1. <git_install_dir>\etc\gitconfig
  2. C:\Users\<Username>\.gitconfig
  3. <local_git_project>\.git\config
If the values overlap, the value in the last configuration file (see list above) is used.
git config --list

Show which configuration value is used by Git

git config <name>

Example:
git config user.name
git config user.email


Use Git in an existing project

Procedure:
  • Goto main directory of your existing project:
    cd my_project

  • Type: git init
    This will create directory:
    my_project/.git

  • Type: git add *.html
    All html files will be tracked by Git

  • Type: git add readme.txt
    The readme.xt file will be tracked by Git

  • Type: git commit -m "Initial version"
    The commit command stores all html files and the readme.txt in the local Git repository and is now version controlled.
Copy an existing remote Git repository on your computer

Procedure:
  • Goto a directory where the remote Git repository should be copied to.
    Type: cd c:\my_repositories

  • Copy the remote Git repository.
    Type: git clone https://github.com/robertlie/appium.git
    This will create the appium Git repository:
    c:\my_repositories\appium

    Note:
    If you want the Git repository to have another name, type: git clone https://github.com/robertlie/appium.git anothername
    This will create directory:
    c:\my_repositories\anothername
Show the Git status of your files

Type: git status


Depending on the file status you might see the following:
  • nothing to commit, working directory clean

    Meaning:
    • Your project directory contains no changes.
    • There are no tracked and modified files
    • There are no untracked files

  • Untracked files:

    Meaning:
    • Your project directory contains new files, not committed previously in the local repository
    • The files are untracked.

  • Changes to be committed:

    Meaning:
    • You will get this status when you apply the commmand: git add <filename>
    • By appying the add command, your project directory contains new files or modified files which are now tracked and staged, but not committed.

  • Changes not staged for commit:

    Meaning:
    • Your project directory contains modified or deleted files.
      These files were committed previously.
    • The files are tracked and modified, but not staged.
Note:
A file may have two descriptions (git status):
  • Changes to be committed:
  • Changes not staged for commit:
This is caused by the following:
  • You have modified an existing file and then applied:
    git add <filename>
  • Next you have modified the same file again.
To solve this problem, apply the git add command, followed by a git commit.

Ignoring files by Git

There are some files you want Git to ignore, for example:
  • Compiled files, eg: *.class, *.o, *.a
  • Log files: *.log
Procedure:
  • Goto the .git directory directly under your project:
    cd my_project/.git

  • Create file: my_project/.git/.gitignore

  • The .gitignore file may contain the following:

    # compiled files:
    *.log # ignore all .log files
    *.class # ignore all .class files
    *.[oa] # ignore all .o and .a files
    !demo.log # do not ignore demo.log
    web/*.txt # ignore all web/*.txt files
    # do not ignore web/help/*.txt files
    /readme # ignore the root readme file,
    # not the /subdir/readme file
    compile/ # ignore all files inside
    # the compile directory
Show file differences between the files in unstaged area (working directory) and staged area.

Type: git diff
Show file differences between the files in staged area and committed.

Type: git diff --staged
or
Type: git diff --cached

Commit all files from staged area

Type: git commit -m "Your comment"
or
Type: git commit

The last command will open the default editor where you can enter your comment in the empty top line. Additional information is shown in the editor, for example:

# Please enter the commit message for your changes.
# Lines starting with '#' will be ignored, and an
# empty message aborts the commit.
# On branch master
# Changes to be committed:
#    modified: test.txt
#


This additional information will be stripped out when you exit the editor.

Type: git commit -v
The -v option shows besides the additional information also differences between staged and commited in the editor. The differences will also be stripped out when you exit the editor.

Move modified, deleted or untracked files to staged area and commit them all in one command.

Type: git commit -a -m "Your comment"


Remove a file from git

There are several methods:

Method 1:
  • Remove the file from the working directory and from staged area, with the following command:
    Type: git rm file.txt
  • Commit the change
    Type: commit -m "Your comment"
Method 2:
  • Delete the file from your working directory:
    rm file.txt
  • Remove the file from the staged area and commit the change
    Type: git commit -a -m "Your comment"
Method 3:
  • You have modified a file.
  • Now you want to delete this modified file using command:
    git rm file.txt
  • This will not work, git will issue an error that the file has local modifications
  • To delete this file from the working directory and from staged area:
    git rm -f file.txt
  • Commit the change
    Type: commit -m "Your comment"
Method 4:
  • You have added a new file in the staged area, eg:
    git add file.txt
  • Now you want to delete this file from the staged area but keep it in your working directory:
    git rm --cached file.txt
Rename a file

  • Create a file old_name.txt and commit to local repository:
    Type: git commit -a -m "Your comment"
  • To rename this file in your working directory and staged area:
    Type: git mv old_name.txt new_name.txt
  • Commit the change
    Type: commit -m "Your comment"