LinuxCommandLibrary

git

Manage code versions and collaborate

TLDR

Create an empty Git repository

$ git init
copy

Clone a remote Git repository from the internet
$ git clone [https://example.com/repo.git]
copy

View the status of the local repository
$ git status
copy

Stage all changes for a commit
$ git add [[-A|--all]]
copy

Commit changes to version history
$ git commit [[-m|--message]] [message_text]
copy

Push local commits to a remote repository
$ git push
copy

Pull any changes made to a remote
$ git pull
copy

Reset everything the way it was in the latest commit
$ git reset --hard; git clean [[-f|--force]]
copy

SYNOPSIS

git [--version] [--help] [--git-dir=<path>]
[--work-tree=<path>] [--namespace=<name>]
[-p|--paginate] [-P|--no-pager] [--bare]
<I>command</I> [<I>args</I>]

This synopsis shows the general invocation of Git. Most Git operations involve a specific command (e.g., clone, add, commit) followed by its own set of arguments and options.

PARAMETERS

command
    The specific Git operation to perform (e.g., add, commit, push). Git has hundreds of subcommands, each with its own set of options.

--version
    Displays the Git version information.

--help
    Displays help information for Git or a specific subcommand (e.g., git help commit).

--git-dir=<path>
    Sets the path to the repository (.git) directory.

--work-tree=<path>
    Sets the path to the working tree directory.

--no-pager
    Disables the pager for Git output, showing everything directly to the console.

--bare
    Operates on a bare repository, which has no working tree.

init
    Creates a new empty Git repository or reinitializes an existing one.

clone <repository> [<directory>]
    Clones a repository into a new directory.

add [<file>...]
    Adds file contents to the index (staging area).

status
    Shows the working tree status.

commit [-m <message>]
    Records changes to the repository.

branch [<name>]
    Lists, creates, or deletes branches.

checkout <branch|commit|file>
    Switches branches or restores working tree files.

merge <branch>
    Joins two or more development histories together.

pull
    Fetches from and integrates with another repository or a local branch.

push
    Updates remote refs along with associated objects.

log
    Shows commit logs.

remote
    Manages set of tracked repositories.

config
    Gets and sets repository or global options.

DESCRIPTION

Git is a free and open-source distributed version control system (DVCS) designed to handle everything from small to very large projects with speed and efficiency. It tracks changes in source code during software development, enabling multiple developers to work together on the same project without stepping on each other's toes. Git provides robust features for branching, merging, and reverting to previous states, making it an indispensable tool for collaborative programming and maintaining a comprehensive history of a project's evolution. Unlike older centralized systems, Git operates on a distributed model where every developer's working copy of the code is a full-fledged repository with complete history and version tracking capabilities, allowing for offline work and resilient operations.

CAVEATS

Complexity: While powerful, Git's vast array of commands and options can be overwhelming for new users. Understanding its underlying concepts (e.g., index/staging area, detached HEAD) is crucial.
Merge Conflicts: Resolving merge conflicts can be challenging, especially in large projects with frequent concurrent changes.
Large Files: Git is not ideal for managing very large binary files directly due to its content-addressable nature, which stores every version of every file. Git Large File Storage (LFS) is often used as a workaround.

CONFIGURATION

Git uses configuration files to customize its behavior, which can be set at system-wide, global (user-specific), or local (repository-specific) levels using the git config command. This includes setting user identity, aliases, default editor, and more.

BRANCHING STRATEGY

Git's strength lies in its lightweight branching model, enabling developers to create isolated environments for new features or bug fixes. Common branching strategies like Gitflow or GitHub Flow provide structured workflows for team collaboration and release management.

HISTORY

Git was created by Linus Torvalds in 2005 for development of the Linux kernel, after the project's previous distributed version control system, BitKeeper, revoked its free usage. Torvalds sought a distributed system that was fast, had strong support for non-linear development (thousands of parallel branches), and was completely free and open source. Within months of its initial release, Git became the primary tool for Linux kernel development, and it has since grown to be the most widely used version control system in the world, adopted by millions of developers and organizations.

SEE ALSO

svn(1), hg(1), diff(1), patch(1)

Copied to clipboard