Git cheatsheet

This is a CheatSheet (or “How to…?”) that I wrote after taking the Udacity course : “How to Use Git and GitHub?” (link). The course is well built and it gives a solid overview of basic use of Git Version Control System, and in particular GitHub. The course also includes 2 useful videos on how to configure your workspace.

1. Some definitions


  1. repository: a collection of files and folders
  2. commit: a user created checkpoint. They are ordered by date
  3. tip: last commit on a branch
  4. HEAD: is the current branch
  5. Diagram
working directorystaging area repository
file1 →(add)→ file1 →(commit)→ file1
file2 →(add)→ file2 →(commit)→ file2
file3file3 repository

2. Return info and status


1
2
3
4
5
6
7
8
9
10
11
$ git log  {Return lists of all commits with specs: "commit_id/Author/Date/Description"}
$ git diff commit_id1 commit_id2 {Differences between 2 commits}
$ git show commit_id {Show difference of "commit_id" with its parent}
$ git log -n1 {Show a single commit}
$ git status {Show which files has changed since last commit}
$ git diff {Show changes between working directory and staging area (no argument)}
$ git diff --staged {Compare changes between staging area and repository}
$ git branch {Show current branch (no argument)}
$ git checkout new_branch_name {Switch to "new_branch_name"}
$ git log --graph --oneline master branch_name1 {Visualize branches structures of: "master" and<br> "branch_name1", with short description (oneline)}
$ git remote -v {Show url for push/pull actions}

3. Git commands: action


1
2
3
4
5
6
7
8
9
10
$ git clone repo_url  {Copy entire repository from remote to local}
$ git checkout commit_id {Restoring a previous commit}
{Gives a warning: "you are in detached `HEAD` state".
You can detach the "HEAD" by switching to a previous commit.}

$ git branch new_branch_name {Create a new branch}
$ git remote {Check remote repository}
$ git remote add origin url {Add the remote repository}
$ git add *  {Add to the staging area all files and folders in working dir}
$ git fetch

4. How to …


4.1. commit (locally)

  1. add files to staging area → git add file1 or git add *
  2. check what’s in the staging area → git status
  3. commit with message → git commit -m "commit_message"
    The message must be less than 72 chars, explain problems solved and why the change
  4. check status → git status

Sometimes you might have to force add with: “git add -f *”

4.2. Merge/Combine 2 branches

  1. check active branch label → git branch
  2. mergegit merge master branch_name1
  3. commitgit commit
  4. show commit mergegit log
  5. delete label → git branch -d branch_name1

4.3. Initialize a (local) new repository

  1. Go to the local folder
  2. creates .git folder where all repo info are stored → git init
  3. how which files has changed since last commit → git status (s)

4.4. Push repositories (local to remote/Github)

  1. Create empty repository on Github (Do not initialize with README if the local repository has commits)
  2. Go to the working local directory
  3. check remote repository → git remote
  4. add the remote repository → git remote add origin remote_url
  5. show url for push/pull → git remote -v
  6. Push the master branch (starting from the tip) → git push remote_name local_branch (remote_name = origin and local_branch=master) Commits that are already on remote are not pushed.
  7. The branch name will be unchanged on the remote

4.5. pull/Update repositories (remote/Github to local)

  1. git pull remote_name branch_name
    Typically, remote_name=origin and branch_name=master

4.6. Fork a repository (copy the repo of another user to your Github account)

  1. Go to another user github page
  2. fork repository → [ Github (someone) ] —fork–> [my Github / master]
  3. clone → [my Github / master] <—origin*– [clone local]

4.7. Replace master branch in git, entirely, from another branch?

1
2
3
4
$ git checkout seotweaks
$ git merge -s ours master
$ git checkout master
$ git merge seotweaks