Git Basic Commands
- Telling Git Who you are.
Configure the author's name and email address to be used with the commits.
git config --global user.name "abhi"git config --global user.email abhi@gmail.com
2. Creating a new local repository
Create a folder in your local machine and then run the below command within the directory.
git init
The above command will create a repository in your local system and If you run ls -la in the current directory, You can find a folder .git is created.
3. Adding one or more files to Git
To add a file to the Staging area
git add filename
To add more files to a staging area
git add *
The staging area are the files that are going to be part of the next commit.
4. Committing Changes
Commit is used for saving changes.
After adding files to the staging area using git add, You can commit those changes but not to the remote repository.
git commit -m "commit message"
To commit any files that you have added with git add and also commit any files you have changed since then.
git commit -a
The above command only works for tracked files (files that are added to git using .git command).
5. Checking status of Git
Lists all the files that you’ve changed that still need to added or committed.
git status
The above command will list all the files/folders that are not yet committed.
6. To list all the local branches in the current repository
The command lists all the local branches in the current repository.
git branch
7. To create a new branch
This command will create a new branch.
git branch branchname
To check whether the branch is created or not, Run git branch
8. To delete a branch,
git branch -d branchname
9. witching between branches
If are working on different branches and If you want to switch between them.
git checkout branch
If you append -b to the above command, It will create a new branch and also switches to it.
git checkout -b branchname
10. Copying remote repository to a local working directory
To create a clone or copy of the targeted remote repository to the local working directory.
git clone https://github.com/abhibvp003/expressdemo.git
If the remote git repository which you are cloning to the local working directory is not public, Then it asks for a password.
11. Fetch & Merge remote repository Changes to Local
The command will fetch for any changes in the remote repository and merge it with the local repository.
git pull https://github.com/abhibvp003/expressdemo.git
12. Tracking Changes
git diff command is used to track the difference between the changes made on the file.
git diff
The above command is used to find the differences between commits, branches, files, working trees, etc.
For example, The command will pick a file from the local working directory and check for the changes.
It performs 2 checks, One to compare HEAD to the staging area and one to compare staging area to work-tree.
13. Check differences between the staging area and the latest version
git diff --staged
Let's say, You have a file which is already committed , Now you have made changes in that file and staged it.
To check the differences between the branches
The command will show the differences between the two branches.
git diff master development
14. Check the version history of the current branch
The command is used to list all the version history of the current branch.
git log command lists the commits made in the branch in reverse order.
git log
To check the version history of a particular file,
git log --follow filename
15. To Check log messages and textual difference of a commit
git show command is used to view the changes on the specified commit and the metadata of a commit
git show commitID
16. To tag a specified commit
This command is used to give a tag to a specified commit.
git tag v1.1 commitID
17. To list the tags
using the below command, You can list the tags
git tag
If you want to list the tags with a specific tag pattern, append -l to the above command.
git tag -l v1*
18. Merging branches
This command is used to merge the specified branch’s history into the current branch.
For example: Let's say your current branch is a new branch and you want to merge it with the master , If you run the below command all the changes in the master branch will be reflected in the new branch.
git merge master
19. Connecting Local Repository to the Remote repository.
Using the below command, You can connect your local git repository to the remote git repository
git remote add origin https://github.com/abhibvp003/expressdemo.git
20. Pushing Local branch to Remote repository
if you have worked on a branch locally and you want to push that particular branch to the remote git repository, run the below command.
git push --set-upstream origin localbranchname
21. Stashing Git files
Using this command You can temporarily store all the modified tracked files.
git stash save
To restore the recently stashed files.
git stash popgit stash list
To delete the stash
git stash drop stash@{0}
22. Removing a file from Git
The below command will delete the file from the staging area (index)
git rm filename --cached
if you want to delete the file from git and as well as the file system, use the below command.
git rm filename
23. Git Reset
If you have staged a file and you want to unstage it? , Use the below command.
git reset filename
To reset all the changes after the specified commits, But the changes are locally preserved.
git reset commitID
To move the HEAD to a specified commit. (Too hard reset files to HEAD on git)
git reset --hard commitID