Git Commands Study Notes VCS git

git-init

Create an empty Git repository or reinitialize an existing one:

git init

Git creates a hidden folder:

.git/

The .git/ directory is the heart of a Git repository. It contains all the metadata and the object database required for version control:

  • objects/ The heart of the database. This folder stores all project content as compressed objects (blobs, trees, commits, and tags) named by their SHA-1 hashes.

  • refs/ Contains pointers to specific commits. These are organized into subfolders like heads/ (for local branches), tags/ (for release points), and remotes/ (for tracking remote branches).

  • HEAD A text file that tells Git which branch or commit you are currently working on. It usually points to a reference in refs/heads/.

  • index A binary file representing the staging area. It keeps track of which changes are prepared for the next commit.

  • config Stores repository-specific configuration settings, such as remote URLs and user-specific project preferences.

  • logs/ Contains the reflog, which records the history of movements for references like HEAD and branches. This is useful for recovering lost commits.

  • hooks/ A collection of scripts that Git runs automatically at specific points in the workflow, such as pre-commit or post-merge.

Running git init in an existing repository is safe. It will not overwrite things that are already there.

The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if --separate-git-dir is given).

Some Options

-b / --initial-branch=<branch-name>

Use <branch-name> for the initial branch in the newly created repository. If not specified, fall back to the default name (currently master, but this will change to main when Git 3.0 is released).

Your default branch name

By default Git will create a branch called master when you create a new repository with git init. From Git version 2.28 onwards, you can set a different name for the initial branch.

Warning:
GitHub changed the default branch name from master to main in mid-2020, and other Git hosts followed suit. So you may find that the default branch name in some newly created repositories is main and not master.

To set main as the default branch name do:

git config --global init.defaultBranch main

git-add

Add file contents to the index (staging area), preparing them for commit.

Some Options

-n / --dry-run

Shows what would happen without actually adding (staging) files.


-v / --verbose

Shows detailed output of what files are being added.


-f / --force

Forces Git to add ignored files (like those in .gitignore)


-i / --interactive

Opens interactive mode where you choose what to stage.


-e / --edit

Opens patch in editor so you can manually edit what gets staged.


-N / --intent-to-add

Marks file as to be added later without adding content.


--refresh

Updates Git metadata without staging changes.


--ignore-errors

Continues adding files even if some fail.


--

Separates Some Options from file names. Useful if filename starts with -

git add -- -file.txt

git-clone

Clone a repository into a new directory. Copies a remote repository to your local machine:

git clone https://github.com/user/repo.git
git clone repo.git my-folder

git-status

Show working tree status

git status

Common flags:

  • -s short format output
  • -b show branch info

Explanation:

Shows what files are staged, modified, or untracked.


git-log

Show commit logs

git log

Common flags:

  • --oneline compact view
  • --graph show branch tree visually
  • --all show all branches

Explanation:

Displays history of commits.


git-commit

Record changes to the repository. A commit is a snapshot of your project taken from the staging area (index).

Some Options

-m / --message=<msg>

Use <msg> as the commit message. If multiple -m Some Options are given, their values are concatenated as separate paragraphs.

git commit -m "your message"

-a / --all

Automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.


--dry-run

Do not create a commit, but show a list of paths that are to be committed, paths with local changes that will be left uncommitted and paths that are untracked.


git-pull

Fetch and integrate changes from remote

git pull origin main

Common flags:

  • --rebase apply changes on top of your work instead of merge

Explanation:

Downloads changes and merges them into your branch.


git-push

Upload commits to remote repository

git push origin main

Common flags:

  • -u set upstream tracking branch
  • --force overwrite remote history (dangerous)

Explanation:

Sends your local commits to GitHub or another remote.


git-merge

Join two branches together

git merge feature-branch

Common flags:

  • --no-ff force merge commit
  • --squash combine changes into one commit

Explanation:

Combines changes from another branch into your current branch.


git-show

Show details about commits, tags, or objects

git show HEAD
git show <commit-id>

Common flags:

  • --stat show file change summary
  • --name-only show only file names

Explanation:

Displays full details of a specific commit or object.


git-diff


git-remote