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

Return lists of all commits with specs: "commit_id/Author/Date/Description"git log
Differences between 2 commitsgit diff commit_id1 commit_id2
Show difference of "commit_id" with its parentgit show commit_id
Show a single commitgit log -n1
Show which files has changed since last commitgit status
Show changes between working directory and staging area (no argument)git diff
Compare changes between staging area and repositorygit diff --staged
Show current branch (no argument)git branch
Switch to "new_branch_name"git checkout new_branch_name
Visualize branches structures of: "master" and
"branch_name1", with short description (oneline)
git log --graph --oneline master branch_name1
Show url for push/pull actions git remote -v

3. Git commands: action

Copy entire repository from remote to localgit clone repo_url
Restoring a previous commitgit checkout commit_id
Gives a warning: "you are in detached `HEAD` state". You can detach the "HEAD" by switching to a previous commit.
Create a new branchgit branch new_branch_name
Check remote repositorygit remote
Add the remote repositorygit remote add origin url
Add to the staging area all files and folders in working *dir*git add \*
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. git checkout seotweaks
  2. git merge -s ours master
  3. git checkout master
  4. git merge seotweaks