Basic Git Commands All Software Developers Should Know
Basic commands to use
Git is a distributed, version control tool used by software developers to track and manages changes made to source code. Git is an integral part of software development, and anyone looking to get into the field should atleast know the basic commands in git.
Table of Contents
- Setting up a repository
- Saving changes to the repository
- Repo to repo collaboration
- Configuration and set up
- Inspecting a repository
- Ignoring files
Setting up a repository
• Initializing a new Git repo
git init command is used to initialize a new git project. Once this is done, every file changes within that directory will be tracked and monitored by git. There are two ways you can use this command
cd /path/to/your/existing/code git init
with the above commands, you simply point/navigate to an existing, unversioned project and then transform it into a git repository by running the
git init command.
git init <project directory>
this command simply creates a new empty folder and then transforms it into a git project as well.
• Cloning an existing Git repo
Say you have a repo in a public repository like GitHub, you can copy that repo into your local machine with the
git clone command.
git clone <repo url>
This command does two things: First, it initializes a git project in your local machine. Then, it copies the existing public repo into your git repo in your local machine. You pass git clone a repository URL. Git supports a few different network protocols and corresponding URL formats.
Saving changes to the repository
- git add
- git commit
cd /path/to/project echo "test content for git tutorial" >> CommitTest.txt git add CommitTest.txt git commit -m "added CommitTest.txt to the repo"
First, you navigate to your git repo. Then you run the
git addc command. This command simply adds the changes you make to the staging area
After that, you run the git commit command with a message describing what work was done in that commit. A commit is synonymous to “saving changes”.
- git remote add
- git push
git push -u <remote_name> <local_branch_name>
git push command is used to “push” code from your local repository to a public “bare”repository. Before doing that, you’ll usually want to add the remote repo you want to push to, by it’s URL. You do this with the following command:
git remote add <remote_name> <remote_repo_url>
This command will push the local repo branch under
< local_branch_name > to the remote repo at
< remote_name >
Configuration & set up
- git config
This command sets the name of the user which will be used for all commits made by that user. This is done globally, i.e across all projects
git config --global user.name <name>
This command sets the email of the user which will be used for all commits made by that user. This is done locally, i.e on just that project.
git config --local user.email <email>
This command is used to set aliases for some of the most popular commands so you don’t have to type out the original command. Instead, you run the command by its alias.
git config --global alias.<alias-name> <git-command>
Define the text editor used by commands like git commit for all users on the current machine. The
git config --system core.editor <editor>
< editor >argument should be the command that launches the desired editor (e.g., Notepad).
Inspecting a repository
- git status
git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
Running git status returns the state of your git repo, and the message will typically look like this:
git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # #modified: helloworld.js # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # #modified: main.js # # Untracked files: # (use "git add <file>..." to include in what will be committed) # #hello.pyc
Some related commands are
git tag and
git log. Since tags are references that points to specific points in your project’s git history,
git tag is used to capture them.
git log allows you list the project history, filter it, and search for specific changes.
Sometimes, you may not want git to track specific files in your working directory. You use the
.gitignore file to do this:
This instructs git not to track all (as specified with asterisk) files in the project folder which ends with a
This is by no means an exhaustive list. If you’re looking for an in-depth tutorial on git, I will recommend checking out this git lesson by atlassian.
Make sure to follow this blog for future posts.
Thanks for reading.