{"id":447,"date":"2024-06-27T12:23:20","date_gmt":"2024-06-27T12:23:20","guid":{"rendered":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/?p=447"},"modified":"2024-06-29T05:26:06","modified_gmt":"2024-06-29T05:26:06","slug":"push-code-from-gitub","status":"publish","type":"post","link":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/","title":{"rendered":"How to push code from gitub to linux server"},"content":{"rendered":"<p>Push code from gitub :<\/p>\n<p>Pushing code from GitHub to a Linux server involves several steps, including setting up SSH access, cloning the repository, and pulling updates. Here&#8217;s a step-by-step guide:<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#Push_code_from_gitub\" >Push code from gitub<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#1_Set_Up_SSH_Access\" >1. Set Up SSH Access<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#2_Clone_the_Repository_on_the_Linux_Server\" >2. Clone the Repository on the Linux Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#3_Pull_Updates_from_GitHub_to_the_Linux_Server\" >3. Pull Updates from GitHub to the Linux Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#4_Automate_Deployment_Optional\" >4. Automate Deployment (Optional)<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#Using_Git_Hooks\" >Using Git Hooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/push-code-from-gitub\/#Using_Continuous_Deployment_Tools\" >Using Continuous Deployment Tools<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Push_code_from_gitub\"><\/span>Push code from gitub<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"1_Set_Up_SSH_Access\"><\/span>1. Set Up SSH Access<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ensure you have SSH access to your Linux server.<\/p>\n<ol>\n<li><strong>Generate SSH Key Pair<\/strong> (if you don\u2019t have one): On your local machine, run:<\/li>\n<\/ol>\n<pre>ssh-keygen -t rsa -b 4096 -C \"your_email@example.com\"<\/pre>\n<p><strong>Add SSH Key to GitHub<\/strong>:<\/p>\n<ul>\n<li>Copy the public key to your clipboard:<\/li>\n<\/ul>\n<pre>cat ~\/.ssh\/id_rsa.pub<\/pre>\n<p>.Go to GitHub, navigate to <strong>Settings<\/strong> &gt; <strong>SSH and GPG keys<\/strong> &gt; <strong>New SSH key<\/strong>, and paste your public key.<\/p>\n<ul>\n<li><strong>Copy SSH Key to Linux Server<\/strong>:\n<ul>\n<li>Use <code>ssh-copy-id<\/code> to copy your SSH key to the server:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>ssh-copy-id user@your_server_ip<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"2_Clone_the_Repository_on_the_Linux_Server\"><\/span>2. Clone the Repository on the Linux Server<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li><strong>SSH into the Server<\/strong>:<\/li>\n<\/ol>\n<pre>ssh user@your_server_ip<\/pre>\n<p>2<strong>.Clone the Repository<\/strong>:<\/p>\n<pre>git clone git@github.com:your_username\/your_repository.git<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"3_Pull_Updates_from_GitHub_to_the_Linux_Server\"><\/span>3. Pull Updates from GitHub to the Linux Server<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li><strong>Navigate to the Repository Directory<\/strong>:<\/li>\n<\/ol>\n<pre>cd your_repository<\/pre>\n<p><strong>Pull the Latest Changes<\/strong>:<\/p>\n<pre>git pull origin main<\/pre>\n<p>Replace <code>main<\/code> with the branch name if you\u2019re using a different branch.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Automate_Deployment_Optional\"><\/span>4. Automate Deployment (Optional)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You might want to automate the deployment process using hooks or continuous deployment tools.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Using_Git_Hooks\"><\/span>Using Git Hooks<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ol>\n<li><strong>Create a Post-Receive Hook<\/strong>:On your server, navigate to the <code>hooks<\/code> directory in your repository:<\/li>\n<\/ol>\n<pre>cd your_repository\/.git\/hooks<\/pre>\n<p><strong>Create a <code>post-receive<\/code> Hook<\/strong>:<\/p>\n<pre>nano post-receive<\/pre>\n<p><strong>Add the Following Script<\/strong>:<\/p>\n<pre>#!\/bin\/bash\r\nGIT_WORK_TREE=\/path\/to\/your\/project git checkout -f<\/pre>\n<p>Replace <code>\/path\/to\/your\/project<\/code> with the path to your project directory.<\/p>\n<ul>\n<li><strong>Make the Hook Executable<\/strong>:<\/li>\n<\/ul>\n<pre>chmod +x post-receive<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Using_Continuous_Deployment_Tools\"><\/span>Using Continuous Deployment Tools<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Tools like Jenkins, Travis CI, CircleCI, or GitHub Actions can automate deployment. For instance, with GitHub Actions:<\/p>\n<ol>\n<li><strong>Create a Workflow File<\/strong>:In your repository, create a <code>.github\/workflows\/deploy.yml<\/code> file.<\/li>\n<li><strong>Add Deployment Configuration<\/strong>:<\/li>\n<\/ol>\n<pre>name: Deploy to Server\r\n\r\non:\r\npush:\r\nbranches:\r\n- main\r\n\r\njobs:\r\ndeploy:\r\nruns-on: ubuntu-latest\r\n\r\nsteps:\r\n- name: Checkout code\r\nuses: actions\/checkout@v2\r\n\r\n- name: Deploy to Server\r\nuses: appleboy\/ssh-action@master\r\nwith:\r\nhost: ${{ secrets.SERVER_IP }}\r\nusername: ${{ secrets.SERVER_USER }}\r\nkey: ${{ secrets.SSH_PRIVATE_KEY }}\r\nscript: |\r\ncd \/path\/to\/your\/project\r\ngit pull origin main<\/pre>\n<p><strong>Add Secrets<\/strong>:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Go to your GitHub repository, navigate to <strong>Settings<\/strong> &gt; <strong>Secrets and variables<\/strong> &gt; <strong>Actions<\/strong>.<\/li>\n<li>Add the following secrets:\n<ul>\n<li><code>SERVER_IP<\/code>: Your server&#8217;s IP address.<\/li>\n<li><code>SERVER_USER<\/code>: Your server&#8217;s username.<\/li>\n<li><code>SSH_PRIVATE_KEY<\/code>: Your SSH private key.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>By following these steps, you can push code from GitHub to a <a href=\"https:\/\/www.squarebrothers.com\/dedicated-server-india\/\" target=\"_blank\" rel=\"noopener\">Linux server<\/a> and even set up automated deployments.<\/p>\n<p>If you have error while updating php updates in Linux server <a href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/php-updates-got-failed-error-in-direct-admin\/\">click here\u00a0<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Push code from gitub : Pushing code from GitHub to a Linux server involves several steps, including setting up SSH access, cloning the repository, and pulling updates. Here&#8217;s a step-by-step guide: Push code from gitub 1. Set Up SSH Access Ensure you have SSH access to your Linux server. Generate SSH Key Pair (if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":8,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/447\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}