SSaquif / git-notes

All things git

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Git Notes

All things git

This is a work in progress

TODO (incomplete list):

  1. Basics of branches, (check the workflow for now)
  2. Git rebase
  3. Git hooks

Contents

Creating Snapshots

Initializing a repository

git init

Viewing the status

git status
git status -s # Short status

Staging (Adding) files

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

Committing the Staged Files

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

Skipping the staging area

git commit -am “Message”  # add + commit all changed files

Removing files

git rm file1.js           # Removes from working directory and staging area
git rm --cached file1.js  # Removes from staging area only

Renaming or moving files

git mv file1.js file1.txt

Pushing to an Remote Repo

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

View List of Remote Repos

git remote -v

Adding & Removing Remote Repos

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

Viewing the staged/unstaged changes

git diff            # Shows unstaged changes
git diff --staged   # Shows staged changes
git diff --cached   # Same as the above

Viewing the history

git log              # Full history
git log --oneline    # Summary
git log --reverse    # Lists the commits from the oldest to the newest

Viewing a commit

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

Unstaging files (undoing git add)

git restore --staged file.js # Copies the last version of file.js from repo to index

Discarding local changes

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

Restoring an earlier version of a file

git restore --source=HEAD~2 file.js

Reverting Commits

Stacko

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 Submodules

Adding a Submodule

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>

Specifying Branch

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

Updating a Submoudle

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.

Removing a submodule

Video Tutorial

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>'

About

All things git