Git is the most popular distributed version control system. Git was developed by Linus torvalds.
I will list down most essential git commands which everyone should master when handling github repositories.
- Set up your reposirory details (username and email).
git config --global user.name "vidush5"
git config --global user.email "[email protected]"
- Cache your login details.
git config --global credential.helper cache
- Initialize a github repository.
This folder contains all the objects and references that git uses and it creates as a part of your projects's history.
git init
- Add single file to staging area.
git add helloworld.py
- Add multiple files to staging area.
git add .
- Check your repository status.
git status
- Commit your changes in your repository.
git commit -m "my first commit"
- To check commit history with changes.
git log -p
- To view the changes before commiting.
git diff
- To see the staged changes.
git diff --staged
- Remove files
git rm helloworld.py
- Rollback last commit you can rollback the last commit and it will create a new commit, an inverse of the previous commit.
git revert HEAD
- Rollback a particular commit.
git revert <commit id>
- Create a new branch.
git branch <branch_name>
- Switch to the branch.
git checkout <branch_name>
- List down all branches.
git branch
list down all remote branches.
git branch -a
- Delete a branch.
git branch -d <branch_name>
To delete forcible a branch.
git branch -D <branch_name>
The above commands only delete the local copy of the branch, if you want to delete a remote branch follow the below command.
git push origin --delete <branch_name>
- Merge two branches.
git merge <branch_name>
Merge with commit.
git merge --no-ff <branch_name>
- Push changes to a remote repository.
git push origin main
- Pull changes from a remote repository.
git pull