curriculum/challenges/english/blocks/review-git/6724e4cfea0c4f2425a9d064.md
git --version
If you see a version number, that means Git is installed. If not, then you will need to install it.
For Linux systems, Git often comes preinstalled with most distros. If you do not have Git pre-installed, you should be able to install it with your package manager commands such as sudo apt-get install git or sudo pacman -S git.
For Mac users, you can install Git via Homebrew with brew install git, or you can download the executable installer from Git's website.
For Windows, you can download the executable installer from Git's website. Or, if you have set up Chocolatey, you can run choco install git.install in PowerShell. Note that on Windows, you may also want to download Git Bash so you have a Unix-like shell environment available.
To make sure the installation worked, run the git --version command again in the terminal.
git config is used to set configuration variables that are responsible for how Git operates on your machine. To view your current setting variables and where they are stored on your system, you can run the following command:git config --list --show-origin
Right now you should be seeing only system-level configuration settings if you just installed Git for the first time.
To set your user name, you can run the following command:
git config --global user.name "Jane Doe"
The --global flag is used here to set the user name for all projects on your system that use Git. If you need to override the user name for a particular project, then you can run the command in that particular project directory without the --global flag.
To set the user email address, you can run the following command:
git config --global user.email [email protected]
Another configuration you can set is the preferred editor you want Git to use. Here is an example of how to set your preferred editor to Emacs:
git config --global core.editor emacs
If you choose not to set a preferred editor, then Git will default to your system's default editor.
git init: This will initialize an empty Git repository so Git can begin tracking changes for this project. When you initialize an empty Git repository to a project, a new .git hidden directory will be added. This .git directory contains important information for Git to manage your project.git status: This command is used to show the current state of your working directory - you will be using this command a lot in your workflow.git add: This command is used to stage your changes. Anything in the staging area will be added for the next commit. If you want to stage all unstaged changes, then you can use git add . The period (.) is an alias for the current directory you are in.git commit: This command is used to commit your changes. A commit is a snapshot of your project state at that given time. If you run git commit, it will open up your preferred editor you set in the Git configuration. Once the editor is open, you can provide a detailed message of your changes. You can also choose to provide a shorter message by using the git commit -m command like this:git commit -m "short message goes here"
git log: This will list all prior commits with helpful information like the author, date of commit, commit message and commit hash. The commit hash is a long string which serves as a unique identifier for a commit.git remote add: This command is used to setup the remote connection to your remote repo.git push: This command is used to push up your changes to a remote repository.git pull: This command is used to pull down the latest changes from your remote repository into your local repository.git clone: This command will clone a repository. This means you will have a copy of the repository. This copy includes the repository history, all files/folders and commits on your local device.git remote -v: This command will show the list of remote repositories associated with your local Git repository.git branch: This command will list all of your local branches.git fetch upstream: This command tells Git to go get the latest changes that are on your upstream remote (which is the original repo).git merge upstream/main: This command tells Git to merge the latest changes from the main branch in the upstream remote into your current branch.git reset: This command allows you to reset the current state of a branch. Passing the --hard flag tells Git to force the local files to match the branch state. This ensures that you have a clean slate to work from.git rebase: A rebase in Git is a way to move or combine a sequence of commits from one branch onto another.main branch will often represent the primary or production branch in a real world application. Developer teams will create multiple branches for new features and bug fixes and then merge those changes back into the main branch.git branch feature
To checkout that branch, you can run the following command:
git checkout feature
Most developers will use the shorthand command for creating and checking out a branch which is the following:
git checkout -b new-branch-name
A newer and alternative command would be the git switch command. Here is an example for creating and switching to a new branch:
git switch -c new-branch-name
main branch is your default branch and usually is pretty stable. So it is best to branch off from there to create new branches for items like bug fixes, new features, or other miscellaneous work..gitignore file..gitignore Files.gitignore file is a special type of file related to Git operations. The name suggests that this file is used to tell Git to ignore things, and that's the common use case. But what it actually does is it tells Git to stop tracking a file."New Repository" button and walk through the GitHub UI of setting up a new repository.git init).git status command to see all changes made that are being tracked by git.git add).git commit).git remote add).git push).To generate a GPG key, you'll need to run:
gpg --full-generate-key
ssh utility. You can also use an SSH key to sign commits.For an SSH key, you'll run:
ssh-keygen -t ed25519 -C "[email protected]"
ed25519 is a modern public-key signature algorithm.
gpg --list-secret-keys --keyid-format=long
Then, to get the public key, use:
gpg --armor --export "<key id>"
Then, take the short ID you got from listing the keys and run this command to set it as your git signing key:
git config --global user.signingkey <your_gpg_key_id>
Then, you can pass the -S flag to your git commit command to sign a specific commit - you'll need to provide your passphrase. Alternatively, if you want to sign every commit automatically, you can set the autosign config to true:
git config --global commit.gpgsign true
git config --global gpg.format ssh
Then, to set the signing key, you'll pass the file path instead of an ID:
git config --global user.signingkey <path_to_your_ssh_keys>
Review Git topics and concepts.