Git and GitHub Cheat Sheet
PermalinkInitialize a Git Repository
Explanation: Create a new Git repository for your project.
Usage: Initialize a repository in an existing directory or create a new directory.
Actual Command: git init
Example Command:
mkdir vin_project
cd vin_project
git init
PermalinkClone a Repository
Explanation: Copy a remote repository to your local machine.
Usage: Clone a repository from a URL.
Actual Command: git clone <repository_url>
Example Command:
git clone https://github.com/user/repo.git
PermalinkStage Changes
Explanation: Prepare changes for committing.
Usage: Stage files for the next commit.
Actual Command: git add
Example Command:
git add myfile.txt
PermalinkCommit Changes
Explanation: Save staged changes to the repository.
Usage: Create a commit with a descriptive message.
Actual Command: git commit -m "Commit message"
Example Command:
git commit -m "Added a new feature"
PermalinkView Commit History
Explanation: See a list of commits.
Usage: Display commit history.
Actual Command: git log
Example Command:
git log
PermalinkCreate a New Branch
Explanation: Create a new branch for isolating work.
Usage: Start a new branch and switch to it.
Actual Command: git checkout -b <branch_name>
Example Command:
git checkout -b feature-branch
PermalinkSwitch Branches
Explanation: Switch between branches.
Usage: Move to a different branch.
Actual Command: git checkout <branch_name>
Example Command:
git checkout main
PermalinkMerge Branches
Explanation: Combine changes from one branch into another.
Usage: Merge changes from a source branch into a target branch.
Actual Command: git merge <source_branch>
Example Command:
git merge feature-branch
PermalinkPush to a Remote Repository
Explanation: Upload local changes to a remote repository.
Usage: Push your commits to a remote repository.
Actual Command: git push origin <branch_name>
Example Command:
git push origin main
PermalinkPull from a Remote Repository
Explanation: Fetch and merge remote changes into your local repository.
Usage: Update your local repository with changes from a remote repository.
Actual Command: git pull origin <branch_name>
Example Command: git pull origin main
PermalinkCreate a GitHub Repository
Explanation: Create a new repository on GitHub.
Usage: Create a repository on your GitHub account.
Actual Command: Use the GitHub website.
Example:
Go to GitHub. Click "New" to create a repository.
PermalinkAdd a Remote Repository
Explanation: Link your local repository to a remote repository.
Usage: Add a remote repository URL.
Actual Command: git remote add origin <repository_url>
Example Command:
csharp Copy code git remote add origin https://github.com/user/repo.git
PermalinkFork a Repository
Explanation: Create a personal copy of someone else's repository.
Usage: Fork a repository on GitHub.
Actual Command: Use the GitHub website.
Example:
Go to the GitHub repository you want to fork. Click "Fork" in the top-right corner.
PermalinkCreate a Pull Request
Explanation: Propose changes to a repository by submitting a pull request.
Usage: Create a pull request on GitHub.
Actual Command: Use the GitHub website.
Example:
Go to your forked repository. Click "New Pull Request" and follow the prompts. Merge a Pull Request Explanation: Accept and merge changes from a pull request.
Usage: Merge a pull request on GitHub.
Actual Command: Use the GitHub website.
Example:
Go to the pull request you want to merge. Click "Merge Pull Request" and confirm.
PermalinkGit workflows and best practices that are widely adopted:
Feature Branch Workflow:
Create a new branch for each new feature or bug fix.
Keep each branch focused on a specific task.
Regularly merge the main branch (usually
master
ormain
) into your feature branch to keep it up to date.When the feature is complete, merge the feature branch back into the main branch.
Gitflow Workflow:
Similar to the Feature Branch Workflow but with a more structured approach.
Uses specific branches like
feature
,develop
,release
, andhotfix
to manage different aspects of development.Good for larger teams or projects with frequent releases.
Pull Request (PR) Workflow:
Developers work on their own feature branches.
When ready to merge, they open a pull request.
Code review and testing are done in the pull request before merging into the main branch.
This workflow adds an extra layer of quality control.
Forking Workflow:
Each developer forks the main repository into their own personal repository.
They clone their fork, create feature branches, and make changes.
When ready to contribute, they create a pull request to the main repository.
This is common in open-source projects.
Commit Guidelines:
Write clear and concise commit messages that explain the purpose of the commit.
Use imperative mood (e.g., "Add feature" instead of "Added feature").
Keep commits small and focused on a single change.
Branch Naming Conventions:
Use consistent and descriptive branch names.
Prefix branches with categories like
feature/
,bugfix/
, orhotfix/
.
Git Tags:
Use tags to mark significant points in your project's history, like releases (e.g.,
v1.0
).Tags make it easy to reference specific versions of your code.
Gitignore:
- Maintain a
.gitignore
file to specify files or directories that should be excluded from version control (e.g., build artifacts, temporary files).
- Maintain a
Regularly Pull and Push:
Frequently pull changes from the main branch to stay up to date.
Regularly push your own changes to the remote repository to ensure your work is backed up.
Documentation:
Maintain clear and up-to-date documentation for your project.
Include information on how to set up the development environment, run tests, and contribute.
PermalinkCheat sheet on Linux.
pwd - To check the print working directory. It prints the path of the working directory, starting from the root.
ls - To list directories.
ls -a - To list all files and directories, including hidden files.
mkdir - To make a new directory.
rmdir - To remove a directory/ Folder.
vim - To create a fruits.txt file and to view the content.
cat - To view what's written in a file.
head -3 file_name - To Show only the top three fruits from the file.
tail -3 file_name - To Show only the bottom three fruits from the file.
diff f1 f2 - To find the difference between f1 and f2 files.
chmod - To change the access permissions of files.
chown - The owner of the file or application.
chgrp - The group that owns the file or application.
history - To check which commands you have run till now.
Cron - Cron is generally used for running scheduled backups, monitoring disk space, deleting files periodically which are no longer required, running system maintenance tasks and a lot more.
CronTab - We can define multiple jobs, for their scheduled time and the account under which the job should run.
crontab -e - To select an editor and open corntabEdits a copy of the user's crontab file.
crontab -l \ - Lists the user's crontab file.
crontab -r - Removes the user's crontab file from the crontab directory.
ls -ltr - To see the details of the files.
File permissions:
Read (r) - Allows reading or viewing the content of the file. For directories, it allows listing the contents of the directory.
Write (w) - Permits modifying the file's content or deleting the file. For directories, it allows adding, removing, or renaming files within the directory.
Execute (x) - Enables executing the file if it is a script or a binary executable file. For directories, it allows accessing the contents of the directory.
ACLs - (Access Control Lists) are represented using an extended syntax that includes additional access permissions and qualifiers, such as read-data, write-data, execute, delete, and more.
getfacl - a command displays ACL information.
setfacl - a command that allows you to modify ACLs.
systemctl - is a command-line tool used to control the systemd system and service manager, which is the default initialization system in most modern Linux distributions. It allows users to start, stop, restart, enable, disable, and manage system services, targets (similar to runlevels), and other units managed by systemd.
service - is a legacy command used to control system services in older Linux distributions that do not use systemd as their default initialization system. It is also used in some current distributions for compatibility purposes. While it can perform similar actions to
systemctl
, its syntax and capabilities are more limited.
PermalinkCheat sheet on Git-GitHub.
sudo apt install git: To install it with Ubuntu.
git init: To Initialize a new Git repository in the current directory.
git --version: To confirm that you have installed Git.
git clone <repository-url>: To clone the remote repo to the local repo.
git status: To check the current status of the repo.
git add <file_name>: To add a file to the staging area to be included in the next commit.
git commit -m <Commit Message>: Make a commit by taking the updated working tree contents of the paths specified on the command line.
git push -u origin main: To push the local commits to a remote repository.
git revert <commit-id>: To remove or edit changes you’ve made in the code in previous commits.
git rebase <branch name>: To remove or edit changes you’ve made in the code in previous commits.
git merge: Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.
git stash: Use git stash to save the changes without committing them.
git log --oneline: To print a single commit in a single line as output when the git log command is executed using this option.
git checkout <branch_name>: Command lets you navigate between the branches created by git branch.
git cherry-pick <commit_id>: To apply changes from a specific commit to the current branch.
git push origin --delete <branch>: To fully delete a remote branch on GitHub.
I hope you enjoy the blog post!
If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I’ll be more than happy to help!