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.
1. Global configuration
git config --global user.name "PHP Code Hub"
git config --global user.email "email@example.com"
2. Local configuration
git config --local user.name "PHP Code Hub"
git config --local user.email "firstname.lastname@example.org"
2. Initializes a git repository
git init : Initializes a git repository
cd /home/public_html/new-git-folder/ git init
3. Check out a repository
git clone : Check out a repository
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.
git add file-name : Add specific file to your index.
git add * : Add all changed file to your index.
git rm : Removes files from your index and your working directory.
git rm file-name : Remove specific file.
git commit : Takes all of the changes written in the index.
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.
git status : Shows the status of files in the index versus the working directory.
git status : List the files you've changed and those you still need to add or commit.
git pull: Fetch and merge changes on the remote server to your working directory:
git pull origin branch-name : Fetch and merge changes.
git push : Send changes to branch of your remote repository.
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.
git branch : Lists existing branches, including remote branches if "-a" is provided.
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.
git checkout : Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.
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.
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-name
git diff : To check the conflicts.
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.
git checkout -- file-name