Basic GIT Commands
Here are some basic GIT commands you need to know:
git init
git init will create a new local GIT repository. The following Git command will create a repository in the current directory:
git init
Alternatively, you can create a repository within a new directory by specifying the project name:
git init [project name]
git clone
git clone is used to clone a existing repository into your computer. The command for this is:
git clone [repository url]
If the repository lies on a remote server, use:
git clone username@host:/path/to/repository
Conversely, run the following basic command to copy a local repository:
git clone /path/to/repository
git branch
git branch will list, create, or delete branches. For instance, if you want to list all the branches present in the repository, the command should look like this:
git branch
If you want to create a new branch named test using the following command:
git branch test
If you want to delete a branch named test using the following command::
git branch –d test
git checkout
git checkout creates branches and helps you to navigate between them. For example, the following basic command creates a new branch named test and automatically switches you to it:
git checkout -b test
To just switch from one branch to another, use the following command:
git checkout <branch-name>
git add
git add is used to add files to the staging area. The staging area is there to keep track of all the files which are to be committed. Any file which is not added to the staging area will not be committed. This gives the developer control over which files need to be committed.
For example, the basic Git following command will index the temp.txt file:
git add temp.txt
In case you want to add multiple files you can use:
git add file1 file2 file3
If you want to add all the files inside your project folder to the staging area, use the following command:
git add .
Use this carefully since it adds all the files and folders in your project to the staging area.
git commit
Committing is the process in which the code is added to the local repository. git commit will create a snapshot of the changes and save it to the git directory.
git commit –m "message to go with this commit"
Note that any committed changes won’t make their way to the remote repository.
git status
Use git status
to find out information regarding what files are modified and what files are there in the staging area.
git push
git push is used to send local commits to the master branch of the remote repository. Here’s the basic code structure:
git push origin <master>
Replace <master> with the branch where you want to push your changes when you’re not intending to push to the master branch.
git pull
git pull merges all the changes present in the remote repository to the local working directory.
git pull
git fetch
git fetch
retrieves the latest meta-data info from remote repository, but doesn’t do any file transferring. It’s more like just checking to see if there are any changes available.
git merge
git merge is used to merge a branch into the active one.
git merge <branch-name>
git diff
git diff lists down conflicts. In order to view conflicts against the base file, use
git diff
git stash
git stash command will temporarily save the changes that are not ready to be committed. That way, you can go back to that project later on.
git log
Use git log
to print out all the commits which have been done up until now.