Introduction
In software development writing code, collaborating, and merging code from several developers or contributors, managing versions, rollbacks are very important and common things. Without using any tools this will be hassling, time-consuming, and almost impossible. There are various code management and version control tools available in the market. Among all the Git is a famous and mostly used version control tool in the world. This article describes how developers can use the git command to manage their repository and source code in GitHub. Additionally, it explains each command and process that the developers need to follow to manage their work and remotely collaborate with their teammates in a proper way. The article provides some real industry use case git commands with some examples.
Let’s begin.
Push code using Git Command for the First time
This section provides you simple way to Push code using Git Command for the first time once you created the repository in your GitHub account.
Open CLI from the folder location where you want to set up your local repository and then run the below command.
git clone https://github.com/SatyaKarki/SampleRepo
git init // initializes the folder/repo that you have in your local device
cd SampleRepo //go to directory and then place your code or files inside there then run below command
git add . //adds all the files in your commit since the last changes you have made in your local.
git commit -m"your comment" //commit code with message
git push
Adding git remote upstream and origin
Remote refers to the same version of the repository which is on the server, here in GitHub. Sometimes, we need to add remote origin and upstream to do your part of the job using the git such as to contribute in Open source community or some organization, in this case, you may need to add remote origin and upstream. Additionally, it may be needed in the case when you fork other repositories.
Command to Add and Set origin
//adding and setting for your own repo as origin
git remote add origin https://github.com/SatyaKarki/SampleRepo.git
git remote set-url origin https://github.com/SatyaKarki/SampleRepo.git
//adding and setting for forked repo
git remote add origin https://github.com/SatyaKarki/StratisFullNode.git
git remote set-url origin https://github.com/SatyaKarki/StratisFullNode.git
Command to Add and Set upstream
git remote add upstream https://github.com/stratisproject/StratisFullNode.git
git remote set-url upstream https://github.com/stratisproject/StratisFullNode.git
Working with Branch
Cloning specific branch
git clone -b your-branch git@github.com:user/yourproject.git
//below is example
git clone -b release/1.2.0.0 https://github.com/stratisproject/StratisFullNode.git
Add newly created Specific Branch
git fetch upstream BranchName: BranchName //Fetches branch from upstream to local
git status
git push origin BranchName //Creates Branch in origin
Create a new branch from the existing branch and push it to the origin
git branch newbranchname oldbranchname //it creates the new branch copying from old(existing branch)
git checkout newbranchname
git push origin newbranchname
//Then you can do any changes to newly creates branch and push it as usual.
Fetching the remote and setting your local branch to match with remote
git fetch origin
git reset –hard origin/master
The above example matches your current checkout branch in your local repo to the remote origin with the master branch.
Working with a specific branch
git checkout specificBranchName //Checkout to your branch
git fetch upstream
git pull
git merge upstream/specificBranchName
git push //pushes to your remote origin
Other Git Commands
Check version
git remote -v
See current branch
git branch --show-current
Checkout to branch
git checkout branchname
Delete local branch
git branch -d <branch_name>
git branch -D <branch_name>
Summary
Hence, this writeup provided insight into the most used and important git commands for the developers to do their day-to-day tasks with use case scenarios.
I love what you guys are usually up too. Such clever work and exposure!
Keep up the amazing works guys I’ve added you guys to our blogroll.
When I originally left a comment I appear to have clicked on the -Notify me when new comments
are added- checkbox and from now on each time a comment is added
I get four emails with the exact same comment. There has to be
a way you can remove me from that service? Cheers!