Commonly used Git commands

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 09:48:59 Viewed : 292


Commonly used Git commands:

Git is a popular version control system that allows developers to track changes in their code over time. Here are some commonly used Git commands:

  1. git init: Initializes a new Git repository in the current directory.

  2. git clone <repository-url>: Creates a copy of a remote repository on your local machine.

  3. git add <file>: Adds a file to the staging area, preparing it for the next commit.

  4. git add . or git add --all: Adds all modified and new files in the current directory and its subdirectories to the staging area.

  5. git commit -m "Commit message": Commits the changes in the staging area to the repository with a descriptive message.

  6. git status: Shows the status of your working directory, including which files are modified, staged, or untracked.

  7. git log: Displays the commit history, showing the latest commits first.

  8. git branch: Lists all branches in the repository, with an asterisk indicating the currently active branch.

  9. git checkout <branch-name>: Switches to the specified branch.

  10. git checkout -b <new-branch-name>: Creates and switches to a new branch.

  11. git pull: Fetches and merges changes from the remote repository into the current branch.

  12. git push: Pushes committed changes from the local repository to the remote repository.

  13. git merge <branch-name>: Merges the specified branch into the currently active branch.

  14. git remote add <remote-name> <remote-url>: Adds a remote repository with a given name and URL.

  15. git remote -v: Lists the remote repositories associated with your local repository.

  16. git fetch <remote-name>: Fetches changes from a remote repository without automatically merging them.

  17. git diff: Shows the differences between the working directory and the staging area.

  18. git diff --staged: Shows the differences between the staged changes and the last commit.

  19. git reset <file>: Removes a file from the staging area without undoing the changes.

  20. git reset --hard: Discards all changes in the working directory and resets it to the last commit.

These are just some of the many Git commands available. Git is a powerful tool with a wide range of features, so its recommended to explore its documentation and learn more about its capabilities. Additionally, some commands might have variations or additional options, which you can discover through git --help or by referring to the official Git documentation.

Git commands with examples:

  1. git init: Initializes a new Git repository in the current directory.

    csharp
    git init
  2. git clone <repository-url>: Creates a copy of a remote repository on your local machine.

    bash
    git clone https://github.com/exampleuser/my-repo.git
  3. git add <file>: Adds a file to the staging area, preparing it for the next commit.

    csharp
    git add myfile.txt
  4. git commit -m "Commit message": Commits the changes in the staging area to the repository with a descriptive message.

    sql
    git commit -m "Added new feature"
  5. git status: Shows the status of your working directory, including which files are modified, staged, or untracked.

    lua
    git status
  6. git log: Displays the commit history, showing the latest commits first.

    bash
    git log
  7. git branch: Lists all branches in the repository, with an asterisk indicating the currently active branch.

    git branch
  8. git checkout <branch-name>: Switches to the specified branch.

    css
    git checkout main
  9. git checkout -b <new-branch-name>: Creates and switches to a new branch.

    css
    git checkout -b feature-branch
  10. git pull: Fetches and merges changes from the remote repository into the current branch.

css
git pull origin main
  1. git push: Pushes committed changes from the local repository to the remote repository.
css
git push origin main
  1. git merge <branch-name>: Merges the specified branch into the currently active branch.
sql
git merge feature-branch
  1. git remote add <remote-name> <remote-url>: Adds a remote repository with a given name and URL.
csharp
git remote add upstream https://github.com/upstream-user/upstream-repo.git
  1. git remote -v: Lists the remote repositories associated with your local repository.
git remote -v
  1. git fetch <remote-name>: Fetches changes from a remote repository without automatically merging them.
sql
git fetch upstream
  1. git diff: Shows the differences between the working directory and the staging area.
git diff
  1. git diff --staged: Shows the differences between the staged changes and the last commit.
css
git diff --staged
  1. git reset <file>: Removes a file from the staging area without undoing the changes.
perl
git reset myfile.txt
  1. git reset --hard: Discards all changes in the working directory and resets it to the last commit.
css
git reset --hard

These examples cover some of the basic Git commands. Keep in mind that Git has many more advanced features and options. Always refer to the official Git documentation for a comprehensive understanding of each command and their variations.

Search
Sub-Categories
Related Articles

Leave a Comment: