Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 07:02:15 Viewed : 760
Here are some Git commands with examples to demonstrate their usage:
git init
: Initializes a new Git repository in the current directory.
Example:
bashgit init
git clone <repository-url>
: Creates a copy of a remote repository on your local machine.
Example:
bashgit clone https://github.com/exampleuser/my-repo.git
git add <file>
: Adds a file to the staging area, preparing it for the next commit.
Example:
bashgit add index.html
git commit -m "Commit message"
: Commits the changes in the staging area to the repository with a descriptive message.
Example:
bashgit commit -m "Added a new feature"
git status
: Shows the status of your working directory, including which files are modified, staged, or untracked.
Example:
bashgit status
git log
: Displays the commit history, showing the latest commits first.
Example:
bashgit log
git branch
: Lists all branches in the repository, with an asterisk indicating the currently active branch.
Example:
bashgit branch
git checkout <branch-name>
: Switches to the specified branch.
Example:
bashgit checkout main
git checkout -b <new-branch-name>
: Creates and switches to a new branch.
Example:
bashgit checkout -b feature-branch
git pull
: Fetches and merges changes from the remote repository into the current branch.
Example:
bashgit pull origin main
git push
: Pushes committed changes from the local repository to the remote repository.
Example:
bashgit push origin feature-branch
git merge <branch-name>
: Merges the specified branch into the currently active branch.
Example:
bashgit merge feature-branch
git remote add <remote-name> <remote-url>
: Adds a remote repository with a given name and URL.
Example:
bashgit remote add origin https://github.com/exampleuser/my-repo.git
git remote -v
: Lists the remote repositories associated with your local repository.
Example:
bashgit remote -v
git fetch <remote-name>
: Fetches changes from a remote repository without automatically merging them.
Example:
bashgit fetch origin
git diff
: Shows the differences between the working directory and the staging area.
Example:
bashgit diff
git diff --staged
: Shows the differences between the staged changes and the last commit.
Example:
bashgit diff --staged
git reset <file>
: Removes a file from the staging area without undoing the changes.
Example:
bashgit reset index.html
git reset --hard
: Discards all changes in the working directory and resets it to the last commit.
Example:
bashgit reset --hard
These examples cover some common scenarios in Git, but there are many more commands and options available. Explore Git further and make use of its powerful features for version control and collaboration.