The most simple guide for getting started with Git and GitHub
In this section you will:
- Create a new remote repository on GitHub
- Clone the new repository on your computer
Go to https://www.github.com and create a new repository
Enter the required information about your repository. The only mandatory field is "name", but make sure to check "Add a README file", which will make your life easier later.
In the repository you have just created, click the green button "Code" and copy the https url
In your computer, open a command prompt and go to the folder where you want to save your local repository (Desktop in the example).
Use git clone
followed by the url you copied before, to clone the remote repository on your computer.
At this point, you will have a folder with the same name as your repository.
Use cd <folder_name>
to get inside your folder.
In this section you will:
- Create a new branch
- Make some changes
- Commit the changes you made
Whenever you need to make some changes on a repository, starting from Main/Master, you need to create a new branch (or move to a branch you are already using for making changes) with the command: git branch <new_branch_name>
.
Then, switch to the new branch you have just created git checkout <branch_name>
.
Make some changes (for example, I have created a text file called "newFile" in my folder).
Now, you have to tell git that you want these changes to be saved, and you do so with two commands:
git add <file_name>
git commit -m "Some message"
In this case, I could have used git add newFile.txt
but, for the sake of simplicity, I used git add .
which means that I want to add all my changed files.
In this section you will:
- Use
git push
to update a remote repository
Now that everything is updated locally, we can use git push
to update the repository on GitHub
The only problem is that, even though Git knows which repository to push the contents to (because we cloned it), it doesn't know where to put this specific branch, because it has only been created locally.
Following exactly the instructions in the output message, will solve this issue: git push --set-upstream origin <branch_name>
Once push
has completed uploading the new branch with all its files, let's go back to https://www.github.com and open the repository. A message will say that there is a new branch with recent changes. Click the green button "Compare & pull request"
In the following steps, just clicking the green buttons in sequence will allow you to merge the contents from the new branch to main/master.
At the end, you can decide to delete the new branch, but this will only remove it from GitHub! The local branch, on your computer, will remain intact.
Now, we only need to update our local repository. In you command prompt, first switch back to the main repository.
Pull the most recent changes.
This is particularly important when working with other people! If someone else makes changes and merge a branch to main, using pull
is the only way to have the most updated files on your computer and not risk messing up somebody else's work.
If you need to apply more changes, go back to Section 2: Apply changes locally and repeat the procedure until the end