Skip to content

Latest commit

 

History

History
279 lines (234 loc) · 7.81 KB

File metadata and controls

279 lines (234 loc) · 7.81 KB

Git and Github

Notes taken from Udacity's online course



Navigating a commit history

Find the difference using Mac

diff -u old_game.js new_game.js

-u: unified diff format, making the output easier to read

View history using Git

git log

Shows id number, author, date, and commit message

Find difference using Git

git diff <id_number_1> <id_number_2>

Statistics of which files have changed in each commit

git log --stat

Shows ++++-- across multiple files
Press q to exit git log

Clone Git repository

git clone <url>

Getting Colored Output

To get colored diff output, run

git config --global color.ui auto

Checkout an old version

git checkout <id_number>

Creating and modifying a repository

Show hidden files

ls -a

Initialize a new git repository, there is no commit yet

git init

Show which file has been changed since last commit

git status

Add files to the staging area, aka., changes to be committed

git add filename.txt

Remove a file from staging area

git reset filename.txt

Commit changes and write commit message

git commit

Or

git commit -m "Commit message"

Commit message style guide: http://udacity.github.io/git-styleguide/

Show any changes in the working directory that have not been added to the staging area yet

git diff

Show changes in the staging area that have not been committed yet

git diff --staged

Discard any change from both the working directory and the staging area

git reset --hard

Branches are helpful

  • e.g. production quality vs development
  • e.g. unique feature
  • e.g. experimental work

Show the current branch

git branch

Create a new branch

git branch new_branch_name

Checkout the new branch

git checkout new_branch_name

Or create and checkout in one command

git checkout -b new_branch_name

See the visual representation of the commit history

git log --graph --oneline branch1 branch2

Delete previous branch

git branch -d branch_name

If a branch is deleted and leaves some commits unreachable from existing branches, those commits will continue to be accessible by commit id, until Git’s garbage collection runs. You can also run this process manually with git gc.

Merge branch2 and branch3 into master branch

git checkout master
git merge branch2 branch3

Show the difference added by 1 particular commit without knowing its parent

git show commit_id

Add .gitignore file to stop tracking specific files

A collection of templates
Commonly used for python files:

config.py
__pycache__
.ipynb_checkpoints

Using GitHub to collaborate

Create a git repository online

Add this repository as a remote to the local repository

git remote      # to view and create remote
git remote add <repository_name(e.g. origin)> <url_of_remote>
git remote -v   # to check whether url was added correctly

Push

git push <remote_name> <branch_name>

Pull

git pull <remote_name> <branch_name>  # fast-forward merge

Resolving conflict

git pull <remote_name> <branch_name>

Equivalently

git fetch <remote_name>
git merge <branch_name> <remote_name>/<branch_name>
  • Before fetch:

    local remote
    c-v1 (master)

    b (origin/master)

    a
    c-v2 (master)

    b

    a
  • After fetch:

    local
    c-v1 (master)
    ↓ c-v2 (origin/master)
    ↓ ↙
    b

    a
  • After merge:

    local
    d (master)
    ↓ ↘

    c-v1 ↓
    ↓ c-v2 (origin/master)
    ↓ ↙
    b

    a

Pull request on Github

  • Make another branch
  • Commit change on this new branch
  • Push to remote
  • Pull request

Conflicting changes

  • checkout master branch
  • pull changes from Github master
  • checkout the pull-requested-branch
  • merge the pull-requested-branch with the new master branch
  • push the pull-requested-branch to remote

Fork and keep a fork up to date

  • Fork on Github
  • Clone to local
  • Add the original repository as a remote, name it upstream
  • Pull the master branch of the original repository
  • Merge the master branch into your change branch
  • Push your change branch to your fork

Removing commits on git

git reset --hard HEAD^   # remove the last commit from git
git reset --hard HEAD~2  # remove the last 2 commits from git

Force push to remove commits on Github

git push origin +master