Wednesday, October 12, 2016

Git Command Cheat Sheet

Git        

  • Git is distributed; whereas, TFS is centralized.
  • Unlike all other version control systems, git does not save deltas. Instead, git saves a new copy of a file when it changes. For a given remote repository, the whole thing will have been cloned locally. The local clone is thus also referred to as the local repository. All versions of all files for all branches will have been cloned into the same local folder as if they’ve been overlaid on top of each other and thus occupy the same space, but in fact, they are stored in the “.git” folder, which is created and maintained by git.
  • Even pending files seem to occupy the same space as checked-in files. Git refers to files having pending changes as “modified”. To check modified files into the remote repository, the developer first uses a command such as “add” to move the modified files to “staging”, and then the developer uses the “commit” command to move the staged files into the local repository. Then the developer uses the “push” command to move the files from the local repository to the remote repository.

Get Latest

  1. git pull
    • For the current branch, gets the latest code from the local repository and merges in into the code in your working directory
  2. git pull --origin [branchName]
    • Update my local repository from the remote repository for the currently selected branch in the .git folder AND remove local branches that have been removed on the remote. Basically, this creates a MATCH of the remote repository and cleans up the local repository of unneeded branches.
  3.  

Check In

  1. git status
    • Shows any files that have been edited (red) or added (green). Both will say “modified”
  2. git add .
    • Adds everything that has been edited
  3. git rm *pathsubstring*
    • Removes the matching files from staging and from working directory, the next commit will remove it from the repository
  4. git commit
    • -m "fixed bug in node helper"
    • -v
      • Brings up the vim editor, which will include a list of the changes to be committed
    • -a
      • Does an add first for any files that are already tracked, thus skipping staging
    • --amend
      • Includes the changes in staging in the previous commit
  5. git push origin [branchName]
    • Syncs the local branchName with the remote "origin" of the same branchName

Undo Pending Changes

  1. git clean –df
    • Undo any added or modified directories or files but not any deleted files. seems to only undo untracked changes
  2. git reset --hard HEAD~1
    • Undo all local changes including any deleted files. Start at HEAD with the specified offset
  3. git reset --soft HEAD~
    • Move the previously committed changes back to staging
  4. git reset HEAD
    • Move the file from staging back to the working directory
  5. git rm
    • Removes the file from staging and from working directory, the next commit will remove it from the repository
  6. git rm --cached
    • Removes the file from staging, but leaves it as untracked in the working directory
  7. git rm \*.log
    • Removes all files from the current folder whose name ends with .log and regardless of state
  8. git checkout --
    • Undo unstaged changes to the file

Merge the Latest Develop (trunk) code to my Local Branch


  1. git checkout develop
    •  Since, for the repository, all files for all branches will have been cloned and thus exist in the .git folder, the user can specify that subsequent commands will apply to the files in the develop branch and it will not have been necessary to communicate with the remote repository.
  2. git pull
    • Update my local repository from the remote repository for the currently selected branch in the .git folder. Do not delete local branches that have been removed on the remote repository.
  3. git checkout [branchName]
    • Since, for the repository, all files for all branches will have been somehow cloned into the same local folder, specify that subsequent commands will apply to the files in the specified branchName.
  4. git merge develop
    • For the code most recently pulled from the repository for the develop branch, merge it into my local files for the current branch, which should b
  5. git fetch origin develop:develop
    • fetches the commits from origin/develop and updates local/develop
    • can run this command instead of switching to develop, then pull, then switch back
  6. git rebase develop
    • Finds the common parent (E), then reconnects A to G. If merging (not rebase) then there would be 5 commits (F, G, A, B, C) in the PR.  if rebase, then there would be 3 commits (A, B, C)
    • Assume the following history exists and the current branch is "topic":
                A---B---C topic
               /
          D---E---F---G develop
      From this point, the result of either of the following commands:
      git rebase develop
      git rebase develop topic
      would be:
                        A'--B'--C' topic
                       /
          D---E---F---G develop

Diff

  1. git diff
    • Compare working directory with staging area
  2. git diff --staged
    • Compare staged files to last commit (local repository)
  3. git difftool --tool-help

Moving/Renaming a File

  • Full Process: A three-step process because git does not track file movement or renaming
    1. mv
      • Renames the file, but any tool could have been used for this step
    2. git rm
    3. git add
  • Shortcut: a shortcut that does the full three-step process
    1. git mv
      • Renames the file, but any tool could have been used for this step

History

  1. git log
    • -p
      • shows the diferences
    • -2
      • limits the history to the last two commits
    • --stat
      • stats about changes
    • --pretty=oneline
      • one line per commit
    • --pretty=format:”%h - %an, %ar : %s”
    • --pretty=format:”%h %s” –graph § --since=2.weeks
    • --since=”2 years 1 day 3 minutes ago”
    • --since=”2010-01-05”
    • -Smyfunctionname
      • Find any changes to the string “myfunctionname”
    • --grep
      • Show only the commits whose commit message contains the string
    • -- t/
      • Show only the commits that modified files in the t directory, must be the last option

Remote Servers

  1. git remote
    • Show the remote servers, which for us only includes the server named “origin”
  2. git remote -v
    • Show the remote servers with their URLs 

Branching

  1. git branch
    • displays all the branches on your local system
  2. git branch -r
    • displays all branches on the remote system
  3. git branch -a
    • displays all branches on local AND remote system
  4. git branch [branchName]
    • Creates a new LOCAL branch named [branchName] from the currently checked out branch
  5. git push -u origin [branchName]
    • Creates a new REMOTE branch named [branchName] from the currently checked out branch

Cleaning up

  1. git gc
    • Runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects which may have been created from prior invocations of git add. Users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.