Basics of Git and Github
Git is a version control. Using Git, you can track the changes you make to your codes. In other words, it can save multiple versions of your codes while you are developing them. A Git repository, or repo for short, is what you track and save using Git. It is physically a tracked folder. A repo can be local or remote. A local repo is a repo in your local machine. A remote repo is on the internet, for example on Github, Gitlab, or Gitbucket. You need a local repo to track and save your own codes in your own machine. You will need a remote repo to share what you have with others. As a solo developer, you may just want to show what you did to others and let them clone your work. If you are working together with others, having a remote repo is very convenient to share with others in the team.
To use Git in your local machine, you need git installed in your machine. In a Linux or Mac machine, git is typically already there. On Linux Ubuntu, you can check if Git exists by typing:
which git
That should return the location in which git is installed, typically /usr/bin/git. If you cannot find it, you can install Git on Linux Ubuntu using:
sudo apt install git-all
In a Mac machine, you can install Git using:
brew install git-all
In a Windows machine, Git typically is not initially installed, and therefore you need to install it.
To query your Git version, type:
git --version
Working in local Git repo
To set up your local git repo:
git config --global user.name "Your name"
git config --global user.email "email@domain.com"
Suppose you have a folder in which you are working. To be able to track and save all the changes in that folder, go to that folder using “cd” and then initiate Git:
git init
This will create a hidden file .git in your current folder. To see all the files and folders in your folder, including the hidden one(s), type:
ls -a
To add file(s) into your local git repo, use “git add”:
git add (filename)
If you only partially some files in your current folder, the other files will be untracked, i.e., not included in your local git repo.
To add everything in your current folder to your local git repo, use “git add” followed by a period:
git add .
By adding some or all files in your folder using “git add”, you add them to the staging area. In this case, their status is “staged”.
To unstage a file you already move to the staging area, use:
git rm --cached (filename)
To check the status of your local git repo, type:
git status
To bring all the staged files to a level where all the changes are recorded permanently in the local git repo, use “git commit”:
git commit -m "Meaningful message/comment"
Here -m stands for “message”, followed by some meaningful description of what you commit.
When it is your first commit, people usually call it “Initial commit”. Well, this is just a convention or common practice.
To see the history of all the commits you already make, use “git log”:
git log
To show each commit in a single line, use:
git log --oneline
This will return a more readable history if you have many commits.
Interacting with remote Git repo
If you want to push your local git repo to a remote one, for example on Github, first you need to have that remote repo. If not yet, create one.
After that, from your local machine, you need to add that remote repo:
git remote add origin git@hithub.com:(your-repo-url.git)
When you want to push your local repo to the remote repo, use “git push”:
git push -u origin master
or
git push --set-upstream origin master
This will push your local repo to the “master” branch of the remote repo. Before October 2020, “master” is the default branch in Github repo. But since October 2020, the default branch in Github repo is called “main”.
But you can make a new branch called “master” on Github. But the “main” branch will be still the default branch, unless you change your default branch to your newly created “master” branch.
Since the default branch in local Git repo is still called “master”, I think it is more convenient to use “master” branch on Github as the default branch. A more practical way to do this is using the command “git push -u origin master” that will let Github create a branch called “master” dan make it default. This way you don’t need to manually create “master” branch and manually make it default. This video shows how to do this: https://www.youtube.com/watch?v=hZznWbEGv1U
In case you want to call your local Git repo “main” and make it default during the Git initiatiton, use:
git config --global init.defaultBranch main
If initially your local Git repo is called “master” and you want to rename it to “main”, use the following while you are in the “master” branch prompt:
git branch -m main
If you have multiple local Git branches, you can list all the branches using:
git branch
Your currently active branch will be indicated by an asterisk sign, and also usually green colored.
To create a new branch and switch to it, you can use the old way:
git checkout -b (new-branch-name)
Make sure you include -b to create a new branch, since “git checkout” is also used for other uses.
A new way to create a new branch and switch to it is using “git switch” (only working with newer Git versions: 2.23 and above):
git switch -c (new-branch-name)
The argument “-c” here stands for “create”.
Unlike “git checkout”, “git switch” only has one use, that is to create a new branch and switch to it.
To switch to an existing branch (without creating a new one), you can use either “git checkout” or “git switch”:
git checkout (branch-name)
or:
git switch (branch-name)
To push your local repo to another branch in your remote repo, type:
git push -u origin (branch-name)
or
git push --set-upstream origin (branch-name)
When you push your local repo to your remote repo using “git push”, you may be prompted to enter your username and password. To deal with this, generate a personal token (that will serve as your password) on your Github account. This can be done through your profile > Settings > Developer settings > Personal access tokens. In the opened window, typically you want to tick “repo” and finally press “Generate token” button. You can copy the generated token and enter it when prompted upon using “git push” command.
Please note that you use “git push -u” or “git push –set-upstream” only when the first time you push a new local branch to a remote one. “u” here stands for “upstream”. Afterwards, you can then simply use “git push” (and also “git pull”) from the local repo.
To get the latest changes from a remote repo without merging them with your local repo, use “git fetch”:
git fetch origin (remote-branch-name)
For example:
git fetch origin master
After reviewing the changes, if you want to merge the changes with your local repo, use “git merge”. First, you need to switch to your current branch and subsequently merge the updated branch to the current branch:
git checkout (current-branch-name)
git merge (updated-branch-name)
To get the latest changes from a remote repo and merge them with your local repo, use”git pull”:
git pull origin (remote-branch-name)
In other words, “git pull” is “git fetch” followed by “git merge”.
An example of using “git pull” is the following:
git pull origin master
To clone anyone’s remote repo to your local computer, not necessarily a gitted folder:
git clone (remote-repo-url.git)
It is basically to copy a remote repo to your computer.
To clone a specific branch of anyone’s remote repo:
Step 1: git clone (remote-repo-url.git)
Step 2 (optional, list all the available branches): git branch -a
Step 3: git checkout (branch-name)
Another way to clone a specific branch of a remote repo is using the following single-line command:
git clone --single-branch --branch (branch-name) (remote-repo-url.git)
Git Clone vs Git Pull
I asked Gemini the difference between “git clone” and “git pull”. Below is its answer:
“Git clone” and “git pull” are two fundamental Git commands, but they serve very different purposes:1
1. git clone
- Purpose:
git cloneis used to create a brand new local copy of an existing remote Git repository.2 Think of it as downloading the entire project for the first time. - What it does:
- When to use it: When you want to start working on a project that already exists in a remote repository (e.g., on GitHub, GitLab, Bitbucket) and you don’t have a local copy of it yet. It’s typically a one-time operation for a given project on your machine.
- Analogy: Imagine someone hands you a complete, brand-new binder with all the project documents and their full history.
2. git pull
- Purpose:
git pullis used to update an existing local repository with the latest changes from its linked remote repository. - What it does:
- When to use it: When you already have a local copy of a repository, and you want to get the most recent updates that other collaborators have pushed to the remote. You use it regularly to stay synchronized with the project’s development.
- Analogy: You already have the project binder, and someone sends you new pages or updated versions of existing pages. You then integrate those new pages into your binder.
Summary of Differences:
| Feature | git clone | git pull |
| Initial setup? | Yes, creates a new local repository. | No, updates an existing local repository. |
| Scope of data | Downloads the entire repository and history. | Downloads only the new changes from the remote. |
| Connection | Establishes the initial connection to the remote. | Requires an existing connection to a remote. |
| Frequency | Usually a one-time operation per project. | Used frequently to stay updated with remote changes. |
| Components | A single command. | A combination of git fetch and git merge (by default). |
