Bristle Software Programming Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to tips@bristle.com.

Table of Contents:

  1. Version Control Systems
    1. Git
      1. Why Git?
      2. Useful Git commands

Details of Tips:

  1. Version Control Systems

    1. Git

      1. Why Git?

        Original Version: 3/15/2018
        Last Updated: 3/15/18

        Why Git?  What advantage does it have over the many other version control systems I've used:  VAX CMS, sccs, rcs, VSS, PVCS, CVS?

        Here's a very readable explanation of the differences between Git and the previous generations of VCS systems.  Even if you're already using Git, it may help you understand why it's such a good choice, or give you a place to point junior team members.

        Here are some notes I took:

        1st generation (shared files):

        • Unix sccs, rcs
        • Lock the file, then edit it, then commit your changes and unlock
        • Problem: Locked files prevent others from editing
        • Problem: No way to group related changes in multiple files
        • Problem: File server has the only copy of the entire repo

        2nd generation (version control server):

        • CVS, SVN [also PVCS, VSS, VAX CMS]
        • Each user has a local copy of the latest files
        • Edit local copy, merge manually with any other user's changes, commit the merged version
        • Problem: Errors during manual merge
        • Problem: No way to group related changes in multiple files
        • Problem: Version control server has the only copy of the entire repo

        3rd generation (distributed version control):

        • Git [also Mercurial, Bazaar]
        • Each user has a local copy of the entire repo
        • Edit multiple local files, stage them locally for a single commit, commit them locally, pull any changes by others with auto-merge committing merge locally, then push the merged commit

        The article also gives very brief instructions on installing and using Git, but skips all advanced topics and most Git commands. Also, ignores the concept of a "pull request" which is not built in to Git, but is implemented by most/all Git repo sites like GitHub, BitBucket, etc.

        For more info, see the Git row of my links page:

        --Fred

      2. Useful Git commands

        Original Version: 6/7/2010
        Last Updated: 10/16/2018

        Here's a list of Git commands that I use often.

        It's a list of Unix aliases that I define in my Unix startup file.  Some are complicated combinations of options that I might not remember easily without the alias.  Others are really simple obvious Git commands, but I created the alias anyhow because it allows me to just type "git" and then hit the Tab key to be presented with a list of my favorite commands.  Like an autocompleting dropdown at the command line.  There IS a mechanism for defining new Git verbs as aliases that work after the main "git" verb, but I prefer this technique.  Anyhow, you may find these aliases useful as a summary of useful Git commands that you may not be aware of.  Any more I should add to this list?

        alias got git                         # Frequent typo
        alias gitls           git ls-files    # List files in Git
                                              # Example: gitls foo.c
                                              # Example: gitls *.c
                                              #  (May need gitls \*.c to avoid 
                                              #   shell globbing?)
        # Made into a script to use from other scripts:
        # alias gitpulldryrun   git fetch --dry-run -v
                                              # Use fetch.  There's no pull --dry-run
        alias gitfetchdryrun  git fetch --dry-run -v
                                              # Show if there's anything to pull
        alias gitdiffremote   git diff master origin/master
                                              # Show details of what would be pulled
                                              # Doesn't work.  Why not?
        alias gitsync         ./gitsync
        alias gits            git status
        alias gitunstage      git reset HEAD  # Unstage w/o changing working copy
        alias gitdiff         git diff HEAD   # Staged and unstaged diffs
        alias gitdiffmaster   git diff master.. # Diffs from master branch
        alias gitdifffilenames git diff --name-only # Names of files with diffs
        alias gitaddinteractive git add -i    # Git shell for status, diff, add, etc.
        alias gitaddpatch     git add -p      # Stage portion of a file to be committed
        alias gitwhatfileschanged  git whatchanged 
                                              # Show all files in a commit
        alias gitlogshowchanges    git log -p
        alias gitwhatlineschanged  git log -p
                                              # Show diffs of files in commit log
        alias gitlog          git log --graph
        alias gitlogsincereverse 'git log --since=\!:1 --reverse -- \!:2'
                                              # p1 = When in log to start
                                              #      Example: two.weeks.ago
                                              # p2 = File to show log for
        alias gitblameperline git blame
        alias gitshow         git show        # Example: gitshow SomeBranch:foo.c
        alias gitlogstatsummary git log --stat --summary
        alias gitlogoneline   git log --oneline # One line per commit
        alias gitloggroupbyperson  git shortlog # Commits grouped by committer
                                                # Example: gitloggroupbyperson -s -n
        alias gitlogofperson  'git log --author=\!:1'
        alias gitlogbeforeafter 'git log --before=\!:1 --after=\!:2'
        alias gitloglinerange 'git log -L \!:1,\!:2'
                                              # Example: gitloglinerange 1 5 foo.c
        alias gitlogmethodchanged 'got log -L:\!:1:\!:2'
                                              # Example: gitlogmethodchanged bar foo.c
        
        alias gitgui          git gui
        alias gitk            gitk
        alias gitx            gitx
        
        alias gitcheckout     git checkout    # Example: gitcheckout SomeBranch foo.c
                                              # Example: gitcheckout SomeHash foo.c
        alias gitcommit       git commit -v   # Show diffs in editor for Git comments
        alias gitcommitamend  git commit --amend
        alias gitgrep         git grep -i --heading --line-number
        alias gitcherrypick   git cherry-pick --signoff -x 
                                              # Get files from a specified old commit
                                              # and create a new commit on the current
                                              # branch
                                              # --signoff = Add my name to the commit 
                                              #             comment
                                              # -x        = Add "(cherry picked from 
                                              #             commit ...)" to the commit 
                                              #             comment
        alias gitcherrypicknonewcommit git cherry-pick --no-commit
                                              # Get files from a specified old commit
                                              # but do not create a new commit
        alias gitstashlist    git stash list
        alias gitstash        git stash
        alias gitstashapply   git stash apply
        alias gitstashapplystage git stash apply --index
        alias gitstashdrop    git stash drop
        alias gitstashpush    git stash
        alias gitstashpop     git stash pop
        alias gitstashdiff    git stash show -p
        alias gitstashpoptobranch git stash branch
        alias gitdifffast     git diff --no-ext-diff # (Bypass external diff tool)
        alias gitremote       git remote -v   # Show full URLS also
        alias gitbranch       'git status -b --porcelain | head -1 | cut -c 4-'
                                              # Show current branch
        alias gitb            gitbranch
        alias gitbranches     git branch -a -v   # Show all branches
        alias gitbranchswitch git checkout 
        alias gitbranchcreate git branch
        alias gitbranchcreateandswitch git checkout -b
        alias gitbranchdelete git branch -d
        alias gitbranchmerge  'git checkout master; git merge'
        alias gitbranchpush   git push origin
        # No.  Defaults to doing the pull even w/o the --track option
        # alias gitbranchpull   'git checkout --track origin/\!:1'
        alias gitbranchpull   git checkout
        alias gitbranchchanges git log master.. # See ~/fred/git/Tips/branchlog.txt
        alias gitm            git checkout master
        alias gitcf           git checkout merged_from_cf_replacement
        alias reviewcf        'delete_pyc_files; windiff . $PWD.cf_reviewed'
        alias gitmergeresolve git add
        alias githelp         git help        # Help on Git commands
        
        alias gitbisectstart  git bisect start
        alias gitbisectbad    git bisect bad
        alias gitbisectgood   git bisect good
        alias gitbisectreset  git bisect reset
        alias gitbisectrun    'git bisect start HEAD \!:1 run \!:2'
                                              # p1 = bad_hash
                                              # p2 = script to build and run 
                                              #      regression tests

        --Fred

©Copyright 2010-2021, Bristle Software, Inc.  All rights reserved.