Lightweight Git Command Line Operation Tutorial
Lightweight Git Command Line Operation Tutorial
Git is a distributed version control system used to track changes to files, especially source code files. Here are some simple Git operations suitable for beginners:
- Initialize a repository
- git init: Create a new Git repository in the current directory.
- Clone a repository
- git clone [url]: Clone a remote repository to local.
- Add files to the staging area
- git add [file]: Add the specified file to the staging area.
- git add .: Add all changes in the current directory to the staging area.
- Commit changes
- git commit -m “[message]”: Commit the changes in the staging area to the repository with a commit message.
- Check status
- git status: Check the current status of the repository, including untracked files, modified files, etc.
- Check differences
- git diff: View differences between the working directory and the staging area.
- git diff –staged: View differences between the staging area and the last commit.
- View commit history
- git log: View commit history.
- git log –oneline: View commit history in a concise format.
- Branch operations
- git branch: List all local branches.
- git branch [branch-name]: Create a new branch.
- git checkout [branch-name]: Switch to the specified branch.
- git checkout -b [branch-name]: Create and switch to a new branch.
- Merge branches
- git merge [branch-name]: Merge the specified branch into the current branch.
- Push to remote repository
- git push [remote] [branch]: Push changes from the local branch to the remote repository.
- git push -u [remote] [branch]: Push the local branch to the remote repository and set it as the default upstream branch.
- Pull changes from remote repository
- git pull [remote] [branch]: Pull changes from the remote repository and merge them into the current branch.
- Undo operations
- git checkout – [file]: Undo changes to a file in the working directory.
- git reset HEAD [file]: Remove a file from the staging area.
- git revert [commit]: Create a new commit that undoes the changes from the specified commit.
- Tags
- git tag [tag-name]: Create a lightweight tag.
- git tag -a [tag-name] -m “[message]”: Create an annotated tag with a message.
- git push [remote] [tag-name]: Push a tag to the remote repository.
- View remote repositories
- git remote -v: View detailed information about remote repositories.
- Configuration
- git config –global user.name “[name]”: Set the global username.
- git config –global user.email “[email]”: Set the global user email.
These operations are the basics of Git and are suitable for daily use. As you become more familiar with Git, you can learn more advanced features such as rebase, stash, submodules, etc.
Last updated on
