Useful Git commands.
Lets discuss some of the most common and useful Git commands.
Git is a version control system that lets us manage our source code. GitHub is a cloud-based hosting service that lets us manage Git repositories.
Hey readers if you want to know more about GitHub. Checkout my blog :[imsushant.hashnode.dev/github]
Below is a list of some of the most frequently used commands of Git (git bash):
To initiate a folder for a new or existing project :
git init
To add a file to the staging area :
git add <file_name>
To add all the files to the staged area :
git add .
orgit add -A
To check status of the files :
git status
To commit changes to the head repository (local) :
git commit -m "commit message"
To view all the commits made :
git log
To view desired number of commits :
git log -p -10
---> Note : Here we have entered 10 but it can be your desired number.To remove a file from the staging area :
git rm --cached <file_name>
to create a new branch :
git branch <branch_name>
To create a branch and move to that branch :
git checkout -b <branch_name>
To switch between branches :
git checkout <branch_name>
To know all the branches :
git branch
To add a file in staged area as well as commit :
git -am "commit message"
orgit -a -m "commit message"
To merge a branch into the current branch :
git merge <branch_name>
To go to the last commit made :
git checkout <file_name>
orgit checkout -- <file_name>
To go to the last commit of all the files:
git checkout -f
To compare current file with the staged version of that file :
git diff
To compare staged file with the last commit version of that file :
git diff --staged
To remove a file from both staging area as well as working folder/directory :
git rm <file_name>
To check short status/ overview of a file :
git status -s
To delete a git repository :
rm -rf .git
To rename a file as well as stage the file :
git mv <file_name> <new_file_name>
To know all the commits in one line :
git log --pretty=oneline
To know all the commits in short/ comprised form :
git log --pretty=short
To know all the commits in long form :
git log --pretty=long
To know all the commits for a desired time period :
git log --since=number.desired-time-period
---> Example : git log --since=2.weeksTo un-stage a file :
git restore --staged <file_name>
To set a new name (alias) for a command :
git config --global alias.<new_name> <original_name>
---> Example : git config --global alias.ct commitTo discard all the changes made n a file :
git restore <file_name>
To connect local and remote repository (example - connect git with GitHub) :
git remote add origin <url of the remote repository>
---> Note : origin is now the alias name for the remote repository link. We can mention any desired name in the place of origin.To know the remote repositories :
git remote
To push master branch to the remote repository (origin) :
git push -u origin master
To change link (url) of the origin (remote repository) :
git remote set-url origin <new_link>
To clone a repository from GitHub :
git clone <repository clone url>
To pull a branch from remote repository :
git pull
To push files to the master branch (remote repository) directly:
git push
To push a branch to the GitHub with alias name :
git push <branch>:<new_branch_name>
---> Example : We have a branch name feature in our local repository and we want to push that branch with name div1 → command : git push feature:divTo delete a merged branch from local repository :
git branch -d <branch_name>
To delete a branch which in un-merged from local repository :
git branch -D <branch_name>
To delete a branch from remote repository :
git push -d origin <branch_name>