Git Cheat Sheet

Git is the open source distributed version control system that facilitates GitHub activities on your laptop or
desktop. This cheat sheet summarizes commonly used Git command line instructions for quick reference.

MAKE CHANGES

Review edits and craft a commit transaction
[syntax type=”html|php|js|css”]$ git status[/syntax]
Lists all new or modified files to be committed
[syntax type=”html|php|js|css”]$ git add [file][/syntax]
Snapshots the file in preparation for versioning
[syntax type=”html|php|js|css”]$ git reset [file][/syntax]
Unstaged the file, but preserve its contents
[syntax type=”html|php|js|css”]$ git diff[/syntax]
Shows file differences not yet staged
[syntax type=”html|php|js|css”]$ git diff –staged[/syntax]
Shows file differences between staging and the last file version
[syntax type=”html|php|js|css”]$ git commit -m “[descriptive message]”[/syntax]
Records file snapshots permanently in version history

CONFIGURE TOOLING

Configure user information for all local repositories
[syntax type=”html|php|js|css”]$ git config –global user.name “[name]”[/syntax]
Sets the name you want atached to your commit transactions
[syntax type=”html|php|js|css”]$ git config –global user.email “[email address]”[/syntax]
Sets the email you want atached to your commit transactions
[syntax type=”html|php|js|css”]$ git config –global color.ui auto[/syntax]
Enables helpful colorization of command line output

CREATE REPOSITORIES

Start a new repository or obtain one from an existing URL
[syntax type=”html|php|js|css”]$ git init [project-name][/syntax]
Creates a new local repository with the specified name
[syntax type=”html|php|js|css”]$ git clone [url][/syntax]
Downloads a project and its entire version history

GROUP CHANGES

Name a series of commits and combine completed efforts
[syntax type=”html|php|js|css”]$ git branch[/syntax]
Lists all local branches in the current repository
[syntax type=”html|php|js|css”]$ git branch [branch-name][/syntax]
Creates a new branch
[syntax type=”html|php|js|css”]$ git checkout [branch-name][/syntax]
Switches to the specified branch and updates the working directory
[syntax type=”html|php|js|css”]$ git merge [branch][/syntax]
Combines the specified branch’s history into the current branch
[syntax type=”html|php|js|css”]$ git branch -d [branch-name][/syntax]
Deletes the specified branch

SYNCHRONIZE CHANGES

Register a repository bookmark and exchange version history
[syntax type=”html|php|js|css”]$ git fetch [bookmark][/syntax]
Downloads all history from the repository bookmark
[syntax type=”html|php|js|css”]$ git merge [bookmark]/[branch][/syntax]
Combines bookmark’s branch into current local branch
[syntax type=”html|php|js|css”]$ git push [alias] [branch][/syntax]
Uploads all local branch commits to GitHub
[syntax type=”html|php|js|css”]$ git pull[/syntax]
Downloads bookmark history and incorporate changes

REFACTOR FILENAMES

Relocate and remove versioned files
[syntax type=”html|php|js|css”]$ git rm [file][/syntax]
Deletes the file from the working directory and stages the deletion
[syntax type=”html|php|js|css”]$ git rm –cached [file][/syntax]
Removes the file from version control but preserves the file locally
[syntax type=”html|php|js|css”]$ git mv [file-original] [file-renamed][/syntax]
Changes the file name and prepares it for commit

SAVE FRAGMENTS

Shelve and restore incomplete changes
[syntax type=”html|php|js|css”]$ git stash[/syntax]
Temporarily stores all modified tracked files
[syntax type=”html|php|js|css”]$ git stash list[/syntax]
Lists all stashed changesets
[syntax type=”html|php|js|css”]$ git stash pop[/syntax]
Restores the most recently stashed files
[syntax type=”html|php|js|css”]$ git stash drop[/syntax]
Discards the most recently stashed changeset

REDO COMMITS

Erase mistakes and craft replacement history
[syntax type=”html|php|js|css”]$ git reset [commit][/syntax]
Undoes all commits afer [commit], preserving changes locally
[syntax type=”html|php|js|css”]$ git reset –hard [commit][/syntax]
Discards all history and changes back to the specified commit

REVIEW HISTORY

Browse and inspect the evolution of project files
[syntax type=”html|php|js|css”]$ git log[/syntax]
Lists version history for the current branch
[syntax type=”html|php|js|css”]$ git log –follow [file][/syntax]
Lists version history for a file, including renames
[syntax type=”html|php|js|css”]$ git diff [first-branch]…[second-branch][/syntax]
Shows content differences between two branches
[syntax type=”html|php|js|css”]$ git show [commit][/syntax]
Outputs metadata and content changes of the specified commit

SUPPRESS TRACKING

Exclude temporary files and paths

[syntax type=”html|php|js|css”]$ git ls-files –other –ignored –exclude-standard[/syntax]

Lists all ignored files in this project
[syntax type=”html|php|js|css”]*.log
build/
temp-*[/syntax]
A text file named .gitignore suppresses accidental versioning of
files and paths matching the specified patterns

 

Case 1: Don’t care about local changes

  • Solution 1: Get the latest code and reset the code
    git fetch origin
    git reset --hard origin/[tag/branch/commit-id usually: master]
    
  • Solution 2: Delete the folder and clone again :D
    rm -rf [project_folder]
    git clone [remote_repo]
    

Case 2: Care about local changes

  • Solution 1: no conflicts with new-online version
    git fetch origin
    git status
    

    will report something like:

    Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
    

    Then get the latest version

    git pull
    
  • Solution 2: conflicts with new-online version
    git fetch origin
    git status
    

    will report something like:

    error: Your local changes to the following files would be overwritten by merge:
        file_name
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    Commit your local changes

    git add .
    git commit -m ‘Commit msg’
    

    Try to get the changes (will fail)

    git pull
    

    will report something like:

    Pull is not possible because you have unmerged files.
    Please, fix them up in the work tree, and then use 'git add/rm <file>'
    as appropriate to mark resolution, or use 'git commit -a'.
    

    Open the conflict file and fix the conflict. Then:

    git add .
    git commit -m ‘Fix conflicts’
    git pull
    

    will report something like:

    Already up-to-date.
    

 

More info:

How do I use ‘git reset –hard HEAD’ to revert to a previous commit?

How can I get latest updates without committing my code(have conflicts)?
Interesting question. I've never tried this before but you may try it and report back ;) so:
- Make a patch with the changes: git diff > uncommitted-changes.patch
- Reset the branch
- pull the changes
- apply the patch