Git
Git is a distributed version control system. It is used to track changes in source code and manage different versions of the same project.
Installation
scoop install gitUsage & Commands
git clone
Clone a repository to your local machine.
git clone https://github.com/<your-username>/<your-repo>.gitgit add
Add all changes to the staging area.
git add .
# or
git add -AAdd a specific file/directory to the staging area.
git add *filename/path*TIP
(*) stands for a wildcard and will match any file or directory.
Running git add * or git add . will add all modified/new files and
directories to the staging area.
git restore
Restore a file/directory from the staging area (git add-ed file/s).
git restore --staged *filename/path*May also be used to restore a file/directory from the working directory. ("Discard changes")
git restore *filename/path*TIP
Same as git add, you can use wildcards to restore multiple files/directories.
git commit
Commit all changes to the local repository.
git commit -m "commit message"git push
Push all commits to the remote repository.
git pushPush a specific branch to the remote repository.
git push origin <branch-name>git pull
Pull all commits from the remote repository.
git pullPull a specific branch from the remote repository.
git pull origin <branch-name>Flags
Flags that can be used with git pull in specific scenarios.
--rebase: Rebase the current branch on top of the upstream branch.--autostash: Automatically stash/unstash changes before/after a pull.
git log
Show the commit history.
git logFlags
Flags that can be used with git log in specific scenarios.
--oneline: Show the commit history in a single line.
git reflog
I rarely use this command, but in cases like when you accidentally delete a branch without pushing it to the remote repository, you can use this command to restore the deleted branch.
git refloggit rebase
Rebase the currently active branch.
git rebase <branch name># Assuming we're we want to rebase our current
# branch to the latest changes from the main
git rebase mainIf we're on a different branch, we can specify the branch name and we want rebase a specific branch, we can use the following command:
git rebase <branch name> <branch name>git branch
List all branches.
git branchCreate a new branch.
git branch <branch name>Delete a branch.
git branch -d <branch name>
# or
git branch -D <branch name>Rename a branch.
git branch -m <old branch name> <new branch name>
# or if we're on the branch
git branch -m <new branch name>git checkout
Switch to an existing branch.
git checkout <branch name>We can also checkout to a specific commit by copying the commit hash from
the commit history using git log or git reflog.
git switch
Is a somewhat more advanced version of git checkout.
Switch to an existing branch.
git switch <branch name>
# it is essentially the same as
# git checkout <branch name>Unlike git checkout, switch can bring local changes to the branch you're
switching to.
Flags
Flags that can be used with git switch in specific scenarios.
-c: Create a new branch if it doesn't exist and switch to it. (It is essentially the same as creating a new branch branch usinggit branchand then switching to it usinggit checkout.)
git stash
Stash changes.
git stashFlags
Flags that can be used with git stash in specific scenarios.
-u: Include untracked (new) files in the stash.
git stash pop
Readd the changes from the stash.
git stash popDevelopment workflow
- Clone a repo using
git clone - After making changes, use
git addto add the changes to the staging area - Switch to a new branch using
git switch -c <branch name> - Make sure the
main/masterbranch by runninggit fetch origin main:main(This command is not on the list above, as we will only use it this one time).- or if you want to update a different branch, run
git fetch origin <branch name>:<branch name>
- or if you want to update a different branch, run
- Use
git rebase mainto rebase the current branch on top of themainbranch. - Use
git pushto push the changes to the remote repository