Git Commands


Introduction to Git

Git is a distributed version control system renowned for its ability to manage projects of various scales with efficiency and speed. Originally developed by Linus Torvalds for managing the Linux kernel development, Git has become an essential tool in modern software development.

Its distributed nature allows developers to work offline, commit changes locally, and synchronize their work with remote repositories seamlessly. Git tracks changes to files, enabling developers to revert to previous versions, manage different branches for feature development, and collaborate effectively across teams.

Used extensively in both open-source and proprietary projects, Git's popularity stems from its robustness, scalability, and the rich ecosystem of tools and services built around it. Whether you're working on a small personal project or coordinating with a global team on a large-scale application, Git provides the foundation for efficient version control and collaborative software development.

Installing Git

To install Git on your Windows machine, follow these steps:

  1. Download Git from the official Git website: Git for Windows
  2. Run the downloaded installer file and follow the installation instructions.
  3. During installation, you can customize settings as per your preference.
  4. Once installed, open Git Bash or Git GUI to start using Git commands.
Command 1:
git init
Initializes a new Git repository.

Description:
  • git init creates an empty Git repository or reinitializes an existing one.
Output: A new .git directory is created in the current directory.
Command 2:
git clone <repository-url>
Clones a repository into a new directory.

Description:
  • git clone <repository-url> copies an existing Git repository to your local machine.
Output: A new directory is created containing the cloned repository.
Command 3:
git status
Displays the state of the working directory and staging area.

Description:
  • git status shows which changes have been staged, which haven't, and which files aren't being tracked by Git.
Output: A summary of changes to be committed, changes not staged for commit, and untracked files.
Command 4:
git add <file>
Adds changes in the working directory to the staging area.

Description:
  • git add <file> stages the specified file, preparing it for a commit.
Output: The file is added to the staging area.
Command 5:
git commit -m "Commit message"
Records changes to the repository along with a commit message.

Description:
  • git commit -m "Commit message" commits the staged changes to the repository with the specified commit message.
Output: Shows the commit hash and details of the committed changes.
Command 6:
git push origin <branch-name>
Pushes committed changes to a remote repository.

Description:
  • git push origin <branch-name> sends committed changes from your local repository to the remote repository specified by origin.
Output: Updates the remote repository with the local commits.
Command 7:
git pull
Fetches and merges changes from a remote repository to your local branch.

Description:
  • git pull retrieves new work from the remote repository and integrates it with your local branch.
Output: Updates your local repository with the latest changes from the remote.
Command 8:
git branch
Lists all local branches in the current repository.

Description:
  • git branch displays a list of all branches in the local repository, highlighting the current branch.
Output: Lists branches prefixed with an asterisk (*) to denote the current branch.
Command 9:
git checkout <branch-name>
Switches to the specified branch.

Description:
  • git checkout <branch-name> changes the current working branch to the specified branch.
Output: Updates the working directory to match the specified branch.
Command 10:
git merge <branch-name>
Merges the specified branch into the current branch.

Description:
  • git merge <branch-name> integrates changes from the specified branch into the current branch.
Output: Combines the histories of both branches.
Command 11:
git log
Shows the commit history for the current branch.

Description:
  • git log displays a list of commits with details such as hash, author, date, and message.
Output: Commit history log.
Command 12:
git reset --hard <commit>
Resets the working directory and index to the specified commit.

Description:
  • git reset --hard <commit> moves the HEAD to the specified commit and discards all changes in the working directory and index.
Output: Working directory and index are reset to match the specified commit.
Command 13:
git stash
Temporarily saves changes in the working directory.

Description:
  • git stash stores modified tracked files, staging area changes, and working directory changes to a stash.
Output: Changes are stashed and working directory is clean.
Command 14:
git stash pop
Applies the most recent stash and removes it from the stash list.

Description:
  • git stash pop restores the most recently stashed changes to the working directory and removes them from the stash list.
Output: Stashed changes are reapplied and removed from the stash list.
Command 15:
git remote add <name> <url>
Adds a new remote repository.

Description:
  • git remote add <name> <url> creates a new reference to a remote repository.
Output: New remote repository reference is added.
Command 16:
git remote -v
Displays the current configured remote repositories.

Description:
  • git remote -v shows the URLs of the remote repositories for fetch and push operations.
Output: List of remote repositories with their fetch and push URLs.
Command 17:
git tag <tag-name>
Creates a new tag.

Description:
  • git tag <tag-name> creates a new tag at the current commit.
Output: New tag is created at the current commit.
Command 18:
git diff
Shows changes between commits, commit and working tree, etc.

Description:
  • git diff displays the differences between the working directory and the index.
Output: Diff output of changes.
Command 19:
git fetch
Downloads objects and refs from another repository.

Description:
  • git fetch retrieves updates from a remote repository without merging them.
Output: Updates from the remote repository are fetched.
Command 20:
git rebase <base-branch>
Reapplies commits on top of another base tip.

Description:
  • git rebase <base-branch> moves or combines a sequence of commits to a new base commit.
Output: Commits are reapplied on top of the new base tip.

Thank You

Thank you for visiting this website. If you have any questions or would like to get in touch, please feel free to contact me.

Email 9407959924 GitHub     LinkedIn   LinkTree   Portfolio  Devfolio
Visit on GitHub