Suppose you are working in a big project and after some changes you want to revert some changes and want to work with new codes. Also when team is working in one project, then how to share their work? Here comes the Git!
Git is the open source distributed version control system developed by Linux Torvalds as a part of Linux Kernal development. Git makes all community developers to share your project and work colloborately.
In this article, we will not go deep about what is git, or how git works. Rather than we will upload project on GitHub using command line tool, one of the most popular Git repository hosting service. There are also many other popular Git service providers are available, like Bitbucket, GitLab, which provides free private repository. You can go any of them, all commands will be same for all Git client. So let's start.
Git installation and setup
First of all, you need to have git installed in your local machine. Run this command in Terminal to check if git is already installed.
You should get git version in output
Else you need to install git with bellow command:
sudo apt-get install sudo apt-get install git
After that Git is installed, you need to set your username and email address. All your commits will be shown with this information.
git config --global user.name "jiteshmeniya" git config --global user.email firstname.lastname@example.org
Now if you don't have GitHub account, create new Github account. For GitHub, go to GitHub and register. All of your uploaded project will be stored in your account. After you have created account, create new GitHub repository.
Upload project to GitHub
Now your git repository is ready to upload project, perform following command to upload project in Git. For that, go to project root directory in Terminal.
Initialise with Git repository in project
Stage all files to Git index
git add --all
You can also add selected files to index
git add file1 file2
Commit your changes
git commit -m "Initial upload"
Now add your remote url to local project. If you want to add ssh url, then you need to set ssh key in your local machine. origin is the name of git repository in your local project. You can add as many git repository to your local project as you want.
git remote add origin https://github.com/jiteshmeniya/My-new-project.git
and push the code in git repository
git push -u origin master
If you added HTTPS remote url, then you need to enter your GitHub username and password. Now your project is live on GitHub.
Some important Git command.
You can track your changed files in local project.
You can also track changes made in your file.
git diff file.txt
Update your local project from Git repository
git pull origin master
You can clone(download) public git repository
git clone repository-url
Check all Git remote repositories
git remote -v
Get repository url of origin
git remote get-url origin
Remove origin repository url from project
git remote rm origin
Change repository url
git remote set-url origin new-url
Show your all local branch
Create a new branch
git branch branch-name
Change working branch
git checkout branch-name
Merge new branch to master
git checkout master git merge branch-name
This way you can easily upload your local project to new git repository. Please comment bellow if we forget to add basic Git command.