Welcome! Please hold on...

0 %
Kashif Sohail
Sr. Frontend Developer
  • Residence:
    Pakistan
  • City:
    Karachi
  • Age:
    26
Magento 1x 2x
Wordpress
Laravel
Angular
React
English
Urdu
  • Bootstrap, Tailwind
  • Sass, Less, Css
  • Webpack
  • GIT knowledge
0

No products in the cart.

Beginner’s guide to GitHub: Adding code to your repository

August 13, 2024

[ad_1]

Welcome back to GitHub for beginnersa series to help you navigate GitHub.

If you’ve been following us, we’ve covered some of the basics of GitHub, including the most important Git commands every developer should know, How to create repositoriesAnd How to upload files and folders to your repository.

Today we talk about the GitHub Flowand how to use it to add code to our repository. The GitHub flow is a way to use GitHub to share resources and collaborate. It’s not just for code. You can also use this process to collaborate on anything you store in a repository on GitHub.

Cloning a repository

Adding code to your repository starts with Clone this repository. Cloning a repository creates a local copy that you can use for your work. By syncing this local copy with the remote repository stored in GitHub, you can get the latest changes and push your updates for others to receive.

First, navigate to the repository on GitHub that you want to clone. I will clone the following: this repositoryClick the green button that says “<> Code”. You will see a window with a URL for your repository and a “Copy” button next to it under the “Local” tab. Click the “Copy” button.

Now open your terminal and enter git clone with an extra space at the end. Then paste the URL for your repository and press Enter.

Once the command is complete, enter the directory by cd project and then press Enter. Make sure you replace project with the name of your repository. Once you are in the directory, you are on the primarily Branch. You can verify your branch by git status and press Enter.

Create a new branch

The first thing you want to do when adding code to your repository is to create a new branch. This is an essential part of the GitHub flow as it primarily Branch.

When you create a new branch, you must give it a name. Since you are creating the first version of the project, you can Version1. When naming branches, it’s helpful to use names that describe the work you’re doing in that branch. In your terminal, type: git checkout -b version1 and then press Enter.

This will create a new branch with the name . Version1.

Commit changes with GitHub Desktop

In addition to the terminal, you can GitHub Desktop to create new branches and add code. Download and open GitHub Desktop. Just like before, the first thing you need to do is clone the repository. Click the “Clone a repository from the web…” option Paste the URL for your repository in the field provided. If you don’t want to type the full URL, you can enter the GitHub user and repository names separated by a slash instead: /Then click the Clone button.

Once you are connected to the repository, you need to create a new branch. Click on the section of the ribbon at the top of the window that says “Current Branch”. This will open a pulldown window with the branches. Click the “New Branch” button and name your new branch CSSand click the blue “Create Branch” button.

After the branch is created, you can save code in the project directory or move it using your file manager. Save or copy a Style.css File in the project folder; there you will find the Here. As soon as you add code to your project, GitHub Desktop will be notified of the change. You will see the file listed as a changed file and have the option to commit this change to the CSS Branch.

View of style.css in GitHub Desktop with the option to commit the modified file to the CSS branch.

Enter a summary of the changes you made and a short description in the fields just above the blue “Commit to CSS” button, then click the button. Now that you’ve made the changes to this branch, you’re done! Now you can push your code to the remote repository and start the pull request.

Commit changes using the terminal

Go back to the terminal, check that you are in the correct project directory and run touch index.html script.js to create empty files with these names in your local repository. Open these files with the code editor of your choice and write or copy the code you want to upload to GitHub. When you’re done, return to your terminal.

Run git status in your terminal to see the list of files you’ve been working on. At this point, you should see the two files you created: Home And script.jsAdd these files to your staging area by git add . to add both at once. This command will add all the untracked files in your project directory. Once they are added, transfer these files to the staging area by running: git commit -m "created initial project" in your terminal.

Now it is time to push your code to your repository. Before doing so, run a quick git status command to make sure you have no untracked changes. Then run git push origin version1 to submit your code to GitHub.

Opening a pull request

Now that your code has been committed, you can access your repository on GitHub through your browser. A small dialog will appear at the top of your repository indicating that CSS And Version1 had recent changes. To integrate these changes into your main branch, you need to Pull requestIn the dialog box, click the green Compare and Pull Request button for the CSS Branch.

Add a title summarizing the changes and a short description indicating what changes you made. Then scroll down and click the green “Create Pull Request” button below the description. Congratulations! You just opened your first pull request!

Screenshot of the Pull Requests tab in the GitGoing repository, showing where you can add a description and create the pull request.

After you open your pull request, GitHub will run some checks to make sure you can manually merge the pull request. If the check takes more than a few seconds, you may need to reload the page. Once the checks are complete, you’ll see a green button that says “Merge Pull Request.” This will open a dialog where you can provide a title and description of the changes. These fields will auto-populate with the information you added when you created the pull request. Click the green button below the description that says “Confirm Merge.” This will commit your changes back to the pull request. primarily Branch.

Now click on the “<> Code” tab at the top of the window. You will see that you still receive a notification for the changes. Version1. Click the Compare and Pull Request button and create a pull request again, but don’t merge it yet!

Merge with the main branch

You may have realized that your primarily Branch now has changes that are not present in your local Version1 branch. This is because your Version1 The branch was created with a copy of primarily before the CSS changes have been uploaded. When you opened the pull request, you may have even received a warning indicating that your branch is lagging primarily.

So that you can upload your changes securely from Version1 in primarilyyou will see the latest changes from primarily in your local Version1 Branch. To do this, return to your terminal.

type git switch main and then press Enter. Then run git pull to commit the CSS code changes added to the remote repository. Then run git switch version1 back to your Version1 Branch. After changing the branches, git merge main to merge the changes from the primarily branch into your current branch. After running this command, a text editor will appear with a merge message. Enter :wq to save the message and exit the editor. Now you need to commit these changes, which you do by clicking the git push origin version1 Command.

Return to your browser and navigate to the pull request you created. If you need to find it again, navigate to your repository, click the “Pull Requests” tab at the top of the window, then select the pull request. Once you’re at the pull request, select the green button that says “Merge Pull Request.”

Delete a branch

Now that your changes have been uploaded, primarily branch, you can delete your old branch. First, go to your primarily Branching by executing git switch main in your terminal. Then enter git branch -d version1 and press Enter. This will save your Version1 Branch.

Your next steps

Now that you’ve learned how to add code to your repositories, you’re ready to create projects and collaborate with other developers!

If you have any questions, please ask them in the GitHub Community Thread and we will surely answer.

Here are some more resources to help you on your journey with GitHub:

Written by

Kedasha Kerr

[ad_2]

Source link

Posted in TechnologyTags:
Write a comment