Fundamentals 6 min read

Step-by-Step Guide to Using Git: Initial Commit, Subsequent Commits, Common Commands, and Troubleshooting

This article provides a comprehensive, step‑by‑step tutorial on using Git, covering initial repository setup, generating SSH keys, configuring user information, performing first and subsequent commits, essential Git commands, and troubleshooting common clone failures, all illustrated with clear code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Step-by-Step Guide to Using Git: Initial Commit, Subsequent Commits, Common Commands, and Troubleshooting

1. Initial Commit

Download Git for Windows, create a local project folder, open Git Bash in that folder, initialize the repository, generate an SSH key, add the public key to the remote service, configure user name and email, add the remote URL, clone the repository, copy your project files, add, commit, and push.

git init
ssh-keygen -t rsa -C "[email protected]"
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git remote add origin https://git.oschina.net/xxxxxx/xxxxxx.git
# clone the remote repository
git clone ssh://git......
# add all files
git add .
# commit with a message
git commit -m "your message"
# push to remote
git push

2. Subsequent Commits

Enter the local repository folder, open Git Bash, clone the repository if needed, create a .gitignore file, copy your code into the project folder, then add, commit, and push the changes to the appropriate branch.

git add .
git commit -m "****"
git push origin branch_name

3. Common Git Commands

A collection of frequently used Git commands with brief explanations.

git init               # initialize a new repository
git remote add origin https://github.com/username/repo.git   # add a remote repository
git clone <url>        # clone a remote repository
git push -u origin master # push the first commit and set upstream
git status             # show repository status
git checkout -b dev   # create and switch to a new branch
git branch -a          # list all branches
git merge dev          # merge dev branch into current branch
git branch -d dev      # delete a branch
git rm file            # remove a file
git rm -r --cached .   # remove all files from the index (useful after changing .gitignore)
git add .              # stage all changes
git commit -m "message" # commit staged changes
git remote -v          # view remote URLs
git diff file          # show changes in a file
git log                # view commit history
git reset --hard HEAD^ # revert to previous commit
git reflog             # view reference log for commit IDs
git checkout -- file   # discard changes in a file
git stash              # temporarily store uncommitted changes
git stash list         # list stashed changes
git stash apply        # reapply stashed changes
git stash drop         # delete a stash
git stash pop          # reapply and delete a stash

4. Solutions for Git Clone Failures

If cloning fails, try switching the communication protocol or adjusting the SSL backend configuration.

# Switch to SSH protocol
git clone [email protected]:repository.git
# Change SSL backend
git config --global http.sslBackend "openssl"
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

software developmentGitCommand LineTutorialVersion Control
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.