GIT GUIDE

Initialize git proyect

Command line (cmd)

> git init


> git add .


> git commit -m "your commit message"


> git remote add origin https://github.com/user_name/project_name.git


> git branch -M main


> git push -u origin main


Add changes

Command line (cmd)

> git add <filename.extension>


or


Command line (cmd)

> git add .


Remove changes

Command line (cmd)

> git rm <filename.extension>


or


Command line (cmd)

> git rm .


Commit changes

Command line (cmd)

> git commit -m "your commit message"


Add and Commit changes in one step

Command line (cmd)

> git commit -am "your commit message"


Push to project

Command line (cmd)

> git push -u origin <branch-name>


Check status

Command line (cmd)

> git status


Check branch

Command line (cmd)

> git branch


or


Command line (cmd)

> git branch -a


Create new branch

Command line (cmd)

> git branch <new-branch-name>


Delete branch

Command line (cmd)

> git branch -d <branch-name>


or make this for force delete


Command line (cmd)

> git branch -D <branch-name>


Delete branch remotely

Command line (cmd)

> git push <remote> --delete <branch-to-remove>



Example:



> git push origin --delete test

or use shorter command


Command line (cmd)

> git push <remote> :<branch-to-remove>



Example:



> git push origin :test

Switch to branch

Command line (cmd)

> git switch <branch-name>


Create and switch branch in one step

Command line (cmd)

> git checkout -b <new-branch-name>


Fetch branch

Command line (cmd)

> git fetch origin <branch-to-fetch>


Merge branch

Command line (cmd)

> git merge origin <branch-to-merge>


Pull branch, (fetch and merge in one step)

Command line (cmd)

> git pull origin <branch-to-pull>


Show commits

Command line (cmd)

> git log


or


Command line (cmd)

> git log --graph


or


Command line (cmd)

> git log --oneline


or


Command line (cmd)

> git log --graph --oneline


or


Command line (cmd)

> git log --graph --pretty=oneline|short|medium|full|fuller|reference|email|mboxrd|raw


Discard changes

Command line (cmd)

> git restore <filename.extension>


Discard stages

Command line (cmd)

> git restore --staged <filename.extension>


Show remote repository

Command line (cmd)

> git remote -v


Set/Change remote repository

Command line (cmd)

> git remote set-url origin <url-to-repository>



Example:



> git remote set-url origin https://github.com/ToniCoffee/test.git


Prune remote repository

Command line (cmd)

> git remote prune origin


"Note": This command remove all remotes lost references

Other commands

Command line (cmd)

> git show