Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 09:48:59 Viewed : 649
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:
git init
: Initializes a new Git repository in the current directory.
git clone <repository-url>
: Creates a copy of a remote repository on your local machine.
git add <file>
: Adds a file to the staging area, preparing it for the next commit.
git add .
or git add --all
: Adds all modified and new files in the current directory and its subdirectories to the staging area.
git commit -m "Commit message"
: Commits the changes in the staging area to the repository with a descriptive message.
git status
: Shows the status of your working directory, including which files are modified, staged, or untracked.
git log
: Displays the commit history, showing the latest commits first.
git branch
: Lists all branches in the repository, with an asterisk indicating the currently active branch.
git checkout <branch-name>
: Switches to the specified branch.
git checkout -b <new-branch-name>
: Creates and switches to a new branch.
git pull
: Fetches and merges changes from the remote repository into the current branch.
git push
: Pushes committed changes from the local repository to the remote repository.
git merge <branch-name>
: Merges the specified branch into the currently active branch.
git remote add <remote-name> <remote-url>
: Adds a remote repository with a given name and URL.
git remote -v
: Lists the remote repositories associated with your local repository.
git fetch <remote-name>
: Fetches changes from a remote repository without automatically merging them.
git diff
: Shows the differences between the working directory and the staging area.
git diff --staged
: Shows the differences between the staged changes and the last commit.
git reset <file>
: Removes a file from the staging area without undoing the changes.
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:
git init
: Initializes a new Git repository in the current directory.
csharpgit init
git clone <repository-url>
: Creates a copy of a remote repository on your local machine.
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.
csharpgit add myfile.txt
git commit -m "Commit message"
: Commits the changes in the staging area to the repository with a descriptive message.
sqlgit commit -m "Added new feature"
git status
: Shows the status of your working directory, including which files are modified, staged, or untracked.
luagit status
git log
: Displays the commit history, showing the latest commits first.
bashgit log
git branch
: Lists all branches in the repository, with an asterisk indicating the currently active branch.
git branch
git checkout <branch-name>
: Switches to the specified branch.
cssgit checkout main
git checkout -b <new-branch-name>
: Creates and switches to a new branch.
cssgit checkout -b feature-branch
git pull
: Fetches and merges changes from the remote repository into the current branch.
cssgit pull origin main
git push
: Pushes committed changes from the local repository to the remote repository.cssgit push origin main
git merge <branch-name>
: Merges the specified branch into the currently active branch.sqlgit merge feature-branch
git remote add <remote-name> <remote-url>
: Adds a remote repository with a given name and URL.csharpgit remote add upstream https://github.com/upstream-user/upstream-repo.git
git remote -v
: Lists the remote repositories associated with your local repository.git remote -v
git fetch <remote-name>
: Fetches changes from a remote repository without automatically merging them.sqlgit fetch upstream
git diff
: Shows the differences between the working directory and the staging area.git diff
git diff --staged
: Shows the differences between the staged changes and the last commit.cssgit diff --staged
git reset <file>
: Removes a file from the staging area without undoing the changes.perlgit reset myfile.txt
git reset --hard
: Discards all changes in the working directory and resets it to the last commit.cssgit 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.