DEV Community

Cover image for Basic Commands of Git
Shristy
Shristy

Posted on

Basic Commands of Git

Git Congiguration

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.name
git config --global user.email
Enter fullscreen mode Exit fullscreen mode

Image description

Starting a project

Create a project on your computer

 mkdr [project name]
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

 cd [project name]
Enter fullscreen mode Exit fullscreen mode

Image description
To initialize empty git repository inside the project

 git init
Enter fullscreen mode Exit fullscreen mode

Image description

To view hidden files(.extension) inside the project folder

ls -a
Enter fullscreen mode Exit fullscreen mode

To view inside the hidden .git file

ls .git
Enter fullscreen mode Exit fullscreen mode

Image description

To create new empty file

touch [filename]
Enter fullscreen mode Exit fullscreen mode

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
Adding a particular file to staging area

git add [filename]
Enter fullscreen mode Exit fullscreen mode

Image description
Adding all the files to the staging area from current directory down into the directory tree.

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 history of commit of the 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_hashnode
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.

Put current changes in your working directory into stash for later use.

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 features/branches in a single repository.

git checkout [branchname]
Enter fullscreen mode Exit fullscreen mode

Image description

It shows the list of the 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 this blog!
Follow me on twitter for more such contents

Top comments (2)

Collapse
 
ojhanidhi036 profile image
Nidhi Ojha

Congratulations Shristy on your first blog

Collapse
 
akankshaa_25 profile image
Akanksha

Congratulations 👏🏻🥳