Git Basics Cheatsheet
Basic git commands cheatsheet that every developer must absolutely know
Init
- This command creates an empty Git repository or reinitializes an existing one (if applicable)
git init
- Set origin
The below command is used to add a new remote (URL for your git repository) :
git remote add origin git@github.com:User/UserRepo.git
The below is used to change the URL of an existing remote repository:
git remote set-url origin git@github.com:User/UserRepo.git
The below command will push your code to the master branch of the remote repository defined with the origin and -u let you point your current local branch to the remote master branch:
git push -u origin master
- Remote
You can check existing remotes with
git remote -v
Pull
- This command is used to pull the latest changes from a given branch, from your remote repo into your local branch.
git pull
git pull origin <branch_name>
- Checkout
git checkout -b <new_branch_name>
git checkout <existing_branch_name>
Branch
- This command is used to list, create, or delete branches in your local and remote repo
git branch
git branch -d <branch_name_to_be_deleted>
git branch -D <remote_branch_name_to_be_deleted>
- Add
git add <file_name>
git add .
- Commit
git commit -m "<commit_msg>"
Push
- This command is used to push the latest changes from a local branch to your remote repo into your specified branch.
git push
git push origin <branch_name>
git push -f