Week 2, Episode 1
Estimated Reading Time: 53 secs
My second week at ALX featured me loading up on some soft skills and technical skills:
I also picked a side in the holy war between Emacs and Vim. Of course, I chose Vim.
Best IDE ever!
While working through the technical projects, I kept asking Google the same questions over and over about Git.
So I’m creating my short and sweet stash of Git commands to instantly find a command without sifting through 2,000 words.
I like how Obed Ehoneah, PharmD classified git commands by use cases, so I'll use his classification in this article. (Thanks, Doctor!).
Here's an infographic that summarizes the basic git commands in this article. Feel free to save!
So first, let's briefly define Git and GitHub.
Git versus GitHub
Estimated Reading Time: 3 mins 41 secs
Git is a technology that helps to organize changes in code in chronological order.
Git makes it easier to track changes in code and revert to earlier code versions if the need arises.
On the other hand, GitHub is a web platform to store/host code versions. GitHub is built on the Git technology.
So Git is local on your personal computer while GitHub is remote.
Now, let's go through a short, sample workflow and outline the git commands for each step.
Create a Git Repository
Use the CLI to enter the directory you want to turn into a Git repository (Git repo) and type:
git init
That's all!
You can also create a local repo by cloning a remote repo.
Clone a Remote Repository
To clone a remote repo (i.e., download a remote repo that isn't currently on your local computer), use this command:
git clone https://github.com/<your-GitHub-username>/<remote-repo-name>.git
Next!
Make Changes To a Repository
If you're done coding and would like to see all the changes you made, type:
git status
The command above will output all your untracked, modified, staged, and deleted files.
The next step is to add your changed files to the staging area.
Stage Your Code
To add a specific file to the staging area, type:
git add <filename>
To add two or more files to the staging area, type:
git add <filename> <filename>
To add all the files you worked on to the staging area without specifying a particular one, type:
git add .
The period above acts like a wild card and selects all files.
The next step is to commit your changes.
Commit Changes to Your Local Git Repository
To commit your changes with a message, type:
git commit -m "<your-commit-message>"
Now, you've committed your code versions to Git locally.
The next step is to synchronize your current local repo with your remote repo.
Synchronize Your Local and Remote Repository
First, you have to add the remote repo that you want to push your local files to. To do this, type this command:
git remote add origin https://github.com/<your-username>/<remote-repo-name>.git
Next, you can pull changes from your remote repo (if you already have files there that you want in your local repository).
Pull Changes From Your Remote Repo to Local Repo
NOTE: It's best practice to pull changes before pushing. It reduces the chances of merge conflicts when pushing your code versions to your remote repo.
To pull changes from your remote repo, type:
git pull
This git command will fetch changes made in your remote repo and merge it with your local repo.
If there are no merge conflicts after pulling, the next step is to push your changes from your local repo to your remote repo.
Push Changes From Your Local Repo to Remote Repo
To push code versions from your local repo to your remote repo, type:
git push
The command above pushes to the "master" branch on your remote repo by default.
If you want to push to a specific branch on your remote repo instead, use this command:
git push origin <branch-name>
Since we're now talking about branches, let's move to the last use case.
Collaborate With Other Developers on GitHub
NOTE: It's best practice to create a new branch when collaborating with other developers on a new project. This helps to preserve the code in the master branch.
Create a New Git Branch
If you want to create a new branch, use this command:
git branch <new-branch-name>
Note that creating a new branch does not mean you're now on the new branch. After creating a new branch, you still have to switch to the new branch if you want to work on it.
Switch to a Git Branch
To switch to a branch, use this command:
git checkout <branch-name>
And finally, a fun tip: you can do the two steps above in one command.
To create a new branch and switch to it in one line, type this:
git checkout -b <new-branch-name>
Thank you for reading!
If you have corrections to any statement I've made in this article, kindly drop it in the comments section.
Till next week!