Fundamental Git Guide
While working on a project, have you ever thought to yourself "What was that Git command again?". This Git guide has you covered!
Motivation
I believe understanding Git is an essential part of being a Software Engineer so I wanted to create a guide to improve my understanding and help others. Use Ctrl + F to search the guide for your question or topic. (Ex: "How to unstage a file")
General
A git guide which contains commands, explanations and examples for creating, updating and maintaining git repositories.
Website: https://github.com/
Git Download: https://git-scm.com/downloads
Github Desktop: https://desktop.github.com/
Section 1: Creating a repository on Github
Command: echo "# NAME_OF_YOUR_REPO" >> README.md
Explanation: Writes text "NAME_OF_YOUR_REPO" into the README.md file
Command: git init
Explanation: Creates a new git repository. You can begin to start tracking different versions of your project!
Command: git add README.md
Explanation: Adds a change in the working directory to the staging area. In this case, specifically add the README.md file to the “staging area”.
You can repeat this command with different files such as “git add Main.java”
You can also add all files to the staging area using: git add .
Command: git commit -m "first commit"
Explanation: Record a change you’ve made to a file and then store that change in the repository. Commits are referred to as “Snapshots” of your tracked files.
Command: git branch -M main
Explanation: Renames the master branch to main
Command: git remote add origin https://github.com/YOUR_ACCOUNT/YOUR_REPO_HERE.git
Explanation: adds a reference to the remote server in your local project
Command: git push -u origin main
Explanation: Publish your local code changes to a remote repository, specifically the main branch
Picture for reference
Local / Remote Repository Visual Representation
Section 2: How to clone a repository from Github
Command: git clone https://github.com/YOUR_ACCOUNT/YOUR_REPO_HERE.git
Explanation: Make a clone or copy of that repo at in a new directory, or at another location.
Note: Navigate to the desired directory on your computer before cloning repository
Example Directory: C:\Dev\Java
Picture for reference
Steps to clone a repository (HTTPS)
- Go to the Github repository you wish to clone
- Click on "Code"
- Copy and paste the HTTPS Web URL
- Open up Git on your compute
- Navigate to the desired directory
- Use this command to clone the repository: git clone WEB_URL_GOES_HERE
- Example: git clone https://github.com/github/docs.git
Steps to clone a repository (SSH)
Section 3: How to create a .gitignore file
Command: echo > .gitignore
Or
Command: touch .gitignore
Explanation: Creates an empty .gitignore file
Question: What is a .gitignore file?
Answer: Specifies intentionally untracked files that Git should ignore.
More information about .gitignore files can be found here
https://git-scm.com/docs/gitignore
.gitignore file templates can be found here
https://github.com/github/gitignore
Question: Should you commit and push your .gitignore file?
Answer: Yes, you should commit the .gitignore file to your remote repository. This ensures that all contributors to the project are on the same page regarding which files should not be tracked by Git.
Tool to create .gitignore
Enter your tech stack, then press create. Copy and paste the contents of the webpage into your .gitignore file
https://www.toptal.com/developers/gitignore
Section 4: Working Directory
Create files / directories in working directory
Command: echo > FILE_NAME_HERE
Example: echo > fileOne.txt
Or
Command: touch FILE_NAME_HERE
Example: touch fileOne.txt
Explanation: Creates a file in the current directory with no text. This is not a git command.
Command: echo Hello World! > FILE_NAME_HERE.EXTENSION
Examples:
Command: echo Hello World! > fileOne.txt
Command: echo Hello World! > app.go
Command: echo Hello World! > app.js
Command: echo Hello World! > index.html
Explanation: Creates a file in the current directory with text “Hello World!”. You can create all different types of files
Command: echo Hello Again! >> FILE_NAME_HERE
Example: echo Hello Again! >> fileOne.txt
Explanation: Appends “Hello Again!” to the end of a file in the current directory. Or, creates a new file, with the specified file name, if this file does not exist with the text “Hello Again!”
Command: cat FILE_NAME_HERE
Example: cat fileOne.txt
Explanation: View the contents of the file in your terminal. This is not a git command.”
Command: git ls-files
Explanation: How to view all files in directory
Command: mkdir FOLDER_NAME_HERE
Example: mkdir all-text-files
Explanation: How to create a directory (Folder) This is not a git command
Command: git diff FILE_NAME_HERE.txt
Explanation: How to review the changes made to modified a file in working directory
Example: git diff Main.java
Remove files from working directory
Command: rm FILE_NAME_HERE
Explanation: Removes a file named fileOne.txt in the current directory. This is not a git command
Example: rm Main.java
Command: git rm -f FILE_NAME_HERE
Example: git rm -f Main.java
Explanation: Removes a file in the current directory AND from the staging area. This is a git command
Note: Be careful using the -f (force) flag
Move or rename a file(s)
Command: mv ORIGINAL_FILE_NAME NEW_FILE_NAME
Example: mv fileOne.txt fileTwo.txt
Explanation: Rename a file from ORIGINAL_FILE_NAME to NEW_FILE_NAME . This is not a git command
Command: mv FILE_NAME DIRECTORY src/
Example: mv fileOne.txt notes/
Explanation: Move a file to a directory. This is not a git command
Command: git mv ORIGINAL_FILE_NAME NEW_FILE_NAME
Example: git mv fileOne.txt fileTwo.txt
Explanation: Rename a file from ORIGINAL_FILE_NAME to NEW_FILE_NAME. This is a git command and changes are applied to working directory and staging area
Note: File must be added to staging area for this command to work
Add file(s) from working directory to staging area
Command: git add FILE_NAME_HERE
Example: git add fileOne.txt
Explanation: Add file from working directory to staging area
Command: git add FIRST_FILE_NAME_HERE SECOND_FILE_NAME_HERE
Example: git add fileOne.txt fileTwo.txt
Explanation: Add both files from working directory to staging area
Note: File names are separated by a space
Command: git add *.EXTENSION_HERE
Examples:
Command: git add *.txt
Command: git add *.java
Explanation: All files with the specified extension will be added from the working directory to the staging area
Command: git add .
Explanation: Add all the changes from working directory to staging area (Be Careful with this!)
Discarding changes from working directory
Command: git restore FILE_NAME_HERE
Example: git restore fileOne.txt
Explanation: This will undo any local changes in working directory for the file
Command: git restore
Explanation: This will undo any local changes in working directory for all files
Command: git clean -fd
*Explanation: This will remove any newly created files that have not been tracked *
Removing files from Staging area and returning back to working directory
Command: git restore --staged FILE_NAME_HERE
Example: git restore –-staged fileOne.txt
Explanation: Keep your changes to the file, but return file to working directory
Command: git rm--cached FILE_NAME_HERE
Example: git rm –-cached fileOne.txt
Explanation: Keep your changes to the file, but return file to working directory
Note: cached removes from the index aka the staging area
How to delete a file from working directory and staging area
Command: git rm FILE_NAME
Example: git rm fileOne.txt
Explanation: Delete a file from working directory and staging area
Section 5: Commits
How to commit files that are added to staging area
Command: git commit -m “your_commit_message_goes_here”
Example: git commit -m “fixed bug which printed wrong date on customer receipt”
Explanation: Commit a "snapshot" of your Git local repository at one point in time
TIp: The first word of your commit message should be something like “added”, “fixed”, “updated”, “created”, “removed” . Essentially something with an “ed” at the end.
How to combine multiple commits into one commit
Command: git rebase -i HEAD~NUMBER_OF_COMMITS
Example: git rebase -i HEAD~3
Explanation: Opens a Vim terminal allowing you to pick or squash the 3 most recent commits
Tip: If you have many commits, squash some of your commits for a cleaner more readable commit history
Vim Tips
- Press "i" to insert and begin typing.
- Type pick or squash for your commits.
- Press "Esc" when you're done typing
- Press ":" which will allow you to type your final command to write and quit
- Type "wq" (Meaning: Write and Quit)
- Press "Enter"
How to unstage files from a commit
Command: git restore –-staged FILE_NAME_HERE
Example: git restore –-staged fileOne.txt
Explanation: Removes single file from staging. Keeps your changes and returns the modified file to working directory
Command: git restore -–staged FILE_NAME_HERE FILE_NAME_HERE
Example: git restore -–staged fileOne.txt Main.java
Explanation: Removes multiple files from staging. Keeps your changes and returns the modified files to working directory
Command: git restore –-staged .
Explanation: Removes all files from staging area. Keeps your changes and returns the modified files to working directory
How to change file(s) and commit again but keep the same commit message
Command: git add FILE_NAME_HERE
Command: git commit --amend --no-edit
Example: git add fileOne.txt
Example: git commit --amend --no-edit
Explanation: Make the necessary changes to the desired files. Add the files to staging area. Then commit using amend and no edit. This will change your previous commit to include these new changes, while keeping the same commit message
Note: You just committed but you noticed a typo within a file. Go ahead and make your change, then add and commit again. It will look like this exta commit never happened.
How to change / update the commit message (Not making any changes to files)
Command: git commit --amend -m “NEW_COMMIT_MESSAGE_HERE”
Example: git commit --amend -m “Fixed typo in previous commit”
Explanation: You don't need to make any changes to files but want to update your most recent commit message
Note: Your files and code are good, but you accidentally made a typo in your commit message, this is how you can fix it.
How to stash committed changes and pop changes on new branch
Command: git reset HEAD~ --soft
Command: git stash
Command: git checkout name-of-the-desired-branch
Command: git stash pop
Command: git add . # or add individual files
Command: git commit -m "COMMIT_MESSAGE_HERE"
Explanation: You commited on the wrong branch (Ex: Main / Master). You need to move those changes to a different branch.
Note: This example resets HEAD by 1 commit. This command can be altered OR be used multiple times. Use HEAD~num for specific commit
How to view commit history
Command: git log
Explanation: Commits will be sorted from latest(Top of terminal) to earliest (Bottom of terminal). Shows all Commits on all branches
Or
Command: git log –oneline
Explanation: Shows commit history with a short summary
Or
Command: git log –oneline –reverse
Explanation: Shows commit history in reverse (Oldest commit at the top of terminal, Newest at the bottom) with a short summary
How to view changes in a given commit
Command: git show COMMIT_HASH_HERE
Example: git show 7d4590cc6bfdde322653ec1164630803589ed349
Explanation: View Changes made in specific commit within terminal
Or
Command: git show HEAD~Number of commits
Example: git show HEAD~5
Explanation: View Changes made in specific commit within terminal
How to view all files and directories for a given commit
Command: git ls-tree COMMIT_HASH_HERE
Example: git ls-tree 7d4590cc6bfdde322653ec1164630803589ed349
Explanation: Shows blob’s (files), commits, Tags and tree’s (Directories) for a specific commit
Restoring a file to an earlier version
Command: git restore –-source=HEAD~NUMBER_OF_COMMITS FILE_NAME
Example: git restore --source=HEAD~3 fileOne.txt
Explanation: You accidentally deleted an important file. You can recover the file from an earlier commit
Section 6: Branches
Create a branch
Command: git branch BRANCH_NAME
Example: git branch print-out-friend-names
Explanation: Branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter, whatever, you need a new branch to encapsulate your changes.
Note: Do not use spaces in branch names
Checkout a branch
Command: git checkout BRANCH_NAME
Example: git checkout print-out-friend-names
Explanation: Navigate to the specified branche. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.
Create and Checkout a branch at the same time
Command: git checkout -b BRANCH_NAME
Example: git checkout -b print-out-friend-names
Explanation: Allows you to create and checkout a new branch all in one command
Merge a branch
Command: git merge BRANCH_NAME
Example: git merge print-out-friend-names
Explanation: Merges an existing branch into the branch you have checked out
List all local branches
Command: git branch
List all local and remote branches
git branch -a
Section 7: Pushing
Pushing on a new branch for the first time
Command: git push –set-upstream origin BRANCH_NAME
Example: git push --set-upstream origin print-out-friend-name
Explanation: Tells Git to set the upstream branch for the current local branch. The upstream branch is the branch on the remote repository that you want to keep your local branch synchronized with.
Pushing (Not first time)
Command: git push
Explanation: Upload local repository content to a remote repository.
Note: You have added your changes to staging, committed the changes to your local repository, now you want to push your work to the remote repository
Section 8: Pull / Fetch / What's the difference
Command: git pull
Explanation: Used to fetch and download content from a remote repository and immediately update the local repository to match that content
Command: git fetch
Explanation: Used to download commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.
Pull VS Fetch
The most significant difference between "git pull" and "git fetch" is that "git pull" automatically merges the fetched changes into the current branch, while "git fetch" does not. This makes "git pull" a more convenient command if you want to quickly update your local branch with changes from the remote repository
Visual Representation of Pull vs Fetch
Section 9: Pull Requests
Your job / Project may require that you make a Pull Request before your changes can be merged into Main / Master
Explanation: A pull request (PR) is a feature in version control systems like Git that allows developers to propose merging changes from one branch of a repository into another. PRs are also known as merge requests.
Section 10: Viewing Status and changes to files
Show the status of working directory & Staging area
Command: git status
Explanation: Displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
Show the status of working directory & Staging area with short description
Command: git status -s
Explanation: -s is the “short status” flag
View Staged and Unstaged changes
Command: git diff FILE_NAME
Example: git diff fileOne.txt
Explanation: View unstaged changes for a specific file
Command: git diff
Explanation: View all unstaged changes, What is in the working directory VS what is in staging area
Note: Empty response means all files with changes in working directory have been added to staging area. Use the command git diff --staged instead
Command: git diff --staged
Explanation: View staged changes to all files that will be included in next commit
Section 11: Extra helpful commands
Command: git –-version
Explanation: Shows the current version of git you have installed
Command: git –-help
Explanation: Shows a list of helpful git commands
Section 11: Links
Informational
Git Branch Manual: https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-branch.html
Learn Git Branching: https://learngitbranching.js.org/
Different Scenarios: https://ohshitgit.com/
Git Flow: https://www.gitkraken.com/learn/git/git-flow
Practice exercises
Learn Git Branching: https://learngitbranching.js.org/