Here you will learn some basic Git commands and in the end, I am providing you with a free cheat sheet that you can download and use when necessary.
Git is the most widely used open-source version control system that allows you to track changes made to files. Companies and programmers usually use Git to collaborate on developing software and applications.
A Git project consists of three major sections: the working directory, the staging area, and the git directory.
The working directory is where you add, delete, and edit the files. Then, the changes are staged (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved into the git directory.
Here are some basic git commands you need to know.
Config - Used to set user-specific configuration values like email, username, file format, etc.
git config --global user.name [name] git config --global user.email [email]
Config List - To check all of your configuration settings.
git config --list
Init - Creates a new local git repository in the current directory.
You can also create a repository within a new directory by specifying the project name.
git init [project-name]
Clone - Used to copy a repository from a remote server.
git clone username@host:/path/to/repository
Add - Used to add files to the staging area. You can add all files at once with the command shown below.
git add .
Or you can specify exact file names to add in the staging area.
git add [file-name-1] [file-name-2]
To remove files from the repository use the command below.
git add rm --cached [file-name]
Reset - To reset the file to the last commit's state use the following command.
Status - Displays the list of changed files together with the files that are yet to be staged or committed.
Commit - Creates a snapshot of the changes and save it to the git directory.
git commit -m "Write commit message here."
Reset - This command will reset the index and the working directory to the last git commit's state.
git reset --hard HEAD
Commit amend - Convenient way to modify the most recent commit.
git commit --amend -m "Commit message"
Push - Used to send local commits to the master branch of the remote repository. Replace [master] with the branch where you want to push your changes when you're not intending to push to the master branch.
git push origin [master]
Pull - Merges all the changes present in the remote repository to the local working directory.
Remote - Connect local repository to a remote server.
git remote add origin [remote-url]
To delete a connection with the remote repository use command below.
git remote rm [repository-name]
Branch - List all the present branches in the repository.
Or you can delete a branch with the next command.
git branch -d [branch-name]
Checkout - Creates branches and helps you to navigate between them. For example, the following basic command creates a new branch and automatically switches you to it.
git checkout -b [branch-name]
To switch branches use the command below.
git checkout [branch-name]
Merge - Used to merge a branch into the active one.
git merge [branch-name]