Here are some of the most common Git commands with syntax and an example for a repository named “cloudiseasy”:
- clone: This command is used to clone a remote repository onto your local machine. Syntax:
git clone <repo_url>
Example:git clone https://github.com/username/cloudiseasy.git
- init: This command is used to initialize an existing directory as a Git repository. Syntax:
git init
Example:cd cloudiseasy
and thengit init
- add: This command is used to add changes to the staging area. Syntax:
git add <file>
Example:git add index.html
- commit: This command is used to save changes to the local repository. Syntax:
git commit -m "commit message"
Example:git commit -m "Added index page"
- push: This command is used to upload local commits to a remote repository. Syntax:
git push origin <branch>
Example:git push origin master
- pull: This command is used to download changes from a remote repository and merge them with your local repository. Syntax:
git pull origin <branch>
Example:git pull origin master
- checkout: This command is used to switch between branches or restore files in your working directory. Syntax:
git checkout <branch>
Example:git checkout develop
- status: This command is used to check the status of your repository. Syntax:
git status
Example:git status
- diff: This command is used to see the differences between your local repository and the remote repository. Syntax:
git diff
Example:git diff
- log: This command is used to display the history of your repository. Syntax:
git log
Example:git log
Common Git issues and their solutions:
- “fatal: repository not found” error: This error occurs when the remote repository URL specified in the local repository is incorrect. Solution: Verify the remote repository URL and make sure it’s correct.
- “Merge conflict” error: This error occurs when multiple changes are made to the same file on different branches and Git can’t automatically resolve the differences. Solution: Manually edit the file to resolve the conflict and then use Git to stage and commit the changes.
- “Push rejected” error: This error occurs when the remote repository is ahead of the local repository and a push operation can’t be performed. Solution: Pull the changes from the remote repository, resolve any conflicts, and then push the changes again.
- “Detached HEAD” state: This occurs when checking out a specific commit instead of a branch, which detaches the HEAD from the branch. Solution: Checkout a branch to reattach the HEAD, or create a new branch from the current commit.
- “Stash conflict” error: This error occurs when trying to apply a stash that contains conflicts with the current branch. Solution: Resolve the conflicts, stage the changes, and then drop the stash.
- “Lost commits” error: This error occurs when committing changes to the wrong branch, resetting the branch to a previous commit, or other actions that cause the loss of recent commits. Solution: Use the reflog to find the lost commits and then recreate the branch with the missing commits.
Note: These are common Git issues and their solutions, but it’s always a good idea to backup your repository before making any major changes.
These are some of the common git commands and issues faced on daily basis. If you’ve encountered something apart from these, post it in comments.