DEV Community

Umesh Kumar Dhakar
Umesh Kumar Dhakar

Posted on

Basic commands to start Git and GitHub

Hello Folks, Git and GitHub are very useful tools for people like us.
It's a good habit to make notes of anything we are learning and there are many online tools which make note taking easier. and simplenote are some clean and simple tools for note-taking. Happily, I have also developed this good habit and created a document while learning Git.
Some good posts motivated me to pull out those documents and write them here also, which might help someone here and definitely me also.
So here are the basics commands to start Git and host source code on GitHub(remote repository)

Install git.
sudo apt-get install git

Create a git repository locally in a directory.
git init

Set your globle user details.
git config --global <name>
git config --global <email>

Create a README file to store the description of the repository.

Create a file to ignore some files and directories from the repository.
(write the relative path of files and directory, wild cards can also be used here)
touch .gitignore

Create project's files or paste the existing files in current directory.

Add changed files for committing.
git add <filename>
or add all files at once
git add .

Check the status of the repository.
git status

It's time to commit the first changes locally.
git commit -m "initial commit" <file>

Everything we did is stored locally, Now we will link the local repository to a remote repository.

Create a fresh repository on GitHub (now we can create unlimited private repos on GitHub also, Thanks GitHub for this new year gift 😊).
Click that green button 'Clone or download' and copy the URL of the repository.

Link the remote repository with the local repository.
git remote add origin <repo_url>

Push the changes to the master branch of remote repository.
git push -u origin master

One extra command. 💡
If you accidentally commited changes or want to undo the commit these commands can save you time.
git reset --hard HEAD^
git push -f

Thank you for reading 😊.
There are much more commands out there. These are only the basics which can help in getting started. Please let me know if have some doubts, I am happy to help you out.

Top comments (4)

thecodepixi profile image
Emmy | Pixi

Thanks for writing this up! I spent yesterday learning git and github and this is such a nice cheatsheet to have handy. Much appreciated 👍🏻

umeshdhakar profile image
Umesh Kumar Dhakar

You're Welcome.
My mission is accomplished.😊

umeshdhakar profile image
Umesh Kumar Dhakar

Welcome Rene.
I am happy that you found it helpful 😊