1. Learn Git
    1. Learn Git with Bitbucket Cloud
      1. Create a Git repository
      2. Copy your Git repository and add files
      3. Pull changes from your Git repository on Bitbucket Cloud
      4. Use a Git branch to merge a file
    2. Learn about code review in Bitbucket Cloud
      1. Fork a teammate's repository
      2. Copy your fork and make a change to the repository
      3. Create a pull request
  2. Getting Started
    1. What is version control
      1. Benefits of version control
    2. What is Git
      1. Performance
      2. Security
      3. Flexibility
      4. Version control with Git
    3. Why Git for your organization
      1. Git for developers
      2. Git for marketing
      3. Git for product management
      4. Git for designers
      5. Git for customer support
      6. Git for human resources
      7. Git for anyone managing a budget
    4. Install Git
      1. Install Git on Mac OS X
      2. Install Git on Windows
      3. Install Git on Linux
    5. Setting up a repository
      1. git init
      2. git clone
      3. git config
    6. Saving changes
      1. git add
      2. git commit
    7. Git Stash
      1. .gitignore
        1. Inspecting a repository
          1. git status
          2. git log
        2. Viewing old commits
          1. Undoing Changes
            1. git checkout
            2. git revert
            3. git reset
            4. git clean
          2. Rewriting history
            1. git commit --amend
            2. git rebase
            3. git rebase -i
            4. git reflog
        3. Collaborating
          1. Syncing
            1. git remote
            2. git fetch
            3. git pull
            4. git push
          2. Making a Pull Request
            1. How it works
            2. Example
            3. Where to go from here
          3. Using Branches
            1. git branch
            2. git checkout
            3. git merge
          4. Comparing Workflows
            1. Centralized Workflow
            2. Feature Branch Workflow
            3. Gitflow Workflow
            4. Forking Workflow
        4. Migrating to Git
          1. SVN to Git - prepping for the migration
            1. For administrators
            2. Basic Git commands
            3. Git Migration Tools
            4. For developers
          2. Migrate to Git from SVN
            1. Prepare
              1. Convert
                1. Synchronize
                  1. Share
                    1. Migrate
                      1. Perforce to Git - why to make the move
                        1. Migrating from Perforce to Git
                        2. Advanced Tips
                          1. Advanced Git Tutorials
                            1. Merging vs. Rebasing
                              1. Conceptual Overview
                              2. The Golden Rule of Rebasing
                              3. Workflow Walkthrough
                              4. Summary
                            2. Reset, Checkout, and Revert
                              1. Commit-level Operation
                              2. File-level Operations
                              3. Summary
                            3. Advanced Git log
                              1. Formatting Log Output
                              2. Filtering the Commit History
                              3. Summary
                            4. Git Hooks
                              1. Conceptual Overview
                              2. Local Hooks
                              3. Server-side Hooks
                              4. Summary
                            5. Refs and the Reflog
                              1. Hashes
                              2. Refs
                              3. Packed Refs
                              4. Special Refs
                              5. Refspecs
                              6. Relative Refs
                              7. The Reflog
                              8. Summary
                            6. Git LFS

                            Extending git

                            Stefan SaasenStefan Saasen
                            Back to list

                            While Mercurial has a well defined (albeit internal) API that can be used to write extensions that extend the functionality of Mercurial, git's extension model follows the Unix philosophy of composing small, simple programs to achieve a similar effect. What that means is that git "extensions" can be written in any language and by following a few simple rules it's still possible to add commands that appear as if they were built-in.

                            Example: git activity

                            To see the activity on all the branches in a repository I've implemented a git activity command. git activity shows the latest commit on every branch, sorted by recency.

                            It shows the following output when executed in the Rails repository:


                            The script is written in bash and fairly straightforward. We set up colours and parse some command line options the scripts supports (e.g. to turn off colours, to limit the output) and then run git for-each-ref to output information about each ref.

                            set -e
                            OUTPUT_FILTER="cat" # no-op
                            commit_id_format=$(tput setaf 1)
                            date_format=$(tput bold; tput setaf 4)
                            author_format=$(tput setaf 2)
                            ref_name_format=$(tput setaf 3)
                            bold=$(tput bold)
                            reset=$(tput sgr0)
                            function usage() {
                                echo ""
                                echo "git activity"
                                echo ""
                                echo "  See 'man git-activity' for further information"
                            # actually parse the options and do stuff
                            while [[ $1 = -?* ]]; do
                                case $1 in
                                        exit 0
                                        echo "Fetch updates"
                                        git fetch -q
                                        #OUTPUT_FILTER="tail -n ${limit}"
                                    *) ;;
                            # Use newline as a field separator
                            IFS=$(echo -en "\n\b")
                            # Use tac if available, otherwise tail with the possibly-not-always-available
                            # -r flag (for reverse output)
                            TAC=$(which tac || echo 'tail -r')
                            for line in $(git for-each-ref ${GIT_OPTS} refs/remotes --format="%(authordate:relative)|%(objectname:short)|%(authorname)|%(refname:short)|%(subject)" --sort="-authordate"); do
                                fields=(`echo $line | tr "|" "\n"`)
                                printf "${date_format}%15s${reset} ${commit_id_format}%s${reset} - ${author_format}[%s]${reset} (${ref_name_format}%s${reset}): %s\n" ${fields[*]}
                            done | eval $TAC # reverse sort the output to show the newest entry last

                            The important rules to follow to make this script available as a git sub-command are:

                            • It should be named git-COMMANDNAME, in this case it's called git-activity
                            • and it needs to be executable and available on the $PATH In my example the custom git-activity script is in the /usr/local/bin directory but it can be in any directory that is on the $PATH:
                            [5002] λ > type git-activity
                            git-activity is /usr/local/bin/git-activity
                            [5002] λ > git activity

                            Providing a manual/help page

                            If the custom command has an accompanying man page, the git help command will show the help information as well. E.g. the man page for the activity command is located in /usr/local/share/man/man1/git-activity.1 and can be show by either man git-activity or git help activity.

                            The manpath command can be used to show the locations the system uses to locate man pages:

                            [5003] λ > manpath /Users/ssaasen/.opam/system/man:/usr/local/share/man:/usr/share/man:\ /Users/ssaasen/.cabal/share/man:/opt/scala/man

                            The output for git help activity is (using https://bitbucket.org/ssaasen/git-pastiche/src/master/man/git-activity.1):


                            Bonus tip Man pages can easily be generated from Markdown using Pandoc:

                            [5010] λ > pandoc -s -w man doc/git-activity.1.md -o ./distribution/man/git-activity.1
                            # Display the man page for testing
                            [5011] λ > nroff -man ./distribution/man/git-activity.1
                            # Show the actual man page being used after copying it to a known manpath location:
                            [5012] λ > man -wa git-activity


                            By following a couple of simple rules and by embracing the Unix model of composing functionality out of small, focused programs and scripts, it turns out that it is surprisingly easy to extend git's functionality and making custom commands part of the git command suite.

                            More extensions

                            The source for git-activity and a few other commands can be found here: https://bitbucket.org/ssaasen/git-pastiche

                            Other additional git commands can be found here: