All things git
This is a work in progress
TODO (incomplete list):
- Basics of branches, (check the workflow for now)
- Git rebase
- Git hooks
- Git Notes
- Contents
- Creating Snapshots
- Initializing a repository
- Viewing the status
- Staging (Adding) files
- Committing the Staged Files
- Skipping the staging area
- Removing files
- Renaming or moving files
- Pushing to an Remote Repo
- View List of Remote Repos
- Adding & Removing Remote Repos
- Viewing the staged/unstaged changes
- Viewing the history
- Viewing a commit
- Unstaging files (undoing git add)
- Discarding local changes
- Restoring an earlier version of a file
- Reverting Commits
- Git Submodules
git init
git status
git status -s # Short status
git add file1.js # Stages a single file
git add file1.js file2.js # Stages multiple files
git add \*.js # Stages with a pattern
git add . # Stages the current directory
Alternatively on VSCode you can use the +
symbol in the source control tab to add files to the staging area
git commit -m “Message” # Commits with a one-line message
git commit # Opens the default editor to type msg
Alternatively you can write the commit message on VSCode. Go to the source control tab, then in the commit message text box (at the top) write your message and click the tick icon
git commit -am “Message” # add + commit all changed files
git rm file1.js # Removes from working directory and staging area
git rm --cached file1.js # Removes from staging area only
git mv file1.js file1.txt
git push <remote-name> <branch-to-push>
# example
git push origin master
You can also push from VSCode Source Control tab. Click on the ...
icon and then select push
git remote -v
git remote add <remote-name> <repo-url> # add
# example
git remote add origin <url u got from github, gitlab etc>
git remote rm <remote-name> # remove
# example
git remote rm origin
git diff # Shows unstaged changes
git diff --staged # Shows staged changes
git diff --cached # Same as the above
git log # Full history
git log --oneline # Summary
git log --reverse # Lists the commits from the oldest to the newest
git show 921a2ff # Shows the given commit
git show HEAD # Shows the last commit
git show HEAD~2 # Two steps before the last commit
git show HEAD:file.js # Shows the version of file.js stored in the last commit
git restore --staged file.js # Copies the last version of file.js from repo to index
git restore file.js # Copies file.js from index to working directory
git restore file1.js file2.js # Restores multiple files in working directory
git restore . # Discards all local changes (except untracked files)
git clean -fd # Removes all untracked files
git restore --source=HEAD~2 file.js
In order to do it locally, you can do the following commands to go to master and move it to the old commit.
git checkout master
git reset --hard <old_commit_id>
If you then want to push it to the remote, you need to use the -f option.
git push -f origin master
git submodule add <ssh/url-link-to-repo>
# add into a nested folde
git submodule add <path_to_local_folder>/<ssh/url-link-to-repo>
You can specify the branch to add and pull from in the .gitmodules file
by adding the branch as below
[submodule "git-notes"]
path = git-notes
url = git@github.com:SSaquif/git-notes.git
branch = main
You can do update with remote switch
Why we need --remote, if we want to pull master
git submodule update # Update using the revison being tracked
git submodule update --remote # update all
git submodule update --remote <path to the submodule> # path can be found in .gitmodules file
Note: (Need to verify this). Apparently another way is, you can go into the individual submodule from the parent/super repo and do a regular pull (fetch and merge) in those modules.
There are a few steps to it, see below
# Step 1: git remove
git rm <module_name>
# Step 2: remove form the '.git' folder, the folder is usually has same name as module
rm -rf .git/modules/module_folder
# Step 3: Remove it from the '.gitmodules' file. (This happens automatically now, but still check)
# Step 4: Add and commit changes
git commit -am 'Removed submodule <module name>'