- ๐ป Command Line Basics
- ๐ Creating Commits
- ๐ Working Area vs Staging Area
- ๐ Viewing Previous Commits
- โฎ๏ธ Restoring to a Previous Commit
- โ๏ธ Other Features
- ๐ GitHub
- โฌ๏ธ Uploading Code to GitHub
- โฌ๏ธ Downloading Code from GitHub
- ๐ฟ Branching
- ๐ Merging
โ ๏ธ Merge Conflicts- ๐ Feature Branch Workflow
| 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.
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 |
git config --global user.name "Your Name"
git config --global user.email "email@example.com"- ๐ 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>
| 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 |
git checkout <commit_hash|branch_name>๐ Key Concepts:
- ๐ณ
master= branch name- You can
git checkoutbranch - Always points to latest commit on the branch
- You can
- ๐
HEAD= indicates which commit you are currently viewing
| 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 |
git config --global alias.shortcut <command>
git config --global alias.s "status"
# Now: git s = git statusTell git which files/folders it SHOULD NOT track.
rm -rf .git๐ฆ 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)
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/repository1The above command links a local repository to a GitHub repository and gives it a name "origin".
| 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" |
git config --global credential.username <username>Configure your GitHub username so you can get access to your Github repository.
| 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" |
git push <remote_name> <branch> --set-upstream
git push origin main --set-upstreamSets 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.
git push <remote_name> <branch> -fForce-push the branch to the remote repository (it will overwrite what's on the remote 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>| 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 |
git pull origin main --set-upstreamSets 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 = 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.
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"- First switch to the
mainbranch - Then merge the
mainbranch with thefeature1branch. The result of the merge will be added tomainas a commit (a "merge commit")
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
<<<<<<< HEADand=======is from the current branch (indicated byHEAD ->) - Code between
=======and>>>>>>> branchis from the branch being merged
- Delete all the extra code and just leave the final code that you want:
<<<<<<< HEAD
code1
=======
code2
>>>>>>> branch
=> code2
-
If there are conflicts in multiple places in your code, repeat step 1 for all those places.
-
Create a commit:
git add .
git commit -m "message"A popular process that companies use when adding new features to their software.
git branch new-feature
git checkout new-feature
# Make some changes to the code...
git add .
git commit -m "new feature message"git push origin new-featureA 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 mainA merge conflict can happen if 2 or more pull requests change the same file and the same line.
We can either:
- Resolve the merge conflict on GitHub
- Resolve the merge conflict on our computer
1๏ธโฃ Get the latest updates from main:
git checkout main
git pull origin main2๏ธโฃ Get the latest updates from the feature branch:
git checkout feature4
git pull origin feature43๏ธโฃ 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 main4๏ธโฃ Push the resolved feature branch to GitHub:
git push origin feature4Now the pull request should be ready to merge again.