Useful Git Commands


In this article, we will be going through scenarios where we need to push a fresh repository or we need to push some particular files and we will see different scenarios related to it.

Pushing a fresh repository

Create a fresh repository(Any cloud repository).

Open terminal (for mac ) and command (windows) and type the below-mentioned command,

cd <location of your local code >

Initialize git using the below command,

git init 

Add all the files to commit,

git add --all

To link your code to bitbucket use the below command,

git remote add origin <link to repo >

Commit the code mentioning the comment as below,

git commit -m "initial commit to push code to cloud"

Push the code to cloud,

git push -u origin Main

Where main is my repository branch

With this we have successfully pushed our code to the cloud, now we are going to discuss some of the scenarios where we get stuck and it is hard to find the solutions.

By mistake, I have committed the code but I don't want to push it.

Delete the commit keeping the work done,

git reset --soft HEAD~1 

Clean the committed code or delete it,

git reset --hard HEAD~1
Commit all the edited files in 1 go
git commit -a -m ' comments'
-a = all edited files
-m = comment.

Push a particular file,

$ git status
$ git add <file_name>
$ git commit -m "<msg>"
$ git push origin <branch_name>

So in this article, we have gone through pushing up our local repository to the cloud and some of the commands which can help us in different scenarios and in the future articles I will be publishing more commands and the scenarios where we can use them

Happy learning......