Basic Git Commands
Git is a super-powerful version control system that allows its user or developers to track changes in written code and do a collaboration with others on software projects. Whether you are a beginner or an experienced developer, understanding the basics of Git commands is essential for using Git effectively. In this article, we will explore six fundamental Git commands that you need to know to get started with version control.
By the end of this article, you will have a good understanding of how to use these Git commands to manage your code changes and collaborate with other developers. So let’s dive into the world of Git and explore these basic Git commands in detail.
Index
- git clone
- git init
- git add
- git commit
- git push
- git remote
1. git clone
If you want a local copy of a repository from GitHub, this command allows creating a local copy of that repository on your local directory from the
repository URL.
git clone remote-repository-url
You can find the remote repository URL by clicking on the code button with green background in any git repository, as shown in the example below.
2. git init
the git init command will create a new local repository.
git init
Apart from it, you can create a repository within any folder by specifying the project name:
git init MyProject
3. git add
git add is used to add files to the staging area and for removing files from the staging area you can use the same command.
git add FileName
To add all the files of the local repo.
git add -A
Another command for the same is,
git add .
Note → The command git add .
only stages files in the current folder and not any sub-folders, however, git add -A
will stage all available files in sub-folders too.
To add only specific files and folders.
git add FolderName/FileName
4. git commit
git commit will create a snapshot of the changes and save it to the git directory.
git commit -m "Any Message Here"
The message in the commit command is nothing but a text that tells about what is changed in files.
Example → git commit -m "fix: user profile is not showing on the about us page"
5. git push
git push command is used to push or send local commits to any branch of the remote git repository. Here’s the basic code structure
git push origin BranchName
Replace BranchName with your branch name in which you want to push the changes.
Example → git push origin feature-contact-us-page
6. git remote
git remote lets you view all remote repositories also used to connect your local repository to the remote server and List all connections along with their URLs.
To list the remote connections
git remote
List the remote connections with URL :
git remote -v
To connect the local repository to a remote server.
git remote add origin RepositoryURL
To remove a connection of a folder or file to a particular remote repository
git remote rm RepositoryName