CLI Commands

Basic

  • git --version

    • Shows the git version.

  • git \_generic_command -h

    • Shows the help/tutorials for the given command; e.g.: git clean -h.

  • git show \_COMMIT_ID (or) \_HEAD~\_number_of_commits_to_go_back

    • Shows the given commit.

  • git status

    • Gives information about commits and the staging area.

    • git status -s

      • Gives the information in a shortened form; short format.

Setup

  • git init

    • Initializes a git repository. If the directory doesn't have a .git  folder, it is created with files inside.

  • git remote add origin https://github.com/YOUR_USER/YOUR_REPO.git

    • Add the remote repository to your local project.

  • git remote -v

    • Check if the remote repository was added correctly.

Clone

  • git clone
    git clone https://github.com/username/repo.git
    git clone https://github.com/username/repo.git my-custom-folder
    git clone git@github.com:username/repo.git

  • git ls-a

    • Shows all git files.

  • git ls-tree \_COMMIT_ID (or) \_HEAD~\_number_of_commits_to_go_back

    • Shows the content in a 'tree' format of the given commit; if no commit is given, the current one is used.

  • git add \_file_name (or) \_abbreviations_for_multiple_files

    • Adds the changes of the file(s) to the staging area.

  • git rm \_file_name

    • Removes the file from the local directory and the staging area.

  • git commit -m "\_title"

    • git commit -m "\_title" -m "\_description"

    • Commits the changes that were in the staging area. The -m option is used to set the "_title" message for the commit.

    • Adds an extra description in addition to the initial "_title".

Restoring/Undo/Revert

  • revert

    • Creates a new commit that reverses the changes of the previous commit.

    • It's pretty safe.

  • git reset

    • Moves HEAD "back", basically doing an UNDO of everything new that was done.

    • (2024-10-19) I used git reset . It worked nicely. The commit is undone and the commit's changes are placed in the Changes tab to be committed again or discarded.

    • Types of 'reset':

      • git reset  == git reset HEAD

      • git reset HEAD^

        • HEAD^  means "the parent commit" of the commit HEAD is pointing to (the commit before the most recent).

    • .

  • restore

    • Restores the file to a previous version.

    • There are other options with git restore -h .

    • git restore \=\=source\=\_COMMIT_ID (or) \_HEAD~\_number_of_commits_to_go_back \_file_name

  • amend

    • Combines the previous commit with the staged modifications.

    • Usually used only to change the commit message/description.

    • I tried to use this when there were file changes, and it resulted in creating a new branch, with a merge back to the current branch with the changes. It was kind of weird and not "clean".

  • cherry-pick

    • Used to move individual commits between branches, commonly used to fix the mistake of committing to the wrong branch.

    • Explanation and demo .

  • reflog

    • Reverts a commit or branch that was deleted, by going back in time.

    • reflog .

Logging

  • Scroll:

    • Space  → next page

    • b  → previous page

    • Enter  → next line

    • y  → previous line

  • Search:

    • /pattern  → search forward

    • ?pattern  → search backward

    • n  → repeat search forward

    • N  → repeat search backward

  • Go to start/end:

    • g  → start of file

    • G  → end of file

  • Exit:

    • q  → quit less  and return to shell

Git Log
  • git log

    • Ex:

    • git log --follow -p -m "--pretty=format:COMMIT: %ad HASH: %h" --word-diff -U0 --date=short  --  "path.md"

  • Shows the change logs.

  • git log --oneline

    • Shows the logs in a compact form.

  • Filter options can be by 'date', 'message', 'author', 'file' or 'branch'.

  • --follow

    • Continue listing the history of a file beyond renames (works only for a single file).

  • --pretty-format

    • %H

      • commit hash

    • %h

      • abbreviated commit hash

    • %T

      • tree hash

    • %t

      • abbreviated tree hash

    • %P

      • parent hashes

    • %p

      • abbreviated parent hashes

    • %an

      • author name

    • %aN

    • %ae

      • author email

    • %aE

    • %al

      • author email local-part (the part before the @  sign)

    • %aL

    • %ad

      • author date (format respects --date= option)

    • %aD

      • author date, RFC2822 style

    • %ar

      • author date, relative

    • %at

      • author date, UNIX timestamp

    • %ai

      • author date, ISO 8601-like format

    • %aI

      • author date, strict ISO 8601 format

    • %as

      • author date, short format ( YYYY-MM-DD )

    • %ah

      • author date, human style (like the --date=human  option of git-rev-list )

    • %cn

      • committer name

    • %cN

    • %ce

      • committer email

    • %cE

    • %cl

      • committer email local-part (the part before the @  sign)

    • %cL

    • %cd

      • committer date (format respects --date= option)

    • %cD

      • committer date, RFC2822 style

    • %cr

      • committer date, relative

    • %ct

      • committer date, UNIX timestamp

    • %ci

      • committer date, ISO 8601-like format

    • %cI

      • committer date, strict ISO 8601 format

    • %cs

      • committer date, short format ( YYYY-MM-DD )

    • %ch

      • committer date, human style (like the --date=human  option of git-rev-list )

    • %d

      • ref names, like the --decorate option of git-log

    • %D

      • ref names without the " (", ")" wrapping.

  • git blame

    • For line-level attributions.

Submodules

  • Based on having a git repository inside another git repository.

  • The submodule files are in the local directory, but are not part of the main repository, therefore not part of the main repo's version control.

  • SubModules .

    • The video explains how to fetch submodules of an existing repository when cloning, since they are not downloaded automatically:

      • Run git clone --recursive-submodules _repo_url_  when cloning.

      • Or run git submodule update --init --recursive  after cloning.

  • Removing submodules .