DEV Community

Cover image for Essential Git Commands
Nidhi Ojha
Nidhi Ojha

Posted on • Updated on

Essential Git Commands

Git Configuration:

git config --global user.name=“Your Name”
Enter fullscreen mode Exit fullscreen mode

Set the name that will be attached to your commits and tags.

git config --global user.email “you@example.com”
Enter fullscreen mode Exit fullscreen mode

Set the e-mail address that will be attached to your commits and tags.

To view the configuration:

git config --global user.email
git config --global user.name
Enter fullscreen mode Exit fullscreen mode

Image description

Create a project on your computer:

mkdir [projectname]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

cd [projectname]
Enter fullscreen mode Exit fullscreen mode

Image description

to initialize empty Git repository inside your project folder:

git init
Enter fullscreen mode Exit fullscreen mode

Image description
to view files inside your project folder:

ls
Enter fullscreen mode Exit fullscreen mode

It will show list of files.

ls -a
Enter fullscreen mode Exit fullscreen mode

To view hidden files(.extension-file) inside project folder.

Image description

touch (filename)
Enter fullscreen mode Exit fullscreen mode

To create new/empty file.

Image description

Image description

It allows us to see the tracked, untracked files and changes.

git status
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Add a particular file to the staging area.

git add [filename]
Enter fullscreen mode Exit fullscreen mode

Image description

git add .

Enter fullscreen mode Exit fullscreen mode

Image description

Create a new commit from changes added to the staging area.
The commit must have a message!

git commit -m [message]
Enter fullscreen mode Exit fullscreen mode

Image description

It shows the list of commits of current branch.

git log

Enter fullscreen mode Exit fullscreen mode

Image description

To unstage your file/Removing changes from staging area

git restore --staged [filename]

Enter fullscreen mode Exit fullscreen mode

Image description

To remove a commit from the history of a project.

git reset [commit hascode]
Enter fullscreen mode Exit fullscreen mode

Image description

Git has an area called the stash where you can temporarily store a snapshot of your changes without committing to the repository.

git stash
Enter fullscreen mode Exit fullscreen mode

Apply stored stash content into working directory, and clear stash.

git stash pop
Enter fullscreen mode Exit fullscreen mode

Delete a specific stash from all your previous stashes.

git stash drop
Enter fullscreen mode Exit fullscreen mode

Connect remote repository to local repository by using this command

git remote add origin [url]
Enter fullscreen mode Exit fullscreen mode

Image description

pushing local changes to remote repository:

git push origin master/ git push -u origin master 

Enter fullscreen mode Exit fullscreen mode

Image description

A fork is a rough copy of a repository. Generally forking a repository allows us to experiment on the project without affecting the original project
Reason for forking the project
1.propose changes to someone else's project
2.Use an existing project as a starting point.

Cloning the forked project to local
Image description

git clone [url]
Enter fullscreen mode Exit fullscreen mode

Image description

After cloning you will be able to see clone project inside your project folder.
Image description

upstream : upstream generally refers to the original repository that you have forked from other git repositories.

git remote add upstream [url]
Enter fullscreen mode Exit fullscreen mode

Image description

For viewing both url (origin and upstream):

git remote -v
Enter fullscreen mode Exit fullscreen mode

Image description

Create new branch:

git branch [branchname]
Enter fullscreen mode Exit fullscreen mode

Image description

The git checkout command switched branches or restores working tree files. It allows switching between multiple branches in a single repository.

git checkout [branchname]
Enter fullscreen mode Exit fullscreen mode

Image description

It shows the list of branches.

git branch

Enter fullscreen mode Exit fullscreen mode

Image description

Why we create new branch?
=>Because we want to open a new pull request and we can only open a new pull request with a new branch. In simple language one pull request means one branch.
Never commit on main branch and create our pull request first.

Conclusion

That's a wrap! You've made it to the end of the blog!
link mentioned below for other blog about the introduction of the git.
Introduction to Git

Follow me on Twitter for such contents

Oldest comments (1)

Collapse
 
shristy_29 profile image
Shristy

Great 👍