Skip to content

MontessoriVisualization/Git-Github

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 

Repository files navigation

๐Ÿš€ Git - 0 to Pro Reference

A comprehensive guide to master Git from beginner to pro


๐Ÿ“‘ Table of Contents


๐Ÿ’ป Command Line Basics

Command Description
ls List the files and folders in the current folder
cd ~/Desktop/folder Change the folder that the command line is running in

๐Ÿ’ก Note: Git commands must be run inside the folder that contains all the code.


๐Ÿ“ Creating Commits

In Git:

  • โœ… Version = commit
  • ๐Ÿ“š Version history = commit history
Command Description
git init Git will start tracking all changes in the current folder
git status Show all changes since the previous commit
git add <file|folder> Pick changes to go into next commit
git add file Pick individual file
git add folder/ Pick all files inside a folder (and subfolders)
git add . Pick all files (in folder command line is running in)
git commit -m "message" Creates a commit with a message attached
git commit -m "message" --amend Update previous commit instead of creating new one
git log View the commit history
git log --all Show all commits (not just current branch)
git log --all --graph Show branching visually in the command line

โš™๏ธ Configure Name & Email for Commits

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

๐Ÿ”„ Working Area vs Staging Area

  • ๐Ÿ“‚ Working Area = contains changes that start in the working area
  • ๐ŸŽฏ Staging Area = contains changes that will go into the next commit
working => staging:     git add .
staging => commit:      git commit -m "message"
staging => working:     git reset <file|folder>
working => remove:      git checkout -- <file|folder>

๐Ÿ› ๏ธ Common Commands

Command Description
git reset <file|folder> Move changes from staging to working
git reset file Unstage a file
git reset folder/ Unstage all files in folder
git reset . Unstage all files
git checkout -- <file|folder> Remove changes from working area
git checkout -- file Discard changes to a file
git checkout -- folder/ Discard changes to all files in folder
git checkout -- . Discard all changes

๐Ÿ‘€ Viewing Previous Commits

git checkout <commit_hash|branch_name>

๐Ÿ”‘ Key Concepts:

  • ๐ŸŒณ master = branch name
    1. You can git checkout branch
    2. Always points to latest commit on the branch
  • ๐Ÿ‘‰ HEAD = indicates which commit you are currently viewing

โฎ๏ธ Restoring to a Previous Commit

Command Description
git checkout <hash|branch> <file|folder> Restore the contents of files back to a previous commit
git checkout <hash|branch> file Restore a file
git checkout <hash|branch> folder/ Restore all files in folder (& subfolders)
git checkout <hash|branch> . Restore all files in project

โš™๏ธ Other Features of Git

๐Ÿ”– Create Aliases (Shortcuts)

git config --global alias.shortcut <command>
git config --global alias.s "status"
# Now: git s = git status

๐Ÿšซ .gitignore

Tell git which files/folders it SHOULD NOT track.

๐Ÿ—‘๏ธ Remove Git from Project

rm -rf .git

๐ŸŒ GitHub

๐Ÿ“ฆ Repository = a folder containing code where any changes to the code are tracked by git.
(To create a repository, we create a new folder on our computer, and then run git init)

๐ŸŒ GitHub = a service that lets us save our git repositories online. It also helps us:

  • ๐Ÿ’พ Backup our code in case we delete it on our computer
  • ๐Ÿ“Š See the history of our code changes more easily

๐Ÿ’ก Alternatives include: Bitbucket and GitLab

  • ๐Ÿ’ป Local repository = a git repository saved on our computer
  • โ˜๏ธ Remote repository = a git repository saved online (for example on GitHub)

โฌ†๏ธ Uploading Code to GitHub

๐Ÿ”— Linking to a Remote Repository

git remote add <remote_name> <url>

Link a local repository to a remote repository and give a name for this link.

Example:

git remote add origin https://github.com/SuperSimpleDev/repository1

The above command links a local repository to a GitHub repository and gives it a name "origin".

๐Ÿ”ง Managing Remote Repositories

Command Description
git remote List all remote repositories that are linked
git remote -v List all remote repositories (but with more detail)
git remote remove <remote_name> Removes a link to a remote repository
git remote remove origin Removes the link to the remote repository named "origin"

๐Ÿ‘ค Configure GitHub Username

git config --global credential.username <username>

