Github crashcourse

Terchilă Marian
4 min readApr 6, 2020

You can create a new Repository on GitHub through their website.

  1. When creating a new Repository remember to tick the box to add an initial README file, this makes things easier to setup.
  2. On a terminal of your choice, choose a folder, then:

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

  1. Go inside the folder of your Repository. (Windows: Shift Right-Click within a folder and choose open terminal or PowerShell)
  2. Check the Status to see information such as what branch you’re currently on: git status
  3. Make sure you are on the right branch before anything else, check the status.
  4. The next step is to choose what files will go in the commit.
  5. git add FileName, i.e: git add index.html - do this if you want to add a specific file, otherwise:git add -A or git add . to add all of the files of the current directory, this will compare all of the files from the current repository against the branch selected and add the files that have been changed.
  6. git commit -m "Write your commit message here" - The commit message should be very short and should be about the changes you've made to the code.
  7. You can commit multiple times but none of the changes will update the online repository until you have pushed them, to do that do: git push

When working in teams multiple people will want to make changes to multiple different files and as long as these changes don’t change the same file you should just be able to do git pull whenever you want to check that your version of the repository is up-to-date. However, in a project, it is generally not a good idea for everyone to keep pulling and pushing changes onto a main branch (which is usually master), this is why, there are branches. Branches are essentially copies of a given version( branch) of the repository, but once they are created they obviously do not stay up-to-date with any of the other branches but they can be merged at which point you can deal with all of the merge conflicts, all in one go.

To create a branch, do:

  1. Make sure you got the latest version of the branch that you want to branch, either by clonning or by pulling the new changes (git pull).
  2. Do git branch to view existing branches and to verify what branch you're currently on as this will be the branch that will be branched.
  3. Do git branch NameOfTheBranch to create a branch.

You can switch to a different branch at any point in time, just be aware that you will need to commit or discart the changes that you’ve made before doing so. To switch branches, do:

git checkout NameOfTheBranchYouWantToSwitchTo

If you are the owner of the main branch that you want to merge your branch with you can pull the latest changes from that main branch then

Workflow

Where the star denotes the branch your currently in

git checkout SecondaryBranch

Make Your Changes to the Repository

git add . git add readme.md

You can wether specify the files you want to modify or go with . which will automatically add all changes to the commit

git commit -m "Made some changes to X Module"
git checkout master git pull

This will download any new changes to the MainBranch, possible merge conflicts may be generated here, read about how to solve them here.

git checkout SecondaryBranch git merge MainBranch git push origin SecondaryBranch

At this stage, you have to options, the first one being that you are done implementing what you were meant to do to and you want to merge your changes to the main branch. In which case you use the first instruction, or in case you still have some work left and want to upload what you have till this point you can push to your second branch

Ultimately you can also merge a branch using the GitHub website if there are no merge conflicts by making a pull request which also has to be approved before merging.

Originally published at https://github.com.

--

--