(Last Updated: 05-11-21) Work in Progress: My experience so far working with Git and GitHub.
Git
Git is a version control system. Version control is software that is important for tracking changes to code over time. It is the most popular version control system by far. Every developer should know it, and everyone should know of it. Here are some important git commands:
1
$ git init
This initializes a new Git repository at the current working directory and creates a new master branch.
1
$ git status
This checks the status of the repository, project files, and branch.
1
$ git add file_name # (e.g. git add hello.js)
This adds the changes in the file_name of the working directory to the staging area for the next commit.
1
$ git add .
This is a useful variation to select and add all the new or changed files in the current directory to the staging area to commit.
1
$ git commit
This commits the files in the staging area. To commit is essentially to save your changes to the local repository.
1
$ git commit -m "commit description"
Commits require a description. If you just use
$ git commit
then you will need to enter a description in a text editor, so this just avoids that.
1
$ git log
This sees the commit history.
1
$ git checkout branch_name # (e.g git checkout master)
This switches to the branch branch_name.
1
$ git branch branch_name # (e.g. git branch v2)
This creates a new branch branch_name. Branches are helpful for adding new changes without altering an existing branch. Note that it doesn’t switch to the branch automatically: to do so, you must checkout the branch.
1
$ git checkout -b branch_name (e.g. git checkout -b v2)
This creates a new branch branch_name and switches to it. Essentially it combines the branch command with the checkout command.
1
$ git merge branch_name # (e.g. git merge v2)
This merges the branch branch_name with the current branch.
1
$ git rebase branch_name # (e.g. git rebase master)
This moves the base (“rebase”) or start of the current branch to branch_name’s ending point. It’s helpful for having a clean, linear, and streamlined project history. It can also help handling merge conflicts one commit at a time, rather than all at once as in a merge. I will add the golden rule of never using
$ git rebase
on a public branch. More on this here.
This is a good start to learning Git. I will update with details about using the code hosting platform, GitHub, soon.