Git Cheatsheet
Git is a distributed version control system that records changes to source code over time, so you can revisit or recover any version later. Developers use it to collaborate on shared codebases, track history, and safely test new features in branches. Git is the most widely used version control system and is required for working with GitHub, GitLab, and most modern deployment pipelines.
What is Git?
Git is a version control system that allows you to keep track of changes made to a project over time.
What is GitHub?
GitHub is a popular hosting service for Git repositories. GitHub allows you to store your local Git repositories in the cloud. With GitHub, you can backup your files, share your code, and collaborate with others.
Step 1: Install Git for Mac
To install Git on Mac, we first need to install Homebrew.
Step 1.1: Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Step 1.2: Install Git
brew install git
Step 2: Configure Git and Github
Step 2.1: Set up Git
For Git to work properly, we need to configure Git with the following commands. Enter your information inside the quotes. It’s a good idea to use the same email as your Github account email.
git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"
To verify that the output matches your name and email address.
git config --get user.name
git config --get user.email
Step 2.2: Create Github account or Sign in
Go to GitHub.com and create an account. If you already have one, sign in.
Step 2.3: Create SSH Key
Check if you have an SSH key
ls ~/.ssh/id_rsa.pub
If you receive a message “No such file or directory”, you’ll need to create a new one. Create SSH To create an SSH key, run the following command
ssh-keygen -C <youremail>
- When it asks a location to save the generated key, press Enter.
- Next, it will ask you for a password; enter one if you wish, but it’s not required.
Step 2.4: Link Your SSH Key with GitHub
- Log into GitHub and click Settings
- Click SSH and GPG keys and click New SSH Key
- Run this following command in your terminal, copy the output into the key field and click click Add SSH key
cat ~/.ssh/id_rsa.pub
- Follow this Github article to test your key
Git Project
A Git project has three parts:
- A Working Directory: where you’ll be doing all the work: creating, editing, deleting, and organizing files
- A Staging Area: where you’ll list changes you make to the working directory
- A Repository: where Git permanently stores those changes as different versions of the project
Git commands
git initinitialize a new Git repositorygit statuscheck the status of changesgit add filenameadds files from the working directory to the staging areagit add filename_1 filename_2add multiple files to the staging area with a single command:git diff filenameshows the difference between the working directory and the staging areagit commitpermanently stores changes from the staging area inside the repository. The option-mfollowed by a message.
git commit -m "First commit"
Standard commit messages:
- Must be in quotation marks
- Written in the present tense
- Should be brief (50 characters or less) when using -m
git log shows a list of all previous commits. The message includes
- A 40-character code, called an SHA, uniquely identifies the commit. This appears in orange text.
- The committed author
- The date and time of the commit
- The commit message
Backtrack Git
In Git, a HEAD commit is the commit you’re currently on.
git checkout HEAD filenamerestores the file in your working directory to look exactly as it did when you last made a commit. We can also usegit checkout -- filenamegit reset HEAD filenameunstages file changes in the staging area.git reset commit_SHAresets to a previous commit in your commit history. This command works by using the first 7 characters of the SHA of a previous commit.
Git branch
Git allows users to create branches to experiment with versions of a project. To list all a Git project’s branches, use this command git branch.
Create a new branch
git branch new_branch
Switch to the new branch
git checkout branch_name
Commit on a new branch
All the commands you do on main branch, you can also do on this branch.
- To add files to the staging area
git add filename - To commit
git commit -m "Commit message"
Merge
Firstly, we need to switch to the main branch git checkout main
Then use this command to merge the new branch to a main branch git merge branch_name.
Delete branch
After the branch has been integrated into the main, you can delete it.
git branch -d branch_name
If the feature branch is never merged into main branch, we use the -D option.
git branch -D branchname
Git workflow on your own
- Initiate Git or Clone GitHub to a local machine.
git clone https://github.com/USER_NAME/REPOSITORY_NAME.git
- Create a branch to work on a new project feature and push it to the server:
git branch <name of the branch>
git push -u origin <name of the branch>
For example, if your branch is “develop”, you can enter the following command.
git branch develop
git push -u origin develop
- Switch to a the branch
git checkout <name of the branch>
- Make changes to the working files
- Commit changes:
git status
git add --a
git commit -m "Add your comment here"
- After committing, push branch to remote:
git checkout main
git merge <name of the branch>
git push -u- origin main
Git Teamwork
A remote (Git repository) can live on the web, on a shared network, or even in a separate folder on your local computer.
git clone
To create a local copy of a remote:
git clone remote_location clone_name
remote_locationtells Git where to go to find the remote.clone_nameis the name you give to the directory in which Git will clone the repository
git remote
See a list of a Git project’s remotes with the command: git remote -v
git fetch
To fetch work from the remote into the local copy: git fetch
git merge
To merge origin/main into your local branch: git merge origin/main
git push
To push a local branch to the origin remote: git push origin <branch_name>.
Git workflow teamwork
- Fetch and merge changes from the remote
- Create a branch to work on a new project feature
- Develop the feature on your branch and commit your work
- Fetch and merge from the remote again (in case new commits were made while you were working)
- Push your branch up to the remote for review
Resources: