|
| 1 | +--- |
| 2 | +title: Working with GitHub |
| 3 | +author: alkaison |
| 4 | +date: 2023-02-11 16:00:00 +0530 |
| 5 | +categories: [Blogging, Git & GitHub] |
| 6 | +tags: [git, github, github repository, local and remote files] |
| 7 | +--- |
| 8 | + |
| 9 | +### New GitHub Account |
| 10 | + |
| 11 | +- Create a [GitHub](https://github.com "GitHub Website") account to create your remote repositories. Now, create a new repo where we will be uploading our files from local repo. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- Note - Local repository (repo.) means the repo. which is on our system whereas, remote repo. means the one which is on other remote system/server, for eg. - GitHub, GitLab, Bitbucket, etc. |
| 16 | + |
| 17 | +### Push local repo to GitHub |
| 18 | + |
| 19 | +- Copy the url or the link of the repo that we just created. |
| 20 | + |
| 21 | +- Paste the copied url in the below git command. |
| 22 | + |
| 23 | + |
| 24 | +```terminal |
| 25 | +git remote add origin <paste copied URL here> |
| 26 | +``` |
| 27 | + |
| 28 | +> <kbd>CTRL</kbd> + <kbd>V</kbd> won't work in terminal. Use <kbd>SHIFT</kbd> + <kbd>INSERT</kbd> to paste the link into the terminal. |
| 29 | +{: .prompt-warning } |
| 30 | + |
| 31 | +### Add Remote Destinations |
| 32 | + |
| 33 | +- It specifies that we are adding a remote repository, with the specified URL, as an origin to our local Git repo. |
| 34 | + |
| 35 | +- Finally, pushing our master branch to the origin URL (remote repo) and set it as the default remote branch. |
| 36 | + |
| 37 | +```terminal |
| 38 | +git push --set-upstream origin master |
| 39 | +``` |
| 40 | + |
| 41 | +- Go back into GitHub and see that the repository has been updated. |
| 42 | + |
| 43 | +### Pushing local repo to GitHub |
| 44 | + |
| 45 | +- First commit all the changes. Then push all the changes to our remote origin i.e. remote repo on github. |
| 46 | + |
| 47 | +```terminal |
| 48 | +git push origin |
| 49 | +``` |
| 50 | + |
| 51 | +### Pull local repo from GitHub |
| 52 | + |
| 53 | +- Git pull is used to pull all changes from a remote repository into the branch we are working on. It is a combination of fetch and merge. Use it to update your local Git. |
| 54 | + |
| 55 | +```terminal |
| 56 | +git pull origin |
| 57 | +``` |
| 58 | + |
| 59 | +### Pull Branch from GitHub |
| 60 | + |
| 61 | +- First, check which branches we have and where are we working at the moment by using this command. |
| 62 | + |
| 63 | +```terminal |
| 64 | +git branch |
| 65 | +``` |
| 66 | + |
| 67 | +- Since we do not have the new branch on out local Git which is to be pulled from the Github. So, to see all local and remote branches, use - |
| 68 | + |
| 69 | +```terminal |
| 70 | +git branch -a |
| 71 | +``` |
| 72 | + |
| 73 | +#### For viewing only remote branches |
| 74 | + |
| 75 | +```terminal |
| 76 | +git branch -r |
| 77 | +``` |
| 78 | + |
| 79 | +- Now, the new branch is seen in the console but it is not available on |
| 80 | +our local repo. So, let’s check it out using |
| 81 | + |
| 82 | +```terminal |
| 83 | +git checkout <branch name> |
| 84 | +``` |
| 85 | + |
| 86 | +- Now run the below command to pull that branch on our local repo. |
| 87 | + |
| 88 | +```terminal |
| 89 | +git pull |
| 90 | +``` |
| 91 | + |
| 92 | +- We can now check the available branches using the command. |
| 93 | + |
| 94 | +```terminal |
| 95 | +git branch |
| 96 | +``` |
| 97 | + |
| 98 | +### Push branch to GitHub |
| 99 | + |
| 100 | +- First, let’s create a new local branch which we will be pushing to |
| 101 | +Github. Enter the command as |
| 102 | + |
| 103 | +```terminal |
| 104 | +git checkout -b <branch name> |
| 105 | +``` |
| 106 | +- You can check the status of the files in this current branch using |
| 107 | + |
| 108 | +```terminal |
| 109 | +git status |
| 110 | +``` |
| 111 | + |
| 112 | +- Commit all the uncommitted changes for all the files in this branch using |
| 113 | + |
| 114 | +```terminal |
| 115 | +git commit -a -m “<Message>” |
| 116 | +``` |
| 117 | + |
| 118 | +- Now push this branch from our local repo to Github using |
| 119 | + |
| 120 | +```terminal |
| 121 | +git push origin <branch name> |
| 122 | +``` |
| 123 | + |
| 124 | +### Git Clone from GitHub |
| 125 | + |
| 126 | +- We can clone a forked repo from Github on our local repo. A clone is a full copy of a repository, including all logging and versions offiles. Move back to the original repository, and click the green "Code" button to get the URL to clone. Copy the URL. |
| 127 | + |
| 128 | +- Now in the git bash, enter the following command to clone the copied repo onto your local machine |
| 129 | + |
| 130 | +```terminal |
| 131 | +git clone <copied URL> |
| 132 | +``` |
| 133 | + |
| 134 | +- To specify a specific folder to clone to, add the name of the folder after the repository URL, like this |
| 135 | + |
| 136 | +```terminal |
| 137 | +git clone <copied URL> <folder Name> |
| 138 | +``` |
0 commit comments