Git commands article

Home >> Git commands >>

  Git basic commands and it's usages

This article will explain you about git basic command and its usages.

1. Git configuration
git config : Sets configuration values for your repo like : user-name, email etc.

Exmaple:
1. Global configuration
git config --global user.name "PHP Code Hub"
git config --global user.email "query@phpcodehub.co.in"

2. Local configuration
git config --local user.name "PHP Code Hub"
git config --local user.email "query@phpcodehub.co.in"

2. Initializes a git repository
git init : Initializes a git repository

Example: 
cd /home/public_html/new-git-folder/ git init

3. Check out a repository
git clone : Check out a repository

Example:
git clone /path/to/repository clone-folder-name
Note : clone-folder-name is the folder, where your code will reside after clone.

4. Add files
git add : Adds files changes in your working directory to your index.

Example:
git add file-name : Add specific file to your index.
git add * : Add all changed file to your index.

5. Remove
git rm : Removes files from your index and your working directory.

Example:
git rm file-name : Remove specific file.

6. Commit
git commit : Takes all of the changes written in the index.

Example:
git commit -m "Commit message" : Now the file is committed to the HEAD, but not in your remote repository yet.
git commit -a : Commit any files you've added with git add, and also commit any files you've changed since then.

7. Status
git status : Shows the status of files in the index versus the working directory.

Example:
git status : List the files you've changed and those you still need to add or commit.

8. Pull
git pull: Fetch and merge changes on the remote server to your working directory:

Example:
git pull origin branch-name : Fetch and merge changes.

9. Push
git push : Send changes to branch of your remote repository.

Example:
git push origin branch-name : Push the branch to your remote repository, so others can use it.
git push --all origin : Push all branches to your remote repository.
git push origin :branch-name : Delete a branch on your remote repository.

10. Brach
git branch : Lists existing branches, including remote branches if "-a" is provided.

Example:
git branch : List all the branches in your repo, and also tell you what branch you're currently in.
git branch -a : List all the branches in your repo including remote branches.
git branch -d branch-name : Delete the feature branch.

11. Checkout
git checkout : Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.

Example:
git checkout -b branch-name : Create a new branch and switch to it.
git checkout branch-name : Switch from one branch to another.
git remote -v : List all currently configured remote repositories.

12. Merges 
git merge : Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.

Example:
git merge branch-name

13. Conflict
git diff : To check the conflicts.

Example: 
git diff : View all the merge conflicts.
git diff --base file-name : View the conflicts against the base file.
git diff source-branch target-branch

14. Undo the changes.
git checkout : If you mess up, you can replace the changes in your working tree with the last content in head.

Example:
git checkout -- file-name

Write your comment now

  

Related articles