Tag: 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

  • Enable SSL on BitBucket Server

    Enable SSL on BitBucket Server

    BitBucket Server alloow you to host git repositories. By default bitbucket server have url in following format

    http://YOUR_IP_ADDR:7990/login
    

    To install SSL, first point a domain to the server IP.

    Install nginx

    apt install nginx
    

    Now install LetsEncrypt

    wget https://raw.githubusercontent.com/serverok/server-setup/master/install/letsencrypt.sh
    bash ./letsencrypt.sh
    

    Get SSL in standalone mode. We use standalone mode because nginx will proxy all request to bitbucket server, so SSL validation will be difficult using nginx.

    In this example, i will be using git.serverok.in, you need to replace with your actual domain.

    systemctl stop nginx
    certbot certonly --standalone -d git.serverok.in
    

    Edit file

    vi /usr/serverok/ssl-renew
    

    Find

    /usr/bin/certbot renew
    

    Add before

    systemctl stop nginx
    

    Create file

    vi /etc/nginx/sites-enabled/bitbucket.conf
    

    Add

    server {
        listen          443 ssl;
        server_name     git.serverok.in;
        ssl_certificate      	/etc/letsencrypt/live/git.serverok.in/fullchain.pem;
        ssl_certificate_key  	/etc/letsencrypt/live/git.serverok.in/privkey.pem;
        ssl_session_timeout  	5m;
        ssl_protocols  			TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers  			HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;
        client_max_body_size 1000M;
        proxy_read_timeout 600s;
    
        location / {
            proxy_pass 			http://localhost:7990;
            proxy_set_header 	X-Forwarded-Host $host;
            proxy_set_header 	X-Forwarded-Server $host;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_redirect 		off;
        }
    }
    
    server {
        listen 80;
        server_name     git.serverok.in;
        return       301 https://git.serverok.in$request_uri;
    }
    

    Restart nginx server

    systemctl restart nginx
    

    Edit file

    vi /var/atlassian/application-data/bitbucket/shared/bitbucket.properties
    

    At end of the file, add following code

    server.port=7990
    server.secure=true
    server.scheme=https
    server.proxy-port=443
    server.proxy-name=git.serverok.in
    

    Now login to Bitbucket server, Go to Bitbucket Server administration area and click Server settings, and change Base URL to

    https://git.serverok.in
    

    bitbucket server

    Restart bitbucket server

    systemctl stop atlbitbucket.service
    systemctl start atlbitbucket.service
    
  • Git Ignore file Permission (chmod)

    Git Ignore file Permission (chmod)

    Some times when you transfer file to web server, you may need a differnt file permission for files to run in web server, if your code is in git, this make the files marked as modified eventhough file contents are the same. To avoid git checking for file permission (chmod), run following command

    git config core.filemode false
    

    See git

  • Git stdin is not a tty

    When i push to get respo, i get error

    stdin: is not a tty
    

    Other than this error on push and pull, everything worked fine.

    SOLUTION 1

    Add following to top of file /home/git/.bashrc

    if [ $(expr index "$-" i) -eq 0 ]; then
        return
    fi
    

    Modified .bashrc on my server

    root@server70 [~]# cat /home/git/.bashrc
    # .bashrc
    
    if [ $(expr index "$-" i) -eq 0 ]; then
        return
    fi
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    
    # User specific aliases and functions
    root@server70 [~]#
    

    SOLUTION 2

    The problem is fixed by

    cd /home/git
    mv .bashrc .bashrc_old
    

    This is caused on cpanel servers (i have installed gitosis on a cpanel less server a day before and it worked with out any error, this server have cpanel dns only installed.) as cpanel adds

    mesg y
    

    in file /etc/bashrc

    It also adds

    export EDITOR="pico"
    export VISUAL="pico"
    

    i replace it with following as i prefer vi

    export EDITOR="vi"
    export VISUAL="vi"
    

    See Git

  • How to protect .git folder using htaccess

    GIT is popular version control software, this work like a time machine. You can easily get older versions of code. Each changes are stored as commit, so it work like a time machine for source code.

    If you put your .git folder in a publically accessable folder, others will be able to access your source code. It is better put this folder outside of DocumentRoot folder.

    If your .git folder is on public folder, then use following .htaccess code to block access to it.

    RedirectMatch 404 /\.git
    

    Method 2

    Create an .htaccess file inside .git folder with content

    deny from all
    
  • PHP Script to pull changes from GIT Repository

    Here is a PHP script, that pulls the latest code from Git Repository to your website.

    &1", $r2);
    
    echo "
    ";
    
    foreach ($r2 as $line) {
    	echo $line . "\n";
    }
    
    unset($r2);
    
    echo "\n\n";
    echo "------------------------------------------------------";
    echo "\ngit status\n";
    echo "------------------------------------------------------";
    echo "\n\n";
    
    $result = exec("git status 2>&1", $r2);
    
    echo "
    ";
    
    foreach ($r2 as $line) {
            echo $line . "\n";
    }
    

    https://gist.github.com/serverok/7efc12fcd75aac21309dbe234d9030f6

    Upload the file to your web server as git-pull.php, access it with URL

    https://yourdomain/git-pull.php?pw=YOUR_PASSWORD_HERE
    

    For this script to work, your web server should work as the user that owns the files. If your webserver runs as www-data, you need to chown the files as www-data.

    Example

    chown -R www-data:www-data /path/to/doc/root/
    

    See git

  • git log

    To see got commit log, type

    git log
    

    To see logs in one line, run

    git log --oneline
    

    To see one line grap, use

    git log --oneline --graph
    

    or

    git log --oneline --graph --decorate
    

    git