Git is an open-source distributed version control system. In a nutshell, it is used to track changes in any set of files, usually for managing work among developers (programmers) collaboratively developing source code during software development. This helps in speed, data integrity, and support for distributed, non-linear workflows.
Here are some git commands that can help you to accelerate your understanding and usage of git as a developer:
Initializes a new Git repository
git init [project name]
Alternatively, you can create a repository within a new directory by specifying the project name
git add <files>
Adds files to the staging area. You can use
'git add .' to add all modified files.
Undoes the changes to the local files, and restores to the last commit.
Displays the difference between files in two commits or between a commit and your current repository.
It is used to check the state of the staging area and the working directory.
git commit -m "message"
This command is used to commit files (locally) to the repository.
It is used to view the entire commit history.
Used to download existing code from a remote repository.
Used to list all the local branches on the machine.
git branch <branch-name>
Used to create a new branch locally.
git branch -d <branch-name>
Used to delete a branch.
git branch -m <new-name>
Used to rename the current working branch.
git merge <branch-name>
Merges the provided branch with the current working branch.
git checkout <branch-name>
Used to switch from current branch to another one.
git checkout -b <branch-name>
Creates a new branch and switches to the new one.
git push <remote> <branch-name>
Used to save all commits to the remote repository.
git pull <remote>
Used to pull down all the updates from the remote repository.
git rm <file-name>
Used to remove a file from the working directory.
It is used to remove uncommitted changes temporarily.
git stash list
List all the uncommitted changes that were removed temporarily
git stash drop
Discards the changes from the top of the stash stack
git remote –v
Git remote lets you view all remote repositories. The following command will list all connections along with their URLs.
git merge <branch-name>
Used to merge a branch into the active one
git diff --base <file-name>
Lists down conflicts. In order to view conflicts against the base file, use
git rebase master
This is used to apply certain changes from one branch to another.
git show [SHA]
Shows any object in Git in human-readable format
Cleans unnecessary files and optimizes the local repository
performs an integrity check of the git file system and identifies any corrupted objects
git tag <insert-commitID-here>
Marks specific commits. Developers usually use it to mark release points like v1.0 and v2.0.
Learning git commands will help you as a developer to easily control project source code. The learning curve might be steep but hopefully, this GIT cheat sheet will be helpful for you.
Practice those commands and make the most of your developing skills!