Git command quick reference - perrigoh/learner_journal GitHub Wiki
WIP: Usable
A list of frequently used git commands for quick reference. For a complete list of git commands refer git Documentation under Reference section.
- Initialise local repository without GitHub remote repository.
Steps:
-
Change directory to the directory (folder) where the project is to be stored:
$ cd Google\ Drive/Colab\ Notebooks/test_learner_journal NO OUTPUT
The prompt will change to from
[wmxpg] ~
to[wmxpg] ~/Google Drive/Colab Notebooks/test_learner_journal
-
Initialise the directory (folder):
$ git init Initialized empty Git repository in C:/Users/wmxpg/Google Drive/Colab Notebooks/test_learner_journal/.git/
-
Check the status:
$ git status On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)
The setup is completed.
- Initialise local repository with GitHub remote repository.
Steps:
-
Change directory to the directory (folder) where the project is to be store:
$ cd Google\ Drive/Colab\ Notebooks/learner_journal/ NO OUTPUT
The prompt will change to from
[wmxpg] ~
to[wmxpg] ~/Google Drive/Colab Notebooks/learner_journal
-
Initialise the local directory (folder):
$ git init Initialized empty Git repository in C:/...Google Drive/Colab Notebooks/learner_journal/.git/
-
Check the status:
$ git status On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)
-
Create an empty README file (local):
$ echo > README.md NO OUTPUT
-
Stage the README.md file (local):
$ git add . warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it
-
Commit the changes with commit message (local) without going through the editor.
-m
stand for message, specify message'docs (readme): initial commit'
:$ git commit -m 'docs (readme): initial commit' [main (root-commit) d95f5bd] docs (readme): initial commit 1 file changed, 1 insertion(+) create mode 100644 README.md
-
Log in to Github web interface to create a new repository name
learner_journal
. At the Quick setup page copy the repository HTTPS URL. -
Add a remote name for identifying the remote repository, commonly used name is
origin
although it can be any other name. Specify the remote repository's URL `https://github.com/perrigoh/learner_journal.git':$ git remote add origin https://github.com/perrigoh/learner_journal.git NO OUTPUT
-
To ensure the remote name has been created.
-v
stand for verbose which means to display more details:$ git remote -v origin https://github.com/perrigoh/learner_journal.git (fetch) origin https://github.com/perrigoh/learner_journal.git (push)
-
Push all the changes (git commit) in the local directory (folder) to remote repository name
origin
and branch namemain
.
Note: Branch namemain
is more commonly used now thanmaster
.
-u
=--set-upstream
is to add upstream tracking reference:$ git push -u origin main Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 228 bytes | 228.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/perrigoh/learner_journal.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.
Note:
-u
set-upstream only needed when pushing the local repository to remote repository the first time, subsequent push usegit push origin main
will do. And if where to push the repository is not specifygit push
, then the default is origin.
Example of error message that occurred when pushing the local repository without setting the up-stream:
$ git push
fatal: The current branch gh-pages has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin gh-pages
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
The setup is completed.
For more pictorial instruction, refer to GitHub Docs.
To clone a remote repository to local computer.
Steps:
-
Change directory to the folder (directory) where the cloned repository is to be stored. In this case the repository will be store in the
Colab Notebooks
folder in the Google drive:$ cd Google\ Drive/Colab\ Notebooks NO OUTPUT
The prompt will change to from
[wmxpg] ~
to~/Google Drive/Colab Notebooks
. -
Log in to Github web interface and navigate to the existing repository. Click on the green
<> Code
button and from the drop down copy the HTTPS URL. -
Clone the remote repository:
$ git clone https://github.com/perrigoh/perrigoh.git Cloning into 'perrigoh'... remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 0), reused 4 (delta 0), pack-reused 0 Receiving objects: 100% (6/6), done
A new folder (directory) named
perrigoh
along with all the remote repository files had been copied to the local computerGoogle\ Drive/Colab\ Notebooks
. -
Change directory to the newly create directory (folder):
$ cd perrigoh NO OUTPUT
The prompt will change to from
[wmxpg] ~/Google Drive/Colab Notebooks
to[wmxpg] ~/Google Drive/Colab Notebooks/perrigoh
. -
Ensure those files in the remote repository have been downloaded.
-a
stand for all directories and files including those with a period in front (invisible) info source:$ ls -a ./ ../ .git/ .gitignore README.md
There are one
.git/
folder with.gitignore
file in it and one README.md file in the repository. -
After the usual editing of changes or add new files, stage the commit using
git add .
andgit commit
, push all the changes in the local directory (folder) to remote repository nameorigin
and branch namemain
.Note: Branch name
main
is more commonly used now thanmaster
.-u
=--set-upstream
is to add upstream tracking reference:$ git push -u origin main Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 322 bytes | 107.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/perrigoh/perrigoh.git 6a71f55..138c54e main -> main branch 'main' set up to track 'origin/main'.
Note:
-u
set-upstream only needed when pushing the local repository to remote repository the first time, subsequent push usegit push origin main
will do. And if where to push the repository is not specifygit push
, then the default is origin.Example of error message that occurred when pushing the local repository without setting the up-stream:
$ git push fatal: The current branch gh-pages has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin gh-pages To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'.
The cloning process is completed.
Remove link between the local repository and the remote repository info source
Steps:
-
Change directory to the directory (folder) where the local repository is:
$ cd Google\ Drive/Colab\ Notebooks/image_classifier_pytorch_udacity NO OUTPUT
The prompt will change to from
[wmxpg] ~
to[wmxpg] ~/Google Drive/Colab Notebooks/image_classifier_pytorch_udacity
-
Remove the remote name, all remote-tracking branches and configuration settings.
rm
=remove
,origin
the commonly used name representing remote repository:$ git remote rm origin NO OUTPUT
-
Remove the
FETCH_HEAD
that is still pointing to Github:$ rm .git/FETCH_HEAD NO OUTPUT
-
To check whether the link to remote repository has been removed.
-v
=--verbose
to show the remote URL:$ git remote -v NO OUTPUT
No output means there is no link to remote repository. Example of output if there is link to remote repository:
$ git remote -v
origin https://github.com/perrigoh/image_classifier_pytorch_udacity.git (fetch)
origin https://github.com/perrigoh/image_classifier_pytorch_udacity.git (push)
To check the which changes are unstage / stage / yet to commit:
$ git status
On branch add-torcheck
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: Image Classifier Project-test17.ipynb
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
Image Classifier Project-test19.ipynb
__pycache__/
no changes added to commit (use "git add" and/or "git commit -a")
To check the current working branch:
$ git branch
* add-torcheck
issue-8-parameter-pretrained-deprecated
issue-9-rework-test18-ipynb
main 21.3.0
The branch with asterisk is the current working branch.
To add new branch to local repository.
Steps:
-
Add a new branch:
$ git branch rework-test20-ipynb NO OUTPUT
-
Checkout current branch and move to the created branch:
$ git checkout rework-test20-ipynb Switched to branch 'rework-test20-ipynb'
-
Keep a habit to ensure git is pointing to the correct branch:
$ git branch add-torcheck issue-8-parameter-pretrained-deprecated issue-9-rework-test18-ipynb main * rework-test20-ipynb
To add new branch and checkout at the same time info source.
-b stand for create new branch, new branch name rework-test20-ipynb
:
$ git checkout -b rework-test20-ipynb
Switched to new branch 'rework-test20-ipynb'
To stage commit all the files:
$ git add .
warning: in the working copy of 'Image Classifier Project-test19.ipynb',
LF will be replaced by CRLF the next time Git touches it
or
To stage commit on selected file:
$ git add README.md
TODO: NO EXAMPLE AT THE MOMENT
Using command to enter the commit message when there is no need to preview the changes before committing.
-m
stand for message, specify message 'docs (readme): initial commit'
:
$ git commit -m 'docs (readme): initial commit'
[main (root-commit) d95f5bd] docs (readme): initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
Using VS Code editor to enter the commit message gives a preview of changes that will be committed.
Steps:
-
Record changes to the local repository:
$ git commit hint: Waiting for your editor to close the file...
Note: The terminal will hang till commit message is entered and the editor is closed.
-
VS Code editor pop up:
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch add-torcheck # Changes to be committed: # deleted: Image Classifier Project-test17.ipynb # new file: Image Classifier Project-test19.ipynb # modified: README.md # new file: __pycache__/helper.cpython-36.pyc # new file: __pycache__/helper.cpython-39.pyc #
Output in the terminal after VS Code editor is closed:
[add-torcheck 045ec41] abort add torcheck 5 files changed, 8 insertions(+), 5250 deletions(-) delete mode 100644 Image Classifier Project-test17.ipynb create mode 100644 Image Classifier Project-test19.ipynb create mode 100644 __pycache__/helper.cpython-36.pyc create mode 100644 __pycache__/helper.cpython-39.pyc
-
First time pushing local repository to remote repository, requires setting
up stream
. Add a remote nameorigin
for identifying the remote repository and the branch nameadd-torcheck
to be pushed.
-u
=--set-upstream
is to add upstream tracking reference:$ git push --set-upstream origin add-torcheck Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 8 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (7/7), 749.55 KiB | 9.61 MiB/s, done. Total 7 (delta 3), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (3/3), completed with 2 local objects. remote: remote: Create a pull request for 'add-torcheck' on GitHub by visiting: remote: https://github.com/perrigoh/image_classifier_pytorch_udacity/pull/new/add-torcheck remote: To https://github.com/perrigoh/image_classifier_pytorch_udacity.git * [new branch] add-torcheck -> add-torcheck branch 'add-torcheck' set up to track 'origin/add-torcheck'.
-
Subsequent pushing local branch (add-torcheck) to remote branch (origin/add-torcheck):
$ git push origin add-torcheck Enumerating objects: 18, done. Counting objects: 100% (14/14), done. Delta compression using up to 8 threads Compressing objects: 100% (8/8), done. Writing objects: 100% (8/8), 927 bytes | 927.00 KiB/s, done. Total 8 (delta 6), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (6/6), completed with 5 local objects. To https://github.com/perrigoh/image_classifier_pytorch_udacity.git a376ce5..ea3f1f7 add-torcheck -> add-torcheck
Store unwanted branches in close pull request (info source)
Login to GitHub repository
Step 1. Create a Pull Request
Go to the "New pull request" page on the Github and create a pull request.
Step 2. Set the right name Name your pull request the same as your source branch.
Example: “feature_123”.
Step 3. Cancel this pull request
Yes, do not merge it! Scroll down and find "Close pull request". Close it without merge.
Step 4. Delete the branch
Now you can safely delete the branch.
Step 5. How to restore? At any time, when you will need it, simply find your closed pull request on the "Pull requests" page by the filter: “head:feature_123”. Open the pull request page and press "Restore branch" button. All the working process in the branch was successfully restored!
Switching from the current branch to another branch name main
:
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
-d
stand for delete, specify the branch name rework-test20-ipynb
to delete:
$ git branch -d rework-test20-ipynb
Deleted branch rework-test20-ipynb (was 811a21c).
For deleting multiple branches add a space followed by the additional branch name
git branch -d branch-1 branch-2 branch-3
Steps:
-
Delete the remote branch. -d stand for delete, specify the remote name
origin
and the branch namewebsite
to delete:$ git push -d origin website To https://github.com/perrigoh/test_mkdocs_create_gh_pages.git - [deleted] website
Depending on which version of Git, the local repo may still retain a remote reference (Git’s remote tracking branch) that points to the deleted remote branch.
-
The prune option removes any remote tracking branch in the local repository that points to the deleted remote branch.
$ git fetch origin --prune NO OUTPUT
or
-
TODO Delete the remote tracking:
$ git branch --delete --remotes origin/branch-name YET TO TRY
-
Delete the remote branch. -d stand for delete, specify the remote name
origin
and the branch namewebsite
to delete:$ git push -d origin website To https://github.com/perrigoh/test_mkdocs_create_gh_pages.git - [deleted] website
To check the commit history.
-
List all logs with details:
$ git log commit 4d24fae7d8ab5a6125c7ce9d45585f393ac1654f (HEAD -> main, origin/main) Author: Perri Goh <[email protected]> Date: Tue Nov 1 02:43:43 2022 +0800 gitignore file
Arrow up/down to see the next section, hit 'q' to quit when done.
-
List all logs by one line:
$ git log --oneline 4d24fae (HEAD -> main, origin/main) gitignore file 8bd3adf fix: run command with/without flag and arg train.py and predict.py able to run command with/without flag and argument 281f24e fix: rebuild model with saved arch 04407fc fix: build model and train model in one file 1fbcfc6 fix: type error 8e1ee88 fix: program run on default arg if not specified
Arrow up/down to see the next section, hit 'q' to quit when done.
-
List all logs by oneline with graph:
$ git log --oneline --decorate --graph --all * 045ec41 (origin/add-torcheck, add-torcheck) abort add torcheck * 4d24fae (HEAD -> main, origin/main) gitignore file | * be1f410 (origin/issue-9-rework-test18-ipynb, issue-9-rework-test18-ipynb) abort rework ipynb keep branch as ref errors occur while editing: loss values become strange for vgg16, vgg13 is fine !python3 train.py --arch vgg16 --hidden_units 4096 --learning_rate 0.01 --epochs 1 Epoch: 1/1 Train Loss: -171548999680.000000, Valid Loss: -129730248704.000000, Train Accuracy: 0.009615 Epoch: 1/1 Train Loss: -171548999680.000000, Valid Loss: -148130955264.000000, Train Accuracy: 0.009615 |/ | * d54a16c (origin/issue-8-parameter-pretrained-deprecated, issue-8-parameter-pretrained-deprecated) fix: userwarning pretrained deprecated |/ * 8bd3adf fix: run command with/without flag and arg train.py and predict.py able to run command with/without flag and argument * 281f24e fix: rebuild model with saved arch
Arrow up/down to see the next section, hit 'q' to quit when done.
Shows the record of when the tips of branchs and other references were updated in the local repository. HEAD@{2}
means where HEAD used to be two moves ago info source:
$ git reflog
6ffb964 (HEAD -> main, origin/main) HEAD@{0}: pull: Merge made by the 'ort' strategy.
b353f1d HEAD@{1}: commit (amend): Merge remote-tracking branch 'origin/main'
ea3f1f7 HEAD@{2}: merge origin/main: Merge made by the 'ort' strategy.
268ced2 HEAD@{3}: commit: refactor: specific interpreter
4988645 HEAD@{4}: reset: moving to HEAD~1
446e666 HEAD@{5}: commit (merge): Merge branch 'main' of https://github.com/perrigoh/image_classifier_pytorch_udacity
Arrow up/down to see the next section, hit 'q' to quit when done.
git show
info source
-
View the last log details and diff:
$ git show commit 2811b79b21499144a32a57524ac37146dd30294b (HEAD -> main, origin/main) Author: Perri Goh <[email protected]> Date: Sun Nov 13 12:50:32 2022 +0800 docs(site):initial commit diff --git a/gh_pages/docs/index.md b/gh_pages/docs/index.md new file mode 100644 index 0000000..000ea34 --- /dev/null +++ b/gh_pages/docs/index.md @@ -0,0 +1,17 @@ +# Welcome to MkDocs + +For full documentation visit [mkdocs.org](https://www.mkdocs.org). + +## Commands + +* `mkdocs new [dir-name]` - Create a new project. ... \ No newline at end of file
Arrow up/down to see the next section, hit 'q' to quit when done.
-
View log details and diff with specific hash (SHA):
$ git show 76061a7 commit 76061a726a8444aa14d403530d90b4ec6064cd6a Author: Perri Goh <[email protected]> Date: Sun Nov 13 12:44:53 2022 +0800 docs: build site diff --git a/.gitignore b/.gitignore index 9eaa8f1..cdc5c2d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,37 @@ __pycache__/ # Jupyter Notebook .ipynb_checkpoints + +# Build Site +gh_pages/site/ +gh_pages/site/404.html ... \ No newline at end of file
Arrow up/down to see the next section, hit 'q' to quit when done.
Steps:
-
Stage the commit first, followed by:
$ git add . warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it
or
Add selected files to commit
$ git add README.md NO OUTPUT
-
Amend the commit message via VS Code editor:
$ git commit --amend TODO: NO EXAMPLE AT THE MOMENT
or
Use the same previous commit message:
$ git commit --amend --no-edit [main 4290d1d] docs (readme): rephrase content Date: Thu Dec 29 09:55:46 2022 +0800 1 file changed, 2 insertions(+), 2 deletions(-)
Search for commit message with a specific word:
$ git log --grep=fix
or
$ git log --grep fix
commit b578d2d918a8e6d8ffd5c929104b5da061c644eb
Author: Perri Goh <[email protected]>
Date: Wed Nov 2 14:17:37 2022 +0800
fix: rebuild model
Arrow up/down to see the next section, hit 'q' to quit when done.
git revert
back to specific hash (SHA) info source
Better to use git revert
for remote repository as it will create a hash (SHA) for the removal, hence the previous records is still intact:
$ git revert 7d2e50d
[main c35bf11] Revert "docs (readme): add content"
1 file changed, 25 deletions(-)
Log after revert:
$ git log --oneline --decorate --graph --all
* c35bf11 (HEAD -> main) Revert "docs (readme): add content"
* 2811b79 (origin/main) docs(site):initial commit
* 76061a7 docs: build site
| * 76b0cf4 (origin/gh-pages, status, gh-pages) docs(site): gitignore
| * bc3707f Deployed 7d2e50d with MkDocs version: 1.4.2
...
* cd7d4dc initial commit
git reset
back to specific hash (SHA) info source
To premenantly remove --hard
the previous log (note: ensure that there are no changes in the remote (origin) before the specified hash):
$ git reset --hard 7d2e50d
HEAD is now at 7d2e50d docs (readme): add content
Log after reset --hard:
$ git log --oneline --decorate --graph --all
* 76b0cf4 (origin/gh-pages, status, gh-pages) docs(site): gitignore
* bc3707f Deployed 7d2e50d with MkDocs version: 1.4.2
| * 2811b79 (origin/main) docs(site):initial commit
| * 76061a7 docs: build site
|/
* 7d2e50d (HEAD -> main) docs (readme): add content
* 203b254 docs(license): initial commit
...
* cd7d4dc initial commit
Remember to update the changes to remote (origin) using git push -f origin main
.
Undo a merge commit (info source)
Steps:
-
Check the log to get the SHA that is before merge:
$ git reflog 8853c9a (HEAD -> main, origin/main) HEAD@{0}: commit (merge): docs (readme): Changes to documentation fdce63c HEAD@{1}: commit (amend): docs (readme): add content 85bf49a HEAD@{2}: commit: docs (readme): add content 203b254 HEAD@{3}: commit: docs(license): initial commit 400002b HEAD@{4}: commit: add project files e9b81ee HEAD@{5}: commit: docs: gitignore cd7d4dc HEAD@{6}: commit (initial): initial commit
-
Reset to the previous commit before merge:
$ git reset --merge fdce63c NO OUTPUT
or
$ git reset --merge HEAD~1 NO OUTPUT
-
Force push to remote repository:
note: force push only when very sure that there is no other commit after the (wrongly) merge commit in the remote (orign/main)
$ git push -f origin main Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/perrigoh/classify_dog_pytorch.git + 8853c9a...fdce63c main -> main (forced update)
-
Final check both local (main) and remote (origin/main)
HEAD
are at the same SHA:git reflog 4988645 (HEAD -> main, origin/main) HEAD@{0}: reset: moving to HEAD~1 446e666 HEAD@{1}: commit (merge): Merge branch 'main' of https://github.com/perrigoh/image_classifier_pytorch_udacity 4988645 (HEAD -> main, origin/main) HEAD@{2}: commit (amend): readme: update content db0c3ba HEAD@{3}: commit: readme: update content 5504b5d HEAD@{4}: commit: check train.py and predict.py error free
Steps:
-
Check the status:
$ git status On branch main Your branch and 'origin/main' have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean
-
Check what are the differences:
$ git log --oneline --decorate --graph --all * a376ce5 (origin/main) docs (readme): add license shields | * 268ced2 (HEAD -> main) refactor: specific interpreter |/ * 4988645 readme: update content * 5504b5d check train.py and predict.py error free
Remote (origin/main) is at SHA
a376ce5
and local (main) is at SHA268ced
, in this case usegit fetch
instead ofgit pull
. -
Copy the remote (origin/main) to local (main):
$ git fetch origin main From https://github.com/perrigoh/image_classifier_pytorch_udacity * branch main -> FETCH_HEAD
-
Merge the copied
origin/main
which is now in the local with local (main)$ git merge origin/main Merge made by the 'ort' strategy. README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
-
Update the remote repository to be the same as local repository:
$ git push origin main Enumerating objects: 18, done. Counting objects: 100% (14/14), done. Delta compression using up to 8 threads Compressing objects: 100% (8/8), done. Writing objects: 100% (8/8), 927 bytes | 927.00 KiB/s, done. Total 8 (delta 6), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (6/6), completed with 5 local objects. To https://github.com/perrigoh/image_classifier_pytorch_udacity.git a376ce5..ea3f1f7 main -> main
-
Check status ensure HEAD is pointing to both main (local) and origin/main (remote)
git log --oneline --graph --all * ea3f1f7 (HEAD -> main, origin/main) Merge remote-tracking branch 'origin/main' |\ | * a376ce5 docs (readme): add license shields * | 268ced2 refactor: specific interpreter |/ * 4988645 readme: update content * 5504b5d check train.py and predict.py error free
Error example:
$ git push -f origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: GH006: Protected branch update failed for refs/heads/main.
remote: error: Cannot force-push to this protected branch
To https://github.com/perrigoh/classify_dog_pytorch.git
! [remote rejected] main -> main (protected branch hook declined)
error: failed to push some refs to 'https://github.com/perrigoh/classify_dog_pytorch.git'
info source On GitHub.com, navigate to the main page of the repository. Under your repository name, click Settings. In the "Code and automation" section of the sidebar, click Branches. To the right of the branch protection rule you want to delete, click Delete.
Use Toptal | gitignore.io to generate list of files not to be track base on Operation Systems, IDEs or Programming Languages. Copy and paste those that are relevant into .gitignore.
TODO ### Git stashing Reference: https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning