Category: git

  • Find who added a file to git repo

    Find who added a file to git repo

    To find which user and when a file gets committed to a git repository, you can use the command

    git log --diff-filter=A -- FILE_NAME_HERE
    

    For example

    boby@sok-01:~$ git log --diff-filter=A -- global_evildoer.php
    commit 263f547cecba129b14faeb412b33ef43c678cced
    Author: php user 
    Date:   Fri Nov 18 23:27:01 2022 -0800
    
        brunch changes
    boby@sok-01:~$
    

    Back to git

  • Color git command line result

    Color git command line result

    On CentOS, when i run git commands like “git status” or “git diff”, the result is shown with out any color. On Ubuntu, git always show result in color.

    To make git result show in color, edit file

    vi ~/.gitconfig
    

    Add

    [color]
      diff = auto
      status = auto
      branch = auto
      interactive = auto
      ui = true
      pager = true
    

    Now git commands will show output with color.

  • Show git branch in terminal

    Show git branch in terminal

    When working with git, to avoid accidental commit to wrong branch, it will be better if terminal show the branch name you are currently in.

    show git branch in terminal

    To display “git branch” in terminal, edit .bashrc

    vi ~/.bashrc
    

    Add following code to end of the file.

    # Show git branch in command promt if git repo
    
    show_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(show_git_branch)\$ '
    

    You will need to restart terminal after making changes to ~/.bashrc file or run

    source ~/.bashrc
    

    See Git

  • Auto push after git commit

    Git hook allow you to perform tasks when some changes happen on a git repository. To automatically push code when you make a commit, create hook file .git/hooks/post-commit

    vi .git/hooks/post-commit
    

    Add

    #!/bin/sh
    
    git push origin master
    

    Thats all. Unlike normal bash scripts, git hooks don’t need 755 permission to run.

    Example

    [root@lin vshare]# cat .git/hooks/post-commit
    #!/bin/sh
    
    git push origin master
    [root@lin vshare]# ls -l .git/hooks/post-commit
    -rwxrwxrwx 1 root root 37 Apr 27 15:31 .git/hooks/post-commit
    [root@lin vshare]#
    

    See git