GIT

Git stuff
!git -h
unknown option: -h
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

Git Files

.git : main file managing git stuff

.gitignore : list of files and files types to ignore

.github : git hub automation

Install

sudo apt install git

Configure Git

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Creating New Repo

Option 1: Create repo in Github and clone it

git clone <ssh url>

Option 2: Create a Git repository locally

git init

Optional

git branch -m main

Add and commit files

git add . 
git commit -m "Your commit message"

Set up a remote repository

git remote add <name> <url>

Example:

git remote add origin your_remote_repository_url

Sync

git branch --set-upstream-to=<remote name>/<remote branch> <local branch>

Example

git branch --set-upstream-to=origin/main master

sync

git pull

Push changes to remote

git push

Verify remote connection

git remote -v

Basic Commands

Checking git status

git status

Adding files to git repo

git add <filenames> 

For adding all the files in the folder

git add * 

Commit it to local repo

git commit -m "<message>"

For pushing to github

git push

Git Branches

For viewing Braches

!git branch
* main

Adding branches

git branch <name>

Switching branches

git checkout <branch name>

create branch and move into it

git checkout -b <branch name>

Push branch to github

git push --set-upstream <online branch name> <local branch name>

Delete branch

git branch -d <branch name>

Review History

  • git log: Lists version history for the current branch
  • git log --follow [file]: Lists version history for a file, including renames
  • git diff [first-branch]...[second-branch]: Shows content differences between two branches
  • git show [commit]: Outputs metadata and content changes of the specified commit

Git Forking

Git submodules

A Git submodule is a repository embedded inside another Git repository. The submodule itself is a separate Git repository that is maintained independently.

The main repository (also called the superproject) includes a reference to a specific commit in the submodule repository.

Adding a Submodule

git submodule add <repository-url> <path>

Example

git submodule add https://github.com/example/repo.git external/repo

Cloning a Repository with Submodules

When you clone a repository that contains submodules, the submodules are not cloned by default. You can initialize and update the submodules after cloning

git clone <repository-url>
cd <repository-directory>
git submodule update --init --recursive

Alternatively, you can clone the repository and its submodules in one command:

git clone --recursive <repository-url>

Updating Submodules

git submodule update --remote --merge

Synchronizing Submodules

git submodule sync --recursive

Removing a Submodule

git submodule deinit -f -- <submodule-path>
git rm -f <submodule-path>
rm -rf .git/modules/<submodule-path>
Back to top