1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Git Tips and Tricks

Discussion in 'Engineering Concepts' started by pradeep, Nov 28, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    I have tried introducing people to Git VCS, both personally and through by articles at G4E, I have written 2 articles, [THREAD=27216]Introduction to Git VCS[/THREAD] & [THREAD=27305]Git VCS - Cloning & Workflow[/THREAD], in this article

    Get A List Of Files Modified Between Two Commits



    At times working on a feature or an enhancement of an already live project we might forget some files which we might have worked on, or we might need to make the feature live, and so we need to know the files to make live. With this command you can get the list of file between two commit.

    Code:
    git diff --name-only SHA1 SHA2
    Example:
    Code:
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ git diff --name-only fbf08d 001b
    config.pl
    sparse.pl
    ssl.db
    

    Ignoring Some Files



    There are some files which we might not want to use version control on, like logs, temporary files, binary files like images etc. But they always show up on git status command. For this purpose git reads a special file .gitignore where you can specify the files/directories, by names and/or wildcards the items you want to be ignored by Git. See the example below.

    Code:
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #	modified:   config.pl
    #	modified:   sparse.pl
    #	modified:   ssl.db
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #	config.pl~
    #	sparse.pl~
    no changes added to commit (use "git add" and/or "git commit -a")
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ vim .gitignore
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ cat .gitignore
    *.bak
    *.tgz
    *.gz
    *~
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #	modified:   .gitignore
    #	modified:   config.pl
    #	modified:   sparse.pl
    #	modified:   ssl.db
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    
    As you can see from the example, the untracked files ending with ~ are no more reported by Git.

    Creating Git Aliases



    Like all command-line statements we might also like to shorten frequently used Git command like commit branch etc. You can set them up easily, all you have to do is to edit the .gitconfig file and add aliases under the [alias] section, checkout the example below.

    Code:
    [alias] 
    ci = commit 
    co = checkout
    up = pull origin master
    
    Now, you can issue commands like this,

    Code:
    pradeep@deepz-desktop:~/Desktop/Programs/utils$ git ci -m 'My commit'
    
     
    1 person likes this.

Share This Page