Git Commands
Creating a Repository
Initialize a new Git repository:
git init
Clone an existing Git repository:
git clone <repository_URL>
Staging Changes
Add changes to the staging area:
git add <file_name>
Add all changes to the staging area:
git add .
Committing Changes
Commit changes with a message:
git commit -m "Commit message"
Reset to a specific commit
git reset <commit hash>
Revert to a previous commit
git revert <commit hash>
Branching
Create a new branch:
git branch <branch name>
Switch to an existing branch:
git checkout <branch name> git switch <branch name>
Showing all remote branch
git branch -a
Create and switch to a new branch:
git checkout -b <branch name>
Merge a branch into the current branch:
git merge <branch name>
Undo local changes
git checkout -- <file_name>
Delete a branch
git branch -d <branch name>
Remote Repositories
Add a remote repository:
git remote add <remote name> <remote URL>
Push changes to a remote repository:
git push origin <branch name>
Pull changes from a remote repository:
git pull origin <branch name>
Show remote origin URL
git remote -v
Add remote origin URL
git remote add origin <your/remote/git/URL>
Remove remote origin URL
git remote remove origin
Fetch all the remote branches
git fetch
Rewrite History
Rebase apply any commits of the current branch ahead of the specified one
git rebase <branch_name>
Reset clear staging area, rewrite the working tree from the specified commit
git reset --soft <commit>
Temporary Commits
Save modified and staged changes
git stash
List stack order of stashed file changes
git stash list
Write working from the top of the stash stack
git stash pop
Discard the changes from the top of the stash stack
git stash drop
Miscellaneous
Check the status of your Git repository:
git status
Set global username and email for Git (Locally)
git config --global user.name "<your username>" git config --global user.email "<your email>"
Restore the file from being modified to being Tracked
git restore <filename> git checkout <filename>
View the commit history of your Git repository:
git log
View the difference between the two commits:
git diff <commit 1> <commit 2>
GitHub Commands
Creating a Repository
Create a new repository on GitHub and copy the URL.
Clone the repository to your local machine:
git clone <repository_URL>
Collaborating with Others
Add a collaborator to the repository:
Settings -> Manage access -> Invite a collaborator
Accept a collaborator invitation:
Notifications -> Invitations
Fork a repository:
Click the "Fork" button on the repository's GitHub page
Create a pull request:
Click the "New pull request" button on the repository's GitHub page
Review and merge a pull request:
Click the "Merge pull request" button on the repository's GitHub page
Thank You,
Abhisek Moharana