Mr. Editor-in-chief Mr. Editor-in-chief July 26, 2022 Updated April 24, 2026

Git from Zero to Pro Reference

1. CMD (Terminal / Powershell)

ls
cd ~/desktop/project
# Git commands must be run inside the folder that contains all the code

2. Creating Commits

"Version" == "commit" in Git as "version history" == "commit history"

git init
git status

git add <file|folder>
git add file
git add folder/
git add .

git commit -m "message"
git commit -m "message" --amend

git log --all
git log --all --graph

git checkout <hash>  # go check the status of whatever commit
git checkout main    # go back to the main (the last stage)

3. Configure Name & Email for Commits

git config --global user.name "Your Name"
git config --global user.email "exmaple@mail.com"
git config --global alias.shortcut <command>  # create an alias
git config --global alias.s "status"          # git s == git status

4. Go from Working Directory to Staging Area to Repository (Commit History)

git add .                       # working => staging
git commit -m "message"         # staging => commit history

git reset <file|folder>         # staging => working 
git reset file
git reset folder/
git reset .

git checkout -- <file|foler>    # working => remove the changes 
git checkout -- file
git checkout -- folder/
git checkout -- .

git restore .                   # git restore is a new command that can replace "git checkout --"

5. Viewing Previous Commits

git checkout <commit_hash|branch_name>
# HEAD indicates which commit you are currently viewing

6. Restoring to a Previous Commit

git checkout <hash|branch> <file/folder>          #  restore the contents of files back to a previous commit
git checkout <hash|branch> file
git checkout <hash|branch> folder/
git checkout <hash|branch> .

7. Other Git Features

.gitignore  # tell git which files/folders it SHOULD NOT track
rm -rf .git # remove git from project

8. GitHub

# Local repository = a git repository saved on our computer
# Remote repository = a git repository saved online (for example on GitHub or GitLab)

9. Uploading Code to GitHub

git remote add <remote_name> <url> # link a local repo to a remote repo and give a name for this link
git remote add origin https://github.com/github_username/repo_name
git remote    # list all remote repositories that are linked
git remote -v # list all remote repositories that are linked and with more detail

git remote remove <remote_name> # removes a link to a remote repository
git remote remove origin

git push <remote_name> <branch> # upload a branch of your git version history to your remote repositoy
git push origin main
git push origin main --set-upstream # sets up a shortcut for this brnach and remote repo, next time you are on the main branch and you run git push , it will automatically push the main branch to origin
git push -u origin main # shortcut of the above

git push origin main -f  # force-push the branch to the remote repository (it will overwrite what's on the remote repository)

10. Downloading Code from GitHub

git clone <url>
git clone <url> <folder_name>  # download the repository and give it a different folder name
git fetch  # updates all remote tracking branches
git pull origin main # downloads any new commits from the main branch on origin , and updates the local main branch with those new commits
git pull origin main --set-upstream # sets up a shortcut

11. Branching

Branching is to create a copy of the version history that we can work on without affecting the original version history. This lets us work on multiple things (features + fixes) at the same time.

git branch                        # list all local branches
git branch --all                  # list all branches including remote ones
git branch <branch_to_be_created> # create a new branch
git checkout <branch>             # switch to another branch
git branch -D <branch>            # delete a branch

12. Merging

git merge <branch_name> -m "message"
git checkout main                           # switch to main branch and
git merge feature1 -m "message"             # merge the main branch with the feature1 branch. The result of the merge will be added to main as a commit (a "merge commit")

13. Feature Branch Workflow

A popular process that companies use when adding new features to their software - Create a branch for the new feature (called a "feature branch")\

git branch new-feature
git checkout new-feature

# after making changes to the code ...
git add .
git commit -m "new feature message"
  • Upload the feature branch to GitHub
git push origin new-feature
  • Create a pull request on GitHub (a pull request lets teammates do code reviews and add comments)
  • Merge the feature branch into the main branch (by opening the pull request in the browser and clicking "Merge pull request")
  • After merging, update the local repository (so that it stays in sync with the remote repository on GitHub)
git checkout main
git pull origin main

14. Merge Conflicts in Feature Branch Workflows

A merge conflict can happen if two or more pull requests change the same file on the same line. We can either - Resolve the merge conflict on GitHub - or Resolve the merge conflict on our computer

# 1. get the latest updates from main
git checkout main
git pull origin main
# 2. get the latest updates from the feature branch
git checkout feature4
git pull origin feature4
# 3. merge main into the feature branch ( feature4 ), notice the direction of the merge: we want the merge commit to stay on the feature branch so our teammates can review it
git checkout feature4
git merge main
# 4. Push the resolved feature branch to GitHub
git push origin feature4
# 5. "Merge pull request"