Git documentation

git init

Initializes a new Git repository in the current directory.

git init

Example: In an empty folder, this command will create a Git repository.

git clone

Clones a remote Git repository (e.g., from GitHub) into a local directory.

git clone <repository-url>

Example: git clone https://github.com/user/project.git, this will download the project from the specified URL.

git status

Displays the status of the local repository, including modified, added, or staged files.

git status

Example: After modifying a file, this command will show the state of the changes.

git add

Stages specific files to prepare them for the next commit.

git add <file-name>

Example: git add file.txt, this adds the file file.txt for the next commit.

To add all modified files:

git add .

git commit

Records the changes to the repository with a message describing them.

git commit -m "Descriptive message"

Example:

git commit -m "Added login feature"

git push

Sends the local commits to a remote repository.

git push <remote-name> <branch>

Example:

git push origin main

This will send the local commits to the main branch of the remote repository called origin.

git pull

Fetches and merges changes from a remote repository into the local branch.

git pull <remote-name> <branch>

Example:

git pull origin main

This fetches and merges changes from the main branch of the remote origin into your local branch.

git branch

Lists, creates, or deletes branches.

  • List branches
  • git branch
  • Create a branch:
  • git branch <branch-name>
  • Delete a branch:
  • git branch -d <branch-name>

    git checkout

    Switches between branches or restores files.

  • Switch to an existing branch:
  • git checkout <branch-name>
  • Create and switch to a new branch:
  • git checkout -b <new-branch>

    git merge

    Merges changes from one branch into another.

    git merge <branch-name>

    git log

    Displays the commit history of the repository.

    git log

    Example:

    git log --oneline

    This displays the commit history in a compact format.

    git remote

    Manages remote repositories.

  • List remotes:
  • git remote -v
  • Add a remote repository:
  • git remote add <remote-name> <repository-url>

    Example:

    git remote add origin https://github.com/user/project.git

    git fetch

    Downloads commits from a remote repository without merging them.

    git fetch <remote-name>

    Example:

    git fetch origin

    git reset

    Undoes changes by resetting the staging area or commit history.

  • Unstage files:
  • git reset <file-name>
  • Reset the history to a previous commit:
  • git reset --hard <commit-hash>

    Example:

    git reset --hard abc1234