Skip to content

Basic Git Commands

Matt Brown edited this page Jan 13, 2021 · 2 revisions

Configuring global author information

git config --global user.name "John Doe"
git config --global user.email "j.doe@eggplant.io"

Initializing a new local repo

git init

Cloning a remote repo

git clone <remote_path>                          // you will need an SSL key configured to use the SSL path

Listing remote repos

git remote -v                                    // lists the names of any remotes for both fetching and pushing

Checkout branches

git checkout <existing_branch_name>              // updates project to reflect latest known status of specified branch
git checkout -b <branch_name>                    // creates a new branch from current branch/state with specified name

Staging files to be committed

git add --all                                    // stages ALL files
git add .                                        // stages all files from present working directory recursively
git add <path_to_file>                           // stages a specific file

Committing staged changes

git commit                                       // enters VIM (by default) to allow for longer commit messages
git commit -m "<message>"                        // short form; useful for small commit messages

Pushing changes to remote

git push                                         // pushes committed changes corresponding up-stream branch on remote repo
git push --set-upstream <remote> <branch_name>   // sets an upstream branch on remote repo for tracking changes; required for new branches
git push -u <remote> <branch_name>               // shortcut for the above command

View previous commits

git log                                          // displays branch commit history including author, date and commit message; press Q to exit
git log --no-decorate --oneline --graph          // displays an abbreviated history with helpful graph

View differences

git diff                                         // displays differences between unstaged changes and most recent commit on branch
git diff --staged                                // includes staged changes in comparison to most recent commit on branch

Using stash functionality

git stash -u                                     // stashes all changes to current working directory; reverts to previous commit
git stash pop                                    // removes last stash from array and applies changes to current working directory