git stuff - naughtont3/techbits GitHub Wiki

Git Related Stuff

  • Syncing a fork with upstream (using Open MPI as example)

    • NOTE: Assumes remotes are:

      • "remotes/origin" is my fork
      • "remotes/upstream" is original
    • NOTE: For safety I always set the upstream push as bogus. git remote set-url upstream bogus --push (Thanks JeffS!)

        $ cd ompi/
        $ git remote
        origin
        upstream
        $ git remote -v
        origin	https://github.com/naughtont3/ompi.git (fetch)
        origin	https://github.com/naughtont3/ompi.git (push)
        upstream	https://github.com/open-mpi/ompi.git (fetch)
        upstream	bogus (push)
        $ git fetch upstream
        $ git checkout master
        $ git merge upstream/master
        $ git push origin master
    
        ###
        # NOTE: To rename the branch
        #   git push <REMOTE-NAME>  <LOCAL-BRANCH>:<REMOTE-BRANCH>
        #   git push origin        foobar-localhacks:foobar
        ###
    
  • Cache credentials (avoid username/password prompt) stackoverflow

     git config credential.helper store
     git pull
     # Enter username/password (cached next time)
    
  • Update a forked repo (steps from https://gist.github.com/CristinaSolana/1885435)

      1. Clone your fork:

        git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git
        
      1. Add remote from original repository in your forked repository:

         cd into/cloned/fork-repo
         git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
         git fetch upstream
        
      1. Updating your fork from original repo to keep up with their changes:

         git pull upstream master
        
  • Show differences between two branches

        # Color based summary (master vs. tjn-fy15-orbit branch)
       git diff --stat --color master..tjn-fy15-orbit
    
        # Concise 'svn status' style summary (master vs. tjn-fy15-orbit branch)
       git diff --name-status  master..tjn-fy15-orbit
    
  • Restore previously deleted file (e.g., SetupVagrant.md)

      1. Find the last revision that touched it

        git rev-list --max-count=1 --all -- SetupVagrant.md
        8dd9a1c5e962ad5cc5d952aacfcef35a69e3574b
        
      1. To see contents of the file in previous-1 revision (just before last touch)

         git show 8dd9a1c5e962ad5cc5d952aacfcef35a69e3574b~1:SetupVagrant.md
        
      1. Checkout the last version where it occurred (then add/commit/push)

         git checkout 8dd9a1c5e962ad5cc5d952aacfcef35a69e3574b~1 SetupVagrant.md
        
  • Selectively add changes (patch based add)

     git add -p file.c
    
  • Show "remote" repos

     git remote -v
    
  • Create a secondary ("alternate") remote repo

     git remote add tjnalt <URL_FOR_REMOTE>
    

    Example:

     $ git add tjnalt https://github.com/naughtont3/ompi.git
     $ git remote -v
     origin  https://github.com/open-mpi/ompi.git (fetch)
     origin  https://github.com/open-mpi/ompi.git (push)
     tjnalt  https://github.com/naughtont3/ompi.git (fetch)
     tjnalt  https://github.com/naughtont3/ompi.git (push)
    
    
  • Push changes to an "alternate" remote repo

     git push -u tjnalt my-branch-name
    
  • Clone central repo and then track bracn in an "alt" repo

     git remote add tjnalt <REPO-PATH>
     git remote -v
     git branch -a
     git fetch tjnalt
     git branch -a
       # See new branches of other remote
     git checkout -t -b tjn-new-feature-1 tjnalt/tjn-new-feature-1
       # now our 'tjn-new-feature-1' branch is local and tracking upstream ('tjnalt') repo
    
  • Integration of branch changes to master branch (steps)

     git checkout master
     git pull --rebase
     git checkout -b tjn-master-feature1-int
     git branch -a
       # see that we are now in integration branch 'tjn-master-featureFoo1-int'
    
       # merged our featureFoo1 branch into integration branch 
     git merge tjn-featureFoo1
    
  • Tagging: Show tags

     git tag -l 
    
  • Tagging: Create a tag

     git tag -a v1.4 -m "my version 1.4"
    
  • Pull-Request: Fetch a pending pull-request (PR) for local testing

     git fetch origin pull/<PR#>/head:pr-<PR#>-mybranch
     git checkout pr-<PR#>-mybranch
    
  • Setup Name and Email for Git usage (see also: $HOME/.gitconfig)

     git config --global user.name "John Public"
     git config --global user.email "[email protected]"
    
  • Commit Signoff (requires Name/Email setup)

     git commit --signoff
    
  • Commit Signoff for previously commited change (requires Name/Email setup)

     git commit --amend --signoff
    
    • NOTE Results in a new commit hash (SHA#) so exercise care with public contributions. If already pushed to remote repository, then will require a --force push. Generally should avoid force pushes on ANY publicly shared changes.
  • Cherry picking

     git cherry-pick -b newbranch <SHA#>
    
    • NOTE Cherry-picking changes the commit hash (SHA#), resulting in a new/different commit hash (SHA#). Therefore these are only manually identifable by commit message or close inspection of changeset. (Changes are different git commits!)
  • Rebasing a pull-request

     git checkout pr/my-bugfix-branch
     git rebase -i origin/master
       # Now our 'bugfix' branch has been updated (interactively) to
       # be updated with latest master
    
  • Rebasing during pull (Fast-Forward a branch)

      # Pull but fast-forward our branch's changes to tip of new changes
     git pull --rebase
    
  • Make default origin read-only (to avoid accidentally pushing to wrong repo)

     git remote set-url origin bogus --push
     git remote -v
     origin https://github.com/open-mpi/ompi.git (fetch)
     origin bogus (push)
     tjnalt https://github.com/open-mpi/ompi.git (fetch)
     tjnalt https://github.com/open-mpi/ompi.git (push)
    
  • Delete local branches (that are no longer at remote origin repo)

     git remote prune origin
    

    Related from JSquyres:

     git fetch --all --prune
      # Jeff made this into an alias 'fap' (Fetch-all-Prune) in ~/.gitconfig
    
  • Delete remote branches from the command-line (not have to use Web to remove branch)

      git push origin --delete mydev-foobar7
    

    Example...

        # -BEFORE-
        # git br -a
        # * master
        #   remotes/origin/mydev-foobar7
        #   remotes/origin/master
      git push origin --delete mydev-foobar7
        # -AFTER-
        # git br -a
        # * master
        #   remotes/origin/master
    
  • Reword/rework commits (squashing!)

      # Reword/squash last 3 commits
    git rebase -i HEAD~3
    
  • Set master tracking

     git checkout master
     git br --set-upstream-to origin
    
  • Show history for specific line(s) stackoverflow ref

    • Requires Git >=1.8.4
         # Show lines 110 to 110+2 of given file
       git blame -L 110,+2:src/mca/rmaps/base/base.h
    
⚠️ **GitHub.com Fallback** ⚠️