Configure your GitHub username so you can get access to your Github repository.

๐Ÿ“ค Pushing to GitHub

Command Description
git push <remote_name> <branch> Upload a branch of your git version history to your remote repository
git branch Shows a list of available branches
git log --all --graph Shows the branches visually in the history
git push origin main Upload the branch "main" to the remote repository named "origin"

๐Ÿ”— Set Upstream (Shortcut)

git push <remote_name> <branch> --set-upstream
git push origin main --set-upstream

Sets up a shortcut for this branch and remote repository. Next time you are on the main branch and you run git push, it will automatically push the main branch to origin.

โš ๏ธ Force Push

git push <remote_name> <branch> -f

Force-push the branch to the remote repository (it will overwrite what's on the remote repository).


โฌ‡๏ธ Downloading Code from GitHub

๐Ÿ“ฅ Cloning a Repository

git clone <url>
git clone https://github.com/SuperSimpleDev/repository1

# Download the repository and give it a different folder name
git clone <url> <folder_name>

๐Ÿ”„ Fetching and Pulling Updates

Command Description
git fetch Updates all remote tracking branches. Remote tracking branches (like origin/main) show what the branch looks like in the remote repository
git pull <remote_name> <branch> Update the local branch with any updates from the remote repository (on GitHub)
git pull origin main Downloads any new commits from the main branch on origin, and updates the local main branch with those new commits

๐Ÿ”— Set Upstream for Pull

git pull origin main --set-upstream

Sets up a shortcut so that the next time you are on the main branch and run git pull, it will automatically git pull origin main.


๐ŸŒฟ Branching

๐ŸŒฟ Branching = 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.

Command Description
git branch <branch_name> Creates a new branch
git branch feature1 Create a new branch named feature1
git checkout <branch_name> Switch to a different branch and start working on that branch
git checkout feature1 Switch to the feature1 branch. New commits will now be added to the feature1 branch
git branch -D <branch_name> Deletes a branch
git branch -D feature1 Deletes the feature1 branch

๐Ÿ‘‰ HEAD = points to which branch we are currently working on

HEAD -> feature1 = we are currently working on the feature1 branch. Any new commits will be added to the feature1 branch.


๐Ÿ”€ Merging

git merge <branch_name> -m "message"

Merge the current branch (indicated by HEAD ->) with another branch (<branch_name>). Saves the result of the merge as a commit on the current branch.

Example:

git checkout main
git merge feature1 -m "message"
  1. First switch to the main branch
  2. Then merge the main branch with the feature1 branch. The result of the merge will be added to main as a commit (a "merge commit")

โš ๏ธ Merge Conflicts

If there is a merge conflict (git doesn't know what the final code should be), it will add this in your code:

<<<<<<< HEAD
code1
=======
code2
>>>>>>> branch
  • Code between <<<<<<< HEAD and ======= is from the current branch (indicated by HEAD ->)
  • Code between ======= and >>>>>>> branch is from the branch being merged

๐Ÿ”ง Resolving a Merge Conflict

  1. Delete all the extra code and just leave the final code that you want:
<<<<<<< HEAD
code1
=======
code2
>>>>>>> branch

=> code2
  1. If there are conflicts in multiple places in your code, repeat step 1 for all those places.

  2. Create a commit:

git add .
git commit -m "message"

๐Ÿš€ Feature Branch Workflow

A popular process that companies use when adding new features to their software.

1๏ธโƒฃ Create a Feature Branch

git branch new-feature
git checkout new-feature

# Make some changes to the code...
git add .
git commit -m "new feature message"

2๏ธโƒฃ Upload the Feature Branch to GitHub

git push origin new-feature

3๏ธโƒฃ Create a Pull Request on GitHub

A pull request lets teammates do code reviews and add comments.

4๏ธโƒฃ Merge the Feature Branch

Merge the feature branch into the main branch by opening the pull request in the browser and clicking "Merge pull request".

5๏ธโƒฃ Update the Local Repository

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

โš ๏ธ Merge Conflicts in the Feature Branch Workflow

A merge conflict can happen if 2 or more pull requests change the same file and the same line.

We can either:

  1. Resolve the merge conflict on GitHub
  2. Resolve the merge conflict on our computer

๐Ÿ”ง Resolving Merge Conflicts on Your 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

Now the pull request should be ready to merge again.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published