Git Commands for Beginners: How to Upload Project to GitHub Easily
Git is the world's most popular tool for programmers to maintain different versions of their code. Whether you are a beginner or an expert, git will be your one of the close buddies who will help you to upload your project to git platforms, or sync with your live server, or even maintaining private repositories. This article will be your guideline to help you start with the git and learn the essential steps which you will require every now and then in your development career.
Upload Your Project to Git: Step-by-Step
Git is the version controlling system by which we can create different version of your code and track the history. For beginners, it is very important learn about git on the first step. There are many popular platforms available today to support the git management like Github, Gitlab or Bitbucket. But for most of the platforms the commands and processes are quite similar. So if you learn the working ways for one platform, you will be able to work in all of them. Lets see first how we can deploy a sample project to git first.
- Prerequisites
Assume, you have already installed git in your machine. If you didnt, you can install Gitbash in your system. To check it in your machine, you can try with the command below, it should show the git version installed locally:
git --version
It should show output like this:
git version 2.31.1
Assume, you have a local project to push it to github or some other platform. We will see example here for github but the procedure is almost similar for other platforms.
Lets imagine this is your project structure:
my_project/
│
├── index.html
├── style.css
└── script.js
Now we will try to push it to github.
- Create a new repository
First, you need to login to you github account. If you dont have one, please create one.
You will see a button, add new repository, click on it, then give a name to the repository and keep the privacy public for now. Then create the repository.
- Deploy your app to repository
Now to deploy your code to the repository, you need to go to the root directory of your app.
Then run this command:
git init
After that add the remote repository link with this command:
git remote add origin https://github.com/yourusername/your-repository-name.git
Replace the https link with your remote repository link. Here is where you can find the repository link in github:
Now run these commands one after another:
git checkout -b main
git add .
git commit -m "Initial commit"
The first command will create and swtich to a branch named main in the local.
Then the second command will add all the files to tracked
After that, you commit those changes with third command.
Now, these are all done to your local repository. To push them to remote, you have to type:
git push origin main
Thus your full code updates will be pushed to your repository’s main branch in github.
You can follow this process to push your codes efficiently to remote repository. There might be some more features and options to learn, but for now, just showed the most simple and direct way just to start with git.
Deploy your site from git repository to cpanel web hosting automatically
Cpanel is one of most commonly used tool to manage your application in server. We often deploy application live with cpanel by manually uploading files. Alternatively, we can also use git to directly deploy our application automatically. This method is handy and requires just some simple steps to set up. Lets see a quick example.
- Setup git repository
Lets go to github.com and create a new repository there. You can set it to public.
Then push a working project to the repo. Please go to repository home, and copy HTTPS URL. This url will be required to pull files to our cpanel’s local directory.
- Cpanel steps
Lets login to our cpanel. Here please look for the option “Git version control” and get into that. You will see there is no repository connected here if you didnt try previously. Lets click on Create to add a new repository.
You will see a Clone URL field here. Please paste the HTTPS link you have copied from github.
After pasting, there are 2 other fields, named as Repository Path and Repository Name, will get active. They will assign a value automatically. You can keep them as default for now.
Note: Repository Path defines the location, where your git repo will be cloned in cpanel.
Now please hit the “create” button.
You should see a success message that it has successfully cloned the url. Also your files are cloned to our cpanel’s local repository..
Now again, please go to cpanel dashboard and then “Git Version Control”, you should see the newly added repository there. Please click on the “Manage”.
Now you will see 2 tabs, one is Basic information, and the other is Pull or Deploy.
In the Basic Information tab, just select the branch you want to get a pull from and click on Update.
Then in the Pull or Deploy tab, click on the “Update from Remote” and your cpanel repository should be updated with the currently checked out branch..
- Deploying files to Public_HTML
At this point all our files are stored in Repository Path, All the changes we pull will be updated there. But we need to copy all these to our public_html folder, so that we can see the changes live.
You will also see the button, "Deploy HEAD Commit” as disabled. This button deploys our repository files or updates to public_html.
To enable this button, we will have to add another file in application’s root directory. Lets add a file named, .cpanel.yml and it should look like this:
deployment:
tasks:
- export DEPLOYPATH=/home/{username}/public_html/
- /bin/cp -R . $DEPLOYPATH
Here, please replace {username} with your cpanel username
please save the file, push it to git and in cpanel again click “Update from Remote”.
If things are good, you will see, “Deploy HEAD Commit” button has been enabled after certain time.
Once it is enabled, please click on that, and your all the update from git repository will be copied to public_html. Also you will see the latest changes in the site.
- Connecting Private Repositories
For managing private repositories, we will have to do some extra steps to connect cpanel to GIt. Please follow the guide below to learn about that.
Automating Git deployment to cpanel is a very useful feature for development. After successful setup, you will not have to upload your files to server after every changes. Rather, you can just push it to git and the changes will be live by one click in the button "Deploy head commit" in cpanel.
Connect private git repository with cpanel to deploy automatically
For public repositories, we can directly clone or pull from cpanel’s git feature. But for private repositories, we need to use ssh key for successful deployments. Lets see how we can do that efficiently.
- Create a private repository
We will need a private repository in github with the files we want to deploy. It can be a simple index.html or any working project we want to deploy in live server. Also you can set any of your public repository to private for following steps.
Lets create a private repository in github.com. when you create repository, you just have to create it as private. Then please push your files from local to the repository..
- Cpanel Steps
Now we need to enable shell access in the cpanel. Sometimes it is already enabled, in that case you can skip this step. Lets login to our cpanel, Go to “Manage Shell” and enable “Enable SSH access”.
then look for terminal in the cpanel, this should be now available as you have enabled “shell access”
Here lets type in this command:
ssh-keygen -t ecdsa -b 521
and for the next prompts, just press enter until it generates the key successfully.
This command will create a ssh key, which will be needed to connect your github repo with this cpanel.
Now, in the cpanel, look for “SSH Access”, then “Manage SSH Keys” and there you should find new keys that we have generated with the command.
Under public keys, you will see that its not authorized yet. To do that, we click on “Manage” beside the key and click ”authorize”.
Now go back and click on View/Download button, there you can see the key given in box, select all the text and copy
Done, we have generated ssh key that we can use to connect our repository with cpanel.
- Lets add the SSH key and test
Now lets go to our git repository home that we have created. There you will see “settings” and under “security” label, you will find “Deploy keys”. Get into that and click “Add deploy keys”.
here give any “title” and paste the key copied from cpanel and allow “Write access”. Then press “Add key”.
Now our cpanel should be connected with the git repo. Lets have a try if we have done things correctly or not.
Goto repository home and copy “SSH link” from there.
Now in cpanel, Look for “Git version control”, click on “create”.
In the clone url, paste SSH link, and press create, if we have done the steps correctly, the cpanel should be able to connect and pull from your repository now. You will see a success message if thing are working good.
Conclusion
By following the steps above, you now should have extensive knowledge on creating and maintaining git repositories, sync with server which might include private repositories too. Personally I like it very much for git that I dont have to do repetitive task like uploading the project into git and then again to server. So it saves a lot of time for me. Let me know your thoughts too about git's extensive features.
Have a great day.
Comments