DEV Community

Ritik Patel
Ritik Patel

Posted on

Most used git commands

1. git remote

To start working on a repository, you need to either clone the repository or add remote repository. Cloning automatically adds 'origin' as your remote.

git remote add <remote_name> <url>

Example: Consider I want to start working on react repository,
I would type the following command:

git remote add ritik https://github.com/facebook/react

To check whether your remote repository is added or not,
You can use:

git remote -v

2. git checkout

After setting up the remote, you would like to start working on the issue #70. So, you should make a branch for the same.

So, to create a new branch and switch to it at the same time,

git checkout -b <branch_name>

Example,
git checkout -b issue#70-fix-button-styling

Note:
git checkout -b
is shorthand for

git branch issue#70-fix-button-styling
git checkout issue#70-fix-button-styling

3. git pull

While working on a branch, it might be the case that one of the collaborators has made changes to your branch, or there are some new changes in the master branch that you want your branch to have. You can do by,

git pull <remote_name> <branch_name>

Example,
git pull ritik issue#70-fix-button-styling

4. git status

The 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. Status output does not show you any information regarding the committed project history.

git status

The output will look like this:

# On branch issue#70-fix-button-styling
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#modified: hello.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: index.html
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#index.css
Enter fullscreen mode Exit fullscreen mode

5. git add

After making a change, if you want to add an untracked file to the staging area for next commit. git add lets you do that.

Lets say you made changes to index.css file and want to add this file for commit. First we need to add it from untracked files to the tracked files.

git add <filePath1> <filePath2> <filePath3>

(Space separated filenames to add each file to staging area)

example,
git add index.css

6. git commit

To commit all the files that are in staging area, i.e, all the tracked files.

git commit -m <commit_message>

Example,

git commit -m "style: fixed button colors"

7. git push

Our repository is still local, only we can view it, make changes to it. To make it public and to push all the commits, on our first push we would write:

git push -u <remote_name> <branch_name>

Example,
git push -u ritik issue#70-fix-button-styling

After our first push, if we make more commits and to push them on to the remote repository. We can use:

git push

Note: Git push only uploads changes that are committed.

Top comments (0)