Git can be thought of as a database of commits.
A commit is a snapshot of the entire project made of a pointer to this snapshot, metadata(who created it, when it was created, and the commit message), and a pointer backwards to the parent commit. This creates a chain of commits.
Git comrpresses files and if a file doesn’t change between commits, it’s only stored once.
| Config commands | Description |
|---|---|
git config list |
Lists out all of your configs. |
git config set <section>.<key> <value> |
Sets the key for that section. |
git config get <section>.<key> |
Gets the value for that section and key. |
git config unset <section>.<key> |
Removes the key from that section. |
git config remove-section <section> |
Removes a section and all the keys in that section. |
--local level override keys set on the --global level.
--localgit config set --globaluser.nameuser.emailinit.defaultBranch| Status commands | Description |
|---|---|
git status |
Shows current state of your files. |
git add <file/folder> |
Stages a file or folder |
git add -A |
Stages all changes |
git commit -m "message" |
Commits staged changes with a message. |
git commit --amend -m "message" |
Change the last commit message. |
git rm --cached <file/folder> |
Unstages a file/folder. |
.gitignore file to prevent them from being staged.
* matches any number of characters except /. Ex: *.txt ignores any txt files.# are comments./ anchors the patter to the directory containing the .gitignore and not any subdirectories.!A branch is a named pointer to a commit. Committing to a branch updates it to point to the new commit.
| Branch commands | Description |
|---|---|
git branch |
List all branches. * is your current branch. |
git branch -m <oldname> <newname> |
Rename a branch. |
git branch <name> |
Creates a new branch pointing to your current commit. |
git branch <name> <commitHas> |
Creates a new branch pointing to the commit hash. |
git branch -d <name> |
Deletes a branch. |
git switch <branch> |
Switches to a branch. |
git switch -c <name> |
Creates a new branch at the current commit and switches to it. |
switch command is checkout.A merge commit combines changes from two branches into a single commit that has two parent commits.
Before:
A - B - C main(current branch)
\
D - E branch
After:
A - B - C - F main(current branch)
\ /
D - E branch
A in this example.F in this example.| Merge commands | Description |
|---|---|
git merge <branch> |
Creates a merge commit on your current branch from the branch you specified. |
Before:
C branch
/
A - B main(current branch)
After:
branch
A - B - C main(current branch)
Rebase moves the commits from one branch onto the tip of another branch.
Before:
A - B - C main
\
D - E branch(current branch)
After:
A - B - C main
\
D - E branch(current branch)
| Command | Description |
|---|---|
git rebase <branch> |
Rebases the current branch onto the specified branch. |
Example with a merge:
Before:
A - B - C - E - F - G main
\ /
D - H - I branch(current branch)
After:
A - B - C - E - F - G main
\ / \
D H - I branch(current branch)
These commands are used to undo commits.
git reset
git reset --soft <commitHash>
git reset --mixed <commitHash>
git reset --hard <commitHash>
git revert <commitHash>
git log shows the history of commits.
| Flags | Description |
|---|---|
--oneline |
Condensed view of commits. |
--graph |
Draws lines for the path. |
--decorate |
Give branch and tag information. |
--all |
Commits reachable from all branches. |
--parents |
Gives parent commit. |
--author <regex> |
Searches for author. |
git log --oneline --graph --decorate --all
git log <locationName>/<branch>git reflog shows everywhere the head has pointed to, which is useful for reversing resets or rebases.
| Commands | Description |
|---|---|
git clone <https/ssh> |
Copies an existing remote repo to your machine and initializes it. |
git init |
Initializes a new empty local repo. |
git remote -v |
Shows list of locations and their urls. |
git remote add <location> <https/ssh> |
Adds a location. |
git remote remove <location> |
Removes a loction. |
origin is the most common location name for the default remote repository.| Commands | Description |
|---|---|
git fetch <location> |
Updates metadata(trees/folders and blobs/files). Doesn’t update working directory. |
git fetch |
By defualt it fetches from origin. |
git merge <location>/<branch> |
Merges the remote location’s branch onto your current branch. |
git push <location> <branch> |
Sends local changes to any remote location. |
git pull <location> <branch> |
Fetches, then merges what was pulled into your current branch. |
git pull --rebase <location> <branch> |
Fetches, then rebases your changes ontop of what was pulled. |
git pull --rebase instead of git pull to prevent excessive merge commits.
git rebase --abort and pull normally.git push <location> <localBranch>:<remoteBranch>
git push <location> :<remoteBranch>Pull requests are a way to request merging changes from one branch into another on GitHub.
ssh-keygen -t ed25519 -C <githubEmail>
eval "$(ssh-agent -s)"
eval command evaluates the output of ssh-agent -s and sets the necessary environment variables in the current shell session. Host *
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
Host * means that the variables are being applied to all the hosts.AddKeysToAgent yes means the private keys will be automatically added to the ssh-agent when you connect to a remote host.IdentityFile ~/.shh/id_ed25519 sets the default private key for all SSH connections.ssh-add ~/.ssh/id_ed25519 with a new terminal session.ssh -T git@github.com and you should see Successfully Authenticated
eval "$(ssh-agent -s)" in order to start the ssh-agentgit add -Agit commit -m "Message"git push origin maingit pull --rebase origin maingit switch -c <branch>git add -Agit commit -m "message"git push origin <branch>https://youtu.be/ntM7utSjeVU?si=zyk8xC9Pii2EDW4z