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 initgit status
git status -s # Short statusgit 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 directoryAlternatively 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 msgAlternatively 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 filesgit rm file1.js # Removes from working directory and staging area
git rm --cached file1.js # Removes from staging area onlygit mv file1.js file1.txtgit push <remote-name> <branch-to-push>
# example
git push origin masterYou can also push from VSCode Source Control tab. Click on the ... icon and then select push
git remote -vgit 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 origingit diff # Shows unstaged changes
git diff --staged # Shows staged changes
git diff --cached # Same as the abovegit log # Full history
git log --oneline # Summary
git log --reverse # Lists the commits from the oldest to the newestgit 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 commitgit restore --staged file.js # Copies the last version of file.js from repo to indexgit 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 filesgit restore --source=HEAD~2 file.jsIn 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 mastergit 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 = [email protected]:SSaquif/git-notes.git
branch = mainYou 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 fileNote: (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>'