II. Precautions

  1. These git related notes are intended for the author of this website to have a quick reference for his own use
  2. Use these git related notes with caution (there are pitfalls, and there are likely better commands)

III. Background: Windows Set-up

  1. Run git via powershell instead of Git-Bash
    1. Use Posh-Git package
    2. Use Powershell without the ISE (which otherwise tends to hang)
  2. Open via windows explorer
    1. Type powershell in the Address Bar (older windows versions)
    2. Add powershell-here menu option using powershell (windows 7)
      1. http://www.powershellmagazine.com/2013/06/25/pstip-how-to-start-an-elevated-powershell-from-windows-explorer/
    3. Use open powershell here menu option (windows 8)

IV. Technique: Git Commands

  1. Adding (Staging)
    1. git add PATH
      1. Stage specified in the file path (e.g. PATH of '*.txt' would add all files in dir with txt extension, quotes required)
      2. PATH may contain subfolders (e.g. mainFolder/subfile.txt)
    2. git add -A
      1. Stages all files (new, modified, deleted)
      2. May require :/ at end of line to add the entire directory tree in the git repository
    3. git add .
      1. Stages all new and modified files but NOT deleted
    4. git add -u
      1. Stages all modified and deleted files but NOT new
  2. Removing (from staged)
    1. git reset PATH
      1. Removes file or files specified in PATH from the staged files
      2. This however leaves the files but unstages them from the repository
    2. git rm PATH
      1. Physically removes file or files in the PATH (deletes the files)
      2. Removes the files from the git repository
  3. Committing to local repository
    1. git commit -m "description"
  4. Reverting a commit
    1. git revert KEY-ID
      1. Where KEY-ID is the ID of the commit to revert
      2. Adds anything that was removed in that commit and removes anything added
      3. Maintains history (safest undo strategy)
  5. Reverting to previously saved version of a file
    1. git checkout -- PATH
      1. Reverts file or files in PATH to prior status from the last commit
  6. Branching
    1. git branch BRANCH
      1. Where BRANCH is the new branch name to create
    2. git checkout BRANCH
      1. Switch to BRANCH from the current branch (e.g. master)
    3. git checkout master
      1. Return to the main branch, in this case, master
      2. Perform this prior to merging a branch into master
    4. git merge BRANCH
      1. Merges changes in BRANCH into the currently active branch (e.g. master or other main branch)
    5. git branch -d BRANCH
      1. Delete branch named BRANCH from the current repository
  7. Adding a remote repository
    1. git remote add origin URL
      1. Where URL is the location of the remote repository (e.g. https://github.com/you/your_git.git)
  8. Storing changed files (not added/committed) in stash and setting head to last committed changes
    1. git stash
      1. Useful if local commit has a few insignificant file changes and need to pull from origin master
  9. Synchronizing with remote repository
    1. git push ALIAS BRANCH
    2. git push origin BRANCH
      1. If BRANCH is left blank, pushes the remote changes to the origin branch of the same name
      2. If BRANCH is set to MASTER, then changes are written to the origin master
    3. git push -u origin BRANCH
      1. Current parameters (e.g. BRANCH = master) are saved if -u is specified
      2. Future calls to "git push" without parameters will assume, in this case, defaulted "origin master"
    4. git pull
      1. Downloads and incorporates changes from remote repository
    5. git merge BOOKMARK / BRANCH
      1. Merges the bookmarked branch into the current local working branch
    6. git fetch BOOKMARK
      1. Downloads all bookmark history from the remote repository

V. Technique: Utility Functions

  1. git status
    1. Show current git repository status
  2. git log
    1. Show repository history of commits
  3. git diff HEAD
    1. Shows differences between current status and most recent commit
  4. git diff --staged
    1. Shows file differences between what is staged and what was last committed
  5. git remote show origin
    1. Origin URL, configuration for push/pull and current branch
  6. git config credential.helper store
    1. Enables git to save credentials for the current repository when entered the next time
    2. Precaution: Storage of git credentials is stored in a plain text file (not encrypted)

VI. Technique: Rescue methods for give-up situations

  1. Reset remote-master to origin, while still maintaining local files
    1. Perform "git add" and "git commit" first on local repository
    2. git fetch origin master
    3. git merge -s recursive -X theirs origin/master
  2. Reset remote-master to origin-master (local repository is completely overwritten)
    1. git reset origin/master
      1. Resets the remote master to origin master, but retain files in the local directory
    2. git reset --hard origin/master
      1. Resets the remote master to origin master AND overwrites or deletes files in the local directory
  3. Reset the git cache to abide by git.ignore (Visual Studio sometimes includes files despite their being in ignore)
    1. First add and commit any pending changes
    2. git rm -r --cached .
    3. git add .
    4. git commit -m "re-setting git cache based on updated .gitignore"

Images: Related links to external sites (from Bing)

Related Studies