Basic Git Commands You Should Know
15 common and basic commands you should know if you are getting started with Git
Hello everyone, welcome back to another article under the Fundamentals Of Git series. Using a terminal/command line to work with Git might be a bit intimidating to most beginner developers since they either do not know or keep forgetting what "commands" to type in to perform the desired actions. This is perfectly alright because I too didn't know what commands to use to perform certain actions and sometimes I have even forgotten a few commands and had to look them up.
There are roughly about 160 Git commands in total, learning all these is a nightmare for almost all developers out there (including myself) and can be a waste of your time even if you did memorize all of them since you'd be working with only about 10 to 15 commands in your day-to-day workflow.
Although you could use a Git client to work yourself around this problem without needing to use a terminal and instead use a GUI to perform these tasks, it would be very beneficial for you to better understand how Git works if you knew what happens in the background for each task you perform through a Git client. Therefore, I have put together a list of 15 commands that you will use and what each command does. So without any further delay, let's dive in!
Note:- Before trying out these commands make sure you have Git installed and configured on your computer. If you don't, I wrote an article on Correctly Installing and Configuring Git.
Git commands when working with a local repository
Here are 8 common commands you will use when working with a local repository.
1. git init
This is the first command/step used in creating a repository. This command initializes an empty repository in your working directory. In other words, it turns the current directory into an empty Git repository for you to start working on.
How to use?
Firstly, move into your working directory.
# cd /path/to/directory cd programming/first-repository
Then type in the command.
git init
You will get an output similar to
Initialized empty Git repository in /programming/first-repository/.git/
.
2. git add
This command is used to add files to the Staging Area(Git Index). Before you commit changes of a file to a repository (i.e. save changes), the files need to be added to the Staging Area.
How to use?
There are three ways in which you can use this command:
- The first is by adding all unstaged files
git add .
- The second is by adding a specific file
git add index.html
- The third is by adding an entire directory
git add directory_name
You can use any of the three ways, but I'd recommend you use the first way,
git add .
to save yourself from getting errors when typing file/directory names.
3. git commit
This command is used to save the changes to a local repository. Each commit has a unique ID for you to refer. It also allows you to add a "message" to each commit, which is considered a best practice when working with Git and to easily find certain changes or understand why a change was made.
How to use?
Following is a commit with a message.
git commit -m "Initial commit inside quotes"
You'll get an output similar to the following:
[master 0254c3d] My first commit message 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 homepage/index.html
4. git status
This command is used to check the current status of the repository.
How to use?
There are three possible outputs you might get when using this command:
First is an
Untracked files
output, which is displayed if your files have not been staged. You'll get an output similar to the following.On branch master Untracked files: (use "git add <file>..." to include in what will be committed) homepage/index.html
To overcome this, simply execute the
git add
command.Second is a
Changes to be committed
output, which is displayed if your files have not been committed. You'll get an output similar to the following.On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: homepage/index.html
To overcome this, execute the
git commit
command.The Third is a
Nothing to commit
output, which is displayed if your files have been staged and committed. You'll get an output similar to the following.On branch master nothing to commit, working directory clean
5. git config
This command is used to configure the settings of Git.
How to use?
Apart from the first time you install git, you won't be using this command much. It's mainly used to configure user settings like; name, email, and editor. If you are configuring Git for the first time, check out this article I wrote on Correctly Installing and Configuring Git.
6. git branch
This command is used to perform tasks related to branching like: determining the current branch, creating a new branch, and deleting a branch.
How to use?
- To list all the branches of the repository, use the following command:
git branch -a
- To create a new branch, use the following command:
git branch branch-name
- To delete a branch, use the following command:
git branch -d branch-name
7. git checkout
This command is used to switch between branches.
How to use?
There are two ways to use the command:
Switch to an existing branch:
git checkout branch-name
Create a new branch and switch to it.
git checkout -b new-branch
8. git merge
This command is used to integrate/combine changes from one branch to another. For instance, when merging changes made in the "development" branch into the "production" branch.
How to use?
Merging changes to the current branch from another branch:
git merge second-branch-name
Git commands when working with a remote repository
Here are 4 common commands you will use when working with a remote repository.
1. git remote
This command is used to connect a local repository to a remote one. Usually, you set a name to the remote repository to avoid typing or remembering the entire URL of the repo, but you can ignore it if you don't need it.
How to use?
The syntax to use this command is as follows:
git remote <command> <remote_repository_name> <remote_repository_URL>
# or if you don't want to set a name:
git remote <command> <remote_repository_URL>
And you can use the following command to list all named repositories:
git remote -v
Note:- In place of the
<command>
in the above example, you can use the following:
- add
- remove
- rename
- set-head
- set-url
- set-url --add or --delete
- set branches
- get url
- prune
2. git clone
This command is used to create a local copy of an existing remote repository.
How to use?
Use this command along with the URL of the remote repository:
git clone https://urltoremoterepository.com
3. git pull
This command is used to get the latest changes made to a remote repository.
How to use?
Use this command with the branch name and URL of the remote repository:
git pull branch-name https://urltoremoterepository.com
4. git push
This command is used to send the latest changes made locally to a remote repository.
How to use?
Use this command similar to the git pull
command with the branch name and URL of the remote repository:
git push branch-name https://urltoremoterepository.com
Other Common Git Commands
Here are some more commands commonly used when working with Git.
git stash
This command is used to store changes when the changes are not in a state to commit to a repository.
How to use
There are two common ways to use this command:
- Store the current changes without committing:
git stash -u
- Bring the stashed(uncommitted saved changes) work back to the working directory:
git stash pop
git log
This command is used to show the commit history of a repository in chronological order.
How to use
- To show the entire commit history of a repo:
git log
To show the commit history of a repo by date:
git log --<command>=<date>
In place of the
<command>
above, you can use the following:- after
- before
- since
- until
And in place of the
<date>
, you can specify the date within quotes.Example:
git log --after="July 21"
To show the commit history of a repo based on the author of the commit:
git log --author="Author Name"
git rm
This command is used to remove files/directories from the staging area.
How to use
There are two ways to use this command:
- Remove files/directories only from the staging area (Cached).
- Remove a file:
git rm --cached file_name
- Remove a directory:
git rm -r --cached directory_name
- Remove a file:
- Completely delete files/directories (Forced).
- Remove a file:
git rm -f file_name
- Remove a directory:
git rm -r -f directory_name
- Remove a file:
Conclusion
Thanks for reading🎉. If you find this article to be useful, consider liking and sharing it with others. Keep this article bookmarked for you to reference in the future 😉. This is the third article in the Fundamentals of Git series and hopefully, there will be more articles in the future that explains a lot more about Git, its workflow, its commands, etc. So stay tuned for that too.
And as always, if you have any questions or feedback let me know in the comments down below and follow me on Twitter for more updates on my journey, tips and tricks about programming. Ciao 👋