Skip to main content

Git

Git is needed for version control, deployment, and for collaboration in a team. The most important things you need to know about git are the following:

GitHub

First you need to have a remote repository to be able to push your changes to. If you didn't already, just create that on github and running this in your folder:

git init

Pushing your changes

This is how to add something to git

git add filePath

To add all stuff you changed, you can just do git add . for simplicity

After adding your files to your staged files, you need to commit them. It can be done with the following command

git commit -M 'your message'

This will commit your changes, ready to be pushed to your remote repository.

After adding and committing, all that's left is to push.

git push

Cloning

git clone [url]

This clones a public repo (or a repo you have access to) to your computer. After that you can make changes, and push and pull.

Pulling

git pull

This fetches changes on the repo on your branch

Making a new branch

You can easily change your branch with the following command

git branch -M branchname

Checking out an existing branch

git checkout branchname

It's no rocket science...

Stashing and getting it back

Sometimes you have changes while the branch you were working on changed. You can easily continue by doing the following:

git stash
git pull
git stash pop

Gitignore

If you don't want something to be added to your git, make sure you have a .gitignore file that mentions it.

This gitignore file can contain any glob pattern of files. These files will be ignored. A .gitignore file can be located anywhere in your repository, doesn't matter.

Workflow

Usually, when you're working in a team, it is expected that you make a new branch when you start working on something. Once you're done you can make a PR from the GitHub.com UI.

It is good practice to push your changes to remote as often as possible. Don't leave your stuff local at the end of the day. Always make a PR so others can see what you have been working on. If it's not done, just call it a work in progress (#wip).

Pull requests (PR's)

Once you have finished a branch and pushed it, open the "pull requests" tab in your github repo, and create a new one. Select your branch and open a pr to merge it to the main one (usually main or live, or the one that you want to merge it to, e.g. test)

You can then see all your changes and create the PR. Then someone can review it and merge it if it's good enough.

Best practices

Keep the following in mind

  • Never push big files and folders (e.g. /node_modules) to .git, it messes up your repo. If you made a mistake like that, you can always fix it, but I'm not going to explain that. Just google it
  • At sensible I've decided to keep live as the main branch and if we have a testing environment we should all call it test, to keep it simple and easy to understand.

That's it!

That's about it for git! Do you want to know more, just have a look at their docs.

If you ever run in a problem, you can just google it.

Let's git going!

I would recommend writing all of these commands down (on your hand?) so you can easily find them. Also it wouldn't hurt to just repeat them over and over in your head for about half an hour, so you'll never have to go back to docs.

This is all I thought you in a small form:

init
add
commit -M
push
clone
pull
branch -M
checkout
stash
stash pop

If you don't know what this means, just read it again and try this out (recursively 😄)

Wizard stuff!

Everything you do with the git cli can also be done using the file system. Everything you do is saved in the hidden .git folder! Just try and have a look, cd .git inside the root of your git repository and ls there. It's pretty cool!

Think about the stuff you could do with all this data! Programs like wakatime also use it in order to provide their services.

Did you learn something?

Click the button below to tweet about what you thought of this section, what you've made, what you learned... Also don't forget to share it with us via slack! Cool tweets (or other social media posts) will be selected and presented on this page!

Tweet!