Git Commands
What Git is?
Git is an open-source version control system that works locally to help developers work together on software projects that matter.
Initializing
Starting up Git within a project and getting it connected.
Initializes (or starts) your current working directory (folder) as a Git repository (repo).
git init
Copies an existing Git repo hosted remotely.
git clone https://www.github.com/username/repo-name
Shows your current Git directory’s remote repo. Use the -v flag for more info.
git remote or git remote -v
Adds the Git upstream to a URL
git remote add upstream https://www.github.com/username/repo-name
Branching
Means: Isolating work and managing feature development in one place.
Lists all current branches. An asterisk (*) will appear next to your currently active branch.
git branch
Creates a new branch. You will remain on your currently active branch until you switch to the new one.
git branch new-branch
Switch to any existing branch and checks it out into your current working directory.
git checkout another-branch
Consolidates the creation and checkout of a new branch.
git checkout -b new-branch
Deletes a branch.
git branch -d branch-name
Staging
Means: Creating files staged after modifying a file and marking it ready to go in the next commit.
Checks the status of your Git repo, including files added that are not staged.
git status
Stages modified files. If you make changes that you want to be included in the next commit, you can run add again. Use
git add .
for all files to be staged, or specify specific files by name.git add .
or
add my_script.js
Removes a file from staging while retaining changes within your working directory
git reset my_script.js
Committing
Means: Recording changes made to the repo.
Commits staged files with a meaningful commit message so that you and others can track commits.
git commit -am "Commit message
Condenses all tracked files by committing them in one step.
git commit -m "Commit message
Modifies your commit message.
git commit --amend -m New commit message
Collaborating and Sharing
Means: Downloading changes from another repository or sharing changes with the larger codebase.
Pushes or sends your local branch commits to the remote repo. Note: some repos use master instead of main in their commands.
git push origin main
Fetches and merges any commits from the tracking remote branch.
git pull
Merges the fetched commits.
git merge upstream/main
Showing Changes
Means: See changes between commits, branches, and more.
Compares modified files that are in the staging area.
git diff --staged
Displays the difference between what is in a-branch but is not in b-branch.
git diff a-branch..b-branch
Uses commit id to show the diff between two specific commits.
git diff 61ce3e6..e221d9c