If you are new to git, I suggest completing Git-it workshop, then come back to this summary for a refresh.

This document is a copy&paste of tips at Git-it Step-by-step instructions with some additions.

  1. Get Git Install and configure Git

    - git-for-windows.github.io for Windows
    - mac.github.com for Mac
    - Git itself

    Open terminal (aka Bash, aka Shell, aka Prompt) and verify whether git is there:
    $ git --version
    Set your name:
    $ git config --global user.name "<Your Name>"
    Set your email:
    $ git config --global user.email <youremail@example.com>

    Dollar signs $ are often used in programming documentation to signify that the line is command line code (see the code snippets above). You don't actually type the $ in though, only type what comes after.

  2. Repository Create a local repository

    Make a new folder (aka directory)
    $ mkdir <FolderName>
    Navigate into an existing folder (aka change directory)
    $ cd <FolderName>
    List the items in a folder
    $ ls 
    $ dir 
    Turn Git on for a folder
    $ git init
  3. Commit to it Check status, add and commit changes

    After you have done some changes to files in git folder:
    Check status of changes to a repository
    $ git status
    View changes to files
    $ git diff
    Add a file's changes to be committed
    $ git add <Filename>
    To add all files changes
    $ git add .
    To add all files changes including removed files
    $ git add --all .
    To commit (aka save) the changes you've added with a short message describing the changes
    $ git commit -m "<your commit message>"
    To replace the last commit
    $ git commit --amend
    Undo $ git add <Filename>
    $ git reset <Filename>
    Undo $ git add .
    $ git reset
    Undo $ git commit ...
    $ git reset --soft HEAD^
    Remove files from staging area, but keep the in work-tree (src)
    git rm --cached <file>
    List commits
    $ git log
    $ git log --pretty=oneline
  4. GitHubbin Get a GitHub account

    Visit github.com and sign up for a free account.
    Add your GitHub username to your configuration:
    $ git config --global user.username <USerNamE>
  5. Remote Control Connect local repositories to remote ones on GitHub.com

    Git Remotes

    Add remote connections
    $ git remote add <RemoteName> <URL>
    Set a URL to a remote
    $ git remote set-url <RemoteName> <URL>
    Pull in changes
    $ git pull <RemoteName> <RemoteBranch>
    View remote connections
    $ git remote -v
    Push changes
    $ git push <RemoteName> <RemoteBranch>
    Push changes to a different branch
    $ git push <RemoteName> refs/heads/<LocalBranch>:refs/heads/<RemoteBranch>
    or shorter
    $ git push <RemoteName> <LocalBranch>:<RemoteBranch>
  6. Forks and Clones Fork and clone an open source repository

    Git Clone

    Add remote connections
    $ git remote add upstream <URL>
    View remote connections
    $ git remote -v
  7. Branches Create a branch for features/changes

    Git Branches

    Take a look at GitHub Guide.



    Create and switch to a branch in one line:
    $ git checkout -b <BranchName>
    Create a new branch:
    $ git branch <BranchName>
    Move onto a branch:
    $ git checkout <BranchName>
    List the branches:
    $ git branch
    List all branches, including remote ones:
    $ git branch -a
    Rename a branch you're currently on:
    $ git branch -m <NewBranchName>
    Verify what branch you're working on
    $ git status
  8. Collaborators Add and sync with a collaborator

    Collaborators are other GitHub users who are given permission to make edits to a repository owned by someone else.

  9. Pull, Never Out of Date Push and pull to sync changes to GitHub.com

    Git Pull

    Check Git status
    $ git status
    See changes to the remote before you pull in
    $ git fetch --dry-run
    Pull in changes from a remote branch
    $ git pull <RemoteName> <RemoteBranch>
  10. Requesting You Pull Create a pull request

    Git Pull Request

  11. Merge Merge and delete branches

    Merge a branch into current branch
    $ git merge <BranchName>
    Change the branch you're working on
    $ git checkout <BranchName>
    Delete a local branch
    $ git branch -D <BranchName>
    Delete a remote branch
    $ git push <RemoteName> --delete <BranchName>
    Pull from a remote branch
    $ git pull <RemoteName> <BranchName>
  12. Tags Mark important commits with tags (eg. v1.0.0)

    Tag last commit on current branch:
    $ git tag <TagName>
    List tags:
    $ git tag
    Push one tag to a remote repo:
    $ git push <RemoteName> refs/tags/<TagName>:refs/tags/<TagName>
    or shorter
    $ git push <RemoteName> <TagName>:<TagName>
    Push all tags to default remote:
    $ git push --tags
    Remove a tag (locally):
    $ git tag -d <TagName>
    Remove a tag from remote:
    $ git push <RemoteName> :refs/tags/<TagName>
    or shorter
    $ git push <RemoteName> :<TagName>
    or more explicit
    $ git push --delete <RemoteName> <TagName>

