Skip to content

Instantly share code, notes, and snippets.

@hemenkapadia
Last active December 3, 2019 19:08
Show Gist options
  • Select an option

  • Save hemenkapadia/03a31e82d7fc91ce10db to your computer and use it in GitHub Desktop.

Select an option

Save hemenkapadia/03a31e82d7fc91ce10db to your computer and use it in GitHub Desktop.
[Git cheatsheet] Git cheatsheet #git #cheatsheet

Git Fundamentals


  • Traditional VCS, like Subversion, CVS etc. are Centralized VCS based on where they store data
  • They are also Delta Version Control Systems (VCS) based on how they store data.
    • base version of the file is created when it was added to VCS for tracking
    • changes are stored as a list of changes (i.e. diffs or deltas) per tracked file.

Delta Version Control Systems

  • Git is a Snapshot VCS
    • think of the git repository as a miniature file system
    • after each commit, i.e. a logical unit of change in 1 or more files, a snapshot of the complete miniature file system is created. The snapshot includes
      • the complete file, which changed in that commit
      • a link to the file in the previous snapshot, if it did not change.
      • thus changes are preserved as series of snapshots and the entire filesystem can be generated for any needed snapshot
      • snapshots are checksummed with SHA-1 hash. Any further (unauthorized/backdoored) changes to the snapshot will invalidate the checksum, providing integrity to commits.

Snapshot Version Control Systems

Three Areas


  • Local Respository

    • hidden .git sub-directory in the directory created when a repository is cloned
    • it contains metadata and snapshots for all changes ever committed to the repository
    • it also contains details about all branches available locally
  • Working Tree

    • A single checkout of one entire snapshot of the directory.
    • the snapshot is extracted from the .git director, usually the last snapshot on the checkedout branch.
  • Staging area (also called Index)

    • sad
    • sdfsdf
  • Three States of a file tracked by git - SINGLE MOST IMPORTANT CONCEPT TO UNDERSTAND
    • Modified : Tracked file in working directory with changes

Git Configuration


Ignoring

  • .gitignore and .git/info/exclude can be used to ignore untracked files. If file was ever tracked this method will not work (see below)
    • Use .gitignore to exclude files universally for all contributers. This file is checked into the repo.
    • Use .git/info/exclude in case a file needs to be excluded only in the local repo of an individul. This is never checked in the repo
  • To ignore tracked files refer to the git update-index command

Git Operations


Rebasing


Rebase helps get updates from the master branch into the feature branch. While starting a new feature development we will create a feature branch from master. Over time master branch will move ahead due to commits from other team members. To get the feature branch updated with the changes (so that we do not have a giant conflict later) we should periodically rebase the feature branch to master.

git checkout master  
git pull                       # get changes from origin to local master

So now our repository will be in the state below

          A---B---C feature
         /
    D---E---F---G master

To rebase

git checkout feature
git rebase master

Will cause the repo to be in 

                  A'--B'--C' topic
                 /
    D---E---F---G maste

Checking status

# Show local branches, the remote they track and behind/ahead information between the two
git branch -vv   

# Nice colored and compact git log
git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Branch

Update and list refs

git remote update origin --prune                   # Will make local branch refs current with remote refs. Remove dangling local refs
git branch -vv                                     # list local branches and corresponding tracked remote branches

Renaming local and remote branch

git checkout old_name
git branch -m new_name                             # Rename local branch from old_name -> new_name
git push origin :old_name new_name                 # deletes old_branch on remote + push the new local branch
git push origin -u new_name                        # set upstream for the new branch

Checkout

git checkout local_branch_name                     # Checkout an existing local branch
git chechout --track origin/remote_branch          # Checkout a local branch called remote_name and set corresponding remote branch to track it

DAMAGE CONTROL

  • BFG Repo Cleaner Helps clean publich repo if some confidential information is inadvertanatly pushed to origin. Also useful in case very large files are pushed in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment