Basic Git Commands We Should Know
Initial Git Setup
Create an empty git repo or re-initialize an existing one
git init
Clone a repo
Clone the abc repo into a new directory called def:
git clone https://github.com/<username>/abc.git def
If we want to clone as it is then:-
git clone https://github.com/<username>/abc.git -> Here it will create abc directory
Git Branch
Create a New Branch in Git
There are two ways
- Create a branch from the bitbucket website and fetch to local, then use the following command:
git fetch origin
2. By Terminal/Git Bash, the following command will use:
git checkout -b <new_branch_name>
Example:- git checkout -b devuser//where devuser is the branch name
List Branches in Git
If you want to know what branches are available in your current working directory, then use the following command:
git branchOutput:-
master
* vikasHere "*" represents my current branch where i'm /we're working right now.
But if you want to know all the branches in that repo (repository), then use:-
git branch -aOutput:-
remotes/origin/HEAD -> origin/master
remotes/origin/development
remotes/origin/master
remotes/origin/production
remotes/origin/devusera
remotes/origin/testinguser
Switch Branches in Git
We can easily switch to other branches by using the following command
git checkout <branch_name>Exaample:- git checkout vikas Here we've switched to the vikas branch
Delete Branches in Git
To delete a local
branch:
git branch -d <local_branch>Exaample:- git branch -d testingHere we've deleted the testing branch
Use the -D
option flag to force it.
git branch -D testing
The -d
option stands for --delete
, which would delete the local branch, only if you have already pushed and merged it with your remote branches.
The -D
option stands for --delete --force
, which deletes the branch regardless of its push and merge status, so be careful using this one!
Git Staging
Staging a file is simply preparing a file to commit. When we add or modify some files, we need to stage those changes into “the staging area”
For single file
git add <file_name>git add abc.js Here I have added abc.js to the staging area and it can commit to the server now.
For multiple files
git add <file_name1> <file_name2> <file_name3>git add abc.js def.js ghi.jsHere I have added abc.js, def.js and ghi.js to the staging area at once and it can commit to the server now.
For all files
git add .This will all the modified files and new files to the staged area
Git Unstage Changes/Removing Files from Staging Area
If we want to remove a certain file from the stage, then use:
For Single File
git reset HEAD abc.jsIt will revert back to the last pushed code to the server or all the new changes will gone
For Multiple Files
git reset HEAD abc.js def.js ghi.jsIt will revert back all the three files to the last pushed code to the server or all the new changes will gone
For All Files
git reset HEAD .It will remove all the modified files files or unstage all files and the code will revert back to last push code
We can also create an alias for a command and then use it with Git:
$ git config --global alias.unstage 'reset HEAD'
$ git unstage .Here we don't need to write reset HEAD again and againgit reset HEAD . -> git unstage .git reset HEAD abc.js > git unstage abc.js
Git Status
If we want to see what files have been created, modified or deleted, Git status will show us a report for the same. For this, use the following command:
git status
Git Commits
It’s always a good practice to commit your code with the proper message. For committing the code, we need to first stage the code, then after we can commit the code
The commit
command requires a -m option which specifies the commit message.
We can commit your changes like:
git commit -m "Updated Configuartion for Development File"Here we're pushing code with a relevant message that we've changed in the following file for some purpose
Undoing Commits
We can undo our last recent commit by using the following command and the important thing is here we don’t lose any work:-
git reset --soft HEAD~1
To completely delete the commit and remove our changes(will lose work)
git reset --hard HEAD~1
Git Push
When we’re done with code commit, then we’re ready to deploy our code to remote.
First Push
Push a local branch for the first time:
git push --set-upstream origin <branch>
After that, then we can just use the following command:
git push
If we don’t want to set anything, then use:
git push origin <branch_name>Example:- git push origin vikasPush code to the "vikas" branch
Git Fetch
When we use git fetch
, Git doesn’t merge other commits them with your current branch. It will update our directory with the remote, if anything is done from other machine or remotely, then we should know about that
Fetch changes from upstream
git fetch upstream
Git Pull
It will use to fetch changes from the server. If anything new or file are being modified, we’ll get all the update code to our local branch. If we change in the same file, then it might result in git conflict
Pull from a branch
If we want to pull from some specific branch, then use:
git pull origin <branch_name>Example:- git pull origin vikasHere we get all the code from the remote(vikas branch) to local machine
Git Merging and Rebasing
The merge
will generate a new commit and writing the code from one branch to another branch
The rebase
re-writes the changes of one branch onto another without creating a new commit.
Merge master branch to vikas branch
git checkout vikas
git merge masterHere all the code will merge from master branch to vikas branch
And With the rebase option, we use:
git checkout vikas
git rebase master
Merge vikas branch to master branch
git checkout master
git merge vikasHere all the code will merge from vikas branch to master branch
Git Stash
Sometimes we make changes on a branch, and we want to switch to another branch, but you don’t want to lose your changes, then we stash our changes and do the other stuff.
We can stash your changes by using the following command:-
git stash
Now, if we want to unstash those changes and bring them back into our working directory, we use:-
git stash pop
We can stash multiple times and the most important one git stash pop from the last git stash.
~ Changes Done (1)
git stash~ Changes Done (2)
git stashgit stash pop
Revert back to Changes Done (2)git stash pop
Revert back to Changes Done (1)