Basic Git commands


Pre

You should set your global user name and email before going through next operations


With no correct user name and user email will result your commit with no

Init repo

When in the working directory do this

git init

Add files to stage

While in your working directory using below command to add files to stage.

git add . 

Check git status

After add files to git stage, we have to make sure we successfully add files to stage.

git status 

Git commit

If the files are in git stage level, then it is time to commit them (but not yet push to your cloud repository)

git commit -m "first commit"

With -m flag, to give your commit a comment states what this commit is about.

Git push

Warning

Before push your files to cloud repository, you have to check your branch name.

If you haven’t set the global branch name as main, use command below:

git branch -M main

The git’s default branch name will be master which is abandoned by Github.

Set remote origin

Tips

You have to get your private key ready for commit files to Github.

Now after you committed your files and set the correct branch name, before you can push your files to cloud repository. You have to set the origin address to which cloud repository you want to push,

git remote add origin <your cloud repository address>

Final step: git push

The final step is to push your staged files to cloud repository

git push 

Change exist remote url

If you want to change your local repository remote url. It could be done by this following command.

$ git remote -v //check for remote urls first
$ git remote set-url origin <your new repository url>

Reset any modified files to previous commit

If you want to reset modified file back to previous commit.

Below command will achieve this.

$ git reset --hard <previous commit>

Author: Ashless
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Ashless !
  TOC