Git is an essential revision control system for programmers because it aids in managing changes as well as enhance collaborative work. For beginner level programmers, learning to using git on the command line could be a necessary first step but memorizing every command isn't quite simple.
Although there are Git GUI software for the Windows and Mac OS such as SmartGit, learning to use the command line is recommended. On the git bash command line, every git command must start with the keyword "git".
Here are some basic Git commands with examples. For full list of git command, click here.
git config: is used to configure your username, email, file format, etc.
git config --global user.name "Name" git config --global user.email "email@example.com"
git init: when you create a new project, you have to initialize it on git before you can add and commit it. This will create the ".git" directory in your project folder. To initialize a git repository:
git clone: to replicate a repo from the remote source on your local machine, you use the clone command. When you clone a repository, it allows you to update your local files from it as well as push your changes to the repo if you have the permissions.
git clone firstname.lastname@example.org:owner/opensource.git
git add: is used to add your new file or changes to your git index so that it can be tracked.
git add .
git rm: removes files from your index and your working directory so they will not be tracked.
git rm filename
git status: compares the status of files in the index and the working directory and display the differences. It gives you the current status of your project files by showing you files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).
git commit: creates a new commit object pointing to changes written in the index and sets the branch to point to that new commit. Git commit requires a short descriptive message of your changes. Unlike the command, the message must be in quote.
git commit -m "php code changes commit"
To add and commit your changes in git with one command, use:
git commit -a -m "adding and committing changes with a single command"
git fetch: fetches and downloads all the files from the remote repository that are not present in the local one.
git fetch origin
git pull: is used to update the local folder with the changes on the remote repository. The command fetches the files from remote server and merges with the local one.
git pull origin
git push: pushes all the changes made on the local working environment to the remote repository and advances its branches.
git push origin master
git remote: shows all the remote versions of your repository.
git remote origin
git branch: lists all existing branches, including remote branches if ‘-a’ is provided. To create a new branch add the name to the command.
git branch -a git branch newbranch
git checkout: is used to switch between available working branches. You can as well use this command to create a new branch and switch to it with one command.
git checkout git checkout newbranch
git diff: generates statistical differences between paths or files in your git repository, or your index or your working directory. The details can be saved as a file in your repo.
git diff git diff>filename.extension git diff compare-branch>filename.ext
git merge: merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.
git merge branch
git reset: resets your index and working directory to the state of your last commit.
git reset --hard
git log: shows list of commits on a branch with corresponding details.
git log commit 76f241e8a0d768fb37ff4ad40e294b61a99a0iko Author: User <email@example.com> Date: Wed August 23 07:30:10 2017 +0300 first commit
git stash: is used to temporarily save changes without committing it immediately. The changes can be applied later.
git stash working-directory (To restore stashed files) git stash apply
git tag: tags a specific commit with a simple, human readable handle that never moves.
git tag -a v8.x -m 'this is a special tag'
git show: shows information about a git object.
git show commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe Author: User <firstname.lastname@example.org> Date: Wed Aug 23 07:30:10 2017 +0300 first commit diff --git a/README b/README new file mode 100644 index 0000000..e69de29
For full list of git commands, check the official git documentation.