Notes taken from Udacity's online course
- Git and Github
- Navigating a commit history
- Creating and modifying a repository
- Show hidden files
- Initialize a new git repository, there is no commit yet
- Show which file has been changed since last commit
- Add files to the staging area, aka., changes to be committed
- Remove a file from staging area
- Commit changes and write commit message
- Show any changes in the working directory that have not been added to the staging area yet
- Show changes in the staging area that have not been committed yet
- Discard any change from both the working directory and the staging area
- Branches are helpful
- Show the current branch
- Create a new branch
- Checkout the new branch
- Or create and checkout in one command
- See the visual representation of the commit history
- Delete previous branch
- Merge branch2 and branch3 into master branch
- Show the difference added by 1 particular commit without knowing its parent
- Add .gitignore file to stop tracking specific files
- Using GitHub to collaborate
diff -u old_game.js new_game.js
-u: unified diff format, making the output easier to read
git log
Shows id number, author, date, and commit message
git diff <id_number_1> <id_number_2>
git log --stat
Shows ++++-- across multiple files
Press q to exit git log
git clone <url>
To get colored diff output, run
git config --global color.ui auto
git checkout <id_number>
Show hidden files
ls -a
git init
git status
git add filename.txt
git reset filename.txt
git commit
Or
git commit -m "Commit message"
Commit message style guide: http://udacity.github.io/git-styleguide/
git diff
git diff --staged
git reset --hard
- e.g. production quality vs development
- e.g. unique feature
- e.g. experimental work
git branch
git branch new_branch_name
git checkout new_branch_name
git checkout -b new_branch_name
git log --graph --oneline branch1 branch2
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.
git checkout master
git merge branch2 branch3
git show commit_id
A collection of templates
Commonly used for python files:
config.py
__pycache__
.ipynb_checkpoints
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
git push <remote_name> <branch_name>
git pull <remote_name> <branch_name> # fast-forward merge
git pull <remote_name> <branch_name>
Equivalently
git fetch <remote_name>
git merge <branch_name> <remote_name>/<branch_name>
-
local remote c-v1 (master)
↓
b (origin/master)
↓
ac-v2 (master)
↓
b
↓
a -
local c-v1 (master)
↓ c-v2 (origin/master)
↓ ↙
b
↓
a -
local d (master)
↓ ↘
↓
c-v1 ↓
↓ c-v2 (origin/master)
↓ ↙
b
↓
a
- Make another branch
- Commit change on this new branch
- Push to remote
- Pull request
- 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 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
git reset --hard HEAD^ # remove the last commit from git
git reset --hard HEAD~2 # remove the last 2 commits from git
git push origin +master