This repository contains the cheat sheet for the Day 1 session of Git-Learning Version Control. Fork it and you are good to go 💻 !
-
Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help software teams manage changes to source code over time.
-
Software teams that do not use any form of version control often run into problems like not knowing which changes that have been made are available to users.
-
A complete long-term change history of every file.This means every change made by many individuals over the years. They also include the author, date and written notes on the purpose of each change.
-
Branching and merging. Having team members work concurrently is a no-brainer, but even individuals working on their own can benefit from the ability to work on independent streams of changes.
-
And many other different features!
Just go to https://git-scm.com/download/win and the download will start automatically. Apply the steps described in the session to install.
For a Debian-based distribution, such as Ubuntu
sudo apt install git-all
The above command will install all sub-packages including a GUI and tree visualizer. If you only want to include main components with minimal dependencies then you should rather use this command!.
sudo apt install git
For Fedora (or any closely-related RPM-based distribution, such as RHEL or CentOS)
sudo dnf install git-all
For other options, there are instructions for installing on several different Unix distributions on the Git website, at https://git-scm.com/download/linux.
A macOS Git installer is maintained and available for download at the Git website, at https://git-scm.com/download/mac. Follow the steps mentioned ont the website after that.
The git config command changes the configuration options in your Git installation. It is often used to set your Git email, editor, and any aliases you want to use with the git command.
- Setting up username
git config --global user.name "username"
- Seting up Email
git config --global user.email "[email protected]"
- View your configuration file
git config --list
A. By Adding SSH Tokens
GitHub offers a convenient SSH key feature for accessing your repositories on the command line. Instead of using usernames and passwords every time you push to or pull from your repository, you can use your key.
-
Create a new SSH: Type
ssh-keygen -t -b 4096 -C "<your email here>"
in your terminal, then press enter. -
It will ask for a new passphrase. Enter and then confirm a new passphrase.
-
Without changing the path of your terminal, type
eval $(ssh-agent -s)
and press enter. -
Next, type
ssh-add ~/.ssh/id_rsa
, and press enter. -
Go to the present file location manually using File manager and copy the file named
id_rsa.pub
, or you can also typeclip < ~/.ssh/id_rsa.pub
, if you have windows installed. -
Go to github.com and login to your account.
-
Go to
settings>SSH and GPG keys
-
Click on
NEW SSH KEY
-
Choose a Title and paste the SSH key in the Box.
-
You are all caught up!
B. By adding Personal Access Tokens (PAT)
You can also refer to this article: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
-
Login to your account on github.com
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the left sidebar, click Developer settings.
-
In the left sidebar, click Personal access tokens.
-
Click Generate new token.
-
Give your token a descriptive name.
-
To give your token an expiration, select the Expiration drop-down menu, then click a default or use the calendar picker.
-
Select the scopes, or permissions, you'd like to grant this token. To use your token to access repositories from the command line, select repo.
-
Click Generate token.
-
REMEMBER
(i) By using PAT, you'll have to enter the token everytime you try to perform any operation. Here is an example of how you'll use it while cloning:
Example:
$ git clone https://github.com/username/repo.git Username: your_username Password: your_token
(ii) If you donot wish to enter your PAT everytime, enter this line before using any command(like the one shown above in the example). After this you will have to enter the your PAT just one last time as it will cache your credential for the next use.
git config --global credential.helper cache
See where Git is located:
which git
Get the version of Git:
git --version
Create an alias (shortcut) for git status
:
git config --global alias.st status
Help:
git help
Initialize Git:
git init
Get everything ready to commit:
git add .
Get custom file ready to commit:
git add index.html
Pull:
git pull
Pull specific branch:
git pull origin branchname
Commit changes:
git commit -m "Message"
Commit changes with title and description:
git commit -m "Title" -m "Description..."
Add and commit in one step:
git commit -am "Message"
Remove files from Git:
git rm index.html
Update all changes:
git add -u
Remove file but do not track anymore:
git rm --cached index.html
Show branches:
git branch
Create branch:
git branch branchname
Change to branch:
git checkout branchname
Create and change to new branch:
git checkout -b branchname
Clone to localhost:
git clone https://github.com/user/project.git
or:
git clone ssh://[email protected]/~/dir/.git
Clone to localhost folder:
git clone https://github.com/user/project.git ~/dir/folder
Push (set default with -u
):
git push -u origin master
Push:
git push origin master
Force-Push: `git push origin master --force
They allow you to write shortcuts for common commands that you write.
Example:
git config --global alias.co commit
Every time we run git co, the git commit command will be run!
Show folder content: ls -la
Show the path to the present working dirctory: pwd
Create directory: mkdir <file_name>
Change directory: cd <file_name/file_location>