Using Git A list of usefull comands

/* Set up Git Configuration */

git config --global user.email "you@yourdomain.com"

git config --global user.name "Your Name"

git config --global core.editor "vi"

git config --global color.ui true

/* See Git configuration */

git config --list

/* to initialise a local repository */

git init

/* adds a file to the repo */

git add

/* commit the change to git */

git commit -m "Message goes here"

/*  see the commits */

git log

/*  Git has a 3 Tier Architecture:  Working - Staging Index - Respository

Changes to files are put in a Checksum SHA-1 hash 40digit value containing parent hash, author and message.

HEAD is the latest commit of the checked out branch */

/*  Basic Commands  */

git status  /*  the command 'git status' tells which files are not added or committed from Working to Staging to Repository */

git commit -m "" /*  Commits and changes to all files that are in Staging Tier into Repository  */

git diff /*  show changes between Working and Repository, no file supplied shows all files  */

git diff --staged /*  shows changes between Staged and Respository  */

git rm file.txt /*  will remove file from working then git commit -m "" to also remove from Repo */
git rm --cached file.txt /* leaves copy of file in Working but removes from Staging and Repo */

git mv /*  rename or move files - then git commit -m "" to move to Repo */

git commit -am "text goes here" /* adds all files straight to Repo from Staging if they have changes - meaning they skip git add */

git checkout -- file.txt /*  restore Repo file to Working Directory using current branch  */

git reset HEAD file.txt /*  Move a Stage file out of Stage back to Working */

git commit --amend -m "message" file.txt /* Change last commit to Repo (only last one can change) */

/* Reverting --soft --mixed --hard will go back to previous commits*/
git log /* gets the sha1s so you can see the coomits where you want revert  back to  */

git reset --soft sha /* changes Repo but not STaging or Working */

git reset --mixed sha /* changes Repo and STaging but not Working */

git reset --hard sha /* changes all 3 Tiers */

git clean -f /* remove untracked files from Working  */

.gitignore /* ignores files to track in Working / track the .gitignore file */

Global Ignore /* create in home folder  */
/* Add in  */

git config --global core.excludesfile ~/.gitignore_global /* add to gitconfig */

/* Stop tracking changes */

git rm --cached file.txt /* leaves copy in Repo and Working */

/* Track Folders changes

Add an invisble file to a folder like .gitkeeper then add and commit */

/* Commit Log  */
git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="DUzun"
git log --grep="temp"

/* Show Commits */

git show dc094cb /*  show SHA1 */

/* Compare Commits

Branches */

git branch /*  Show local branches * is the one we are on */

git branch -r /* Shows remote branches */

git branch -a /* Shows local and remote */

git branch newbranch /* creates a new branch */

git checkout newbranch /* switch to new branch */

git checkout -b oldbranch /* creates and switches to new branch  */

/* Diff in Branches */

git diff master..otherbranch /*  shows diff */

git diff --color-words master..otherbranch /*  shows diff in color */

git branch --merged /*  shows any merged branches */

/* Rename Branch */

git branch -m oldname newname

/* Delete  Branch */

git branch -d nameofbranch

/* Merge Branch  */

git merge branchname /* be on the receiver branch */

/* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */

git merge --abort /*  Abort basically cancels the merge */

/* Manually Fix Files and commit

The Stash */

git stash save "text message here"

git stash list /* shows whats in stash */

git stash show -p stash@{0} /* Show the diff in the stash */

git stash pop stash@{0} /*  restores the stash deletes the tash */

git stash apply stash@{0} /*  restores the stash and keeps the stash */

git stash clear /*  removes all stash */

git stash drop stash@{0}

/* Remotes
You fetch from the remote server, merge any differences - then push any new to the remote - 3 branches work remote server branch, local origin master and local master

Create a repo in GitHub, then add that remote to your local repo */

git remote add origin https://github.com/neilgee/genesischild.git /*  origin can be named whatever followed by the remote */

git remote /* to show all remotes */

git remote show origin /*to see remote URL*/

git remote remove origin /* to remove remote */

git remote rm origin /* to remove remote */

/* Push to Remote from Local */

git push -u origin master

/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */

git clone https://github.com/neilgee/genesischild.git nameoffolder

/* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */

git push

/* Fetch changes from a cloned Repo */

git fetch origin /*  Pulls down latest committs to origin/master not origin, also pull down any branches pushed to Repo

Fetch before you work
Fetch before you pull
Fetch often */

/* Merge with origin/master */

git merge origin/master

/* git pull = git fetch + git merge

Checkout/Copy a remote branch to local */

git branch branchname origin/branchname /*  this will bring the remote branch to local and track with the remote */

/* Delete branch */

git branch -d branchname

/* Checkout and switch branch and track to remote */

git checkout -b nontracking origin/nontracking

/* Remove remote branch */

git push origin --delete branch