Git is a popular version control system that allows developers to collaborate on projects, keep track of changes, and revert to previous versions of their code. Knowing some fundamental Git commands can be quite beneficial, whether you are an experienced developer or just getting started. We'll go over some of the most popular Git commands with examples in this article to help you get started.
Configuration
- git config
# Configure Git settings, such as user name and email
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Repository Creation
git init
git clone
# create .git directory in folder
cd /path/to/my/codebase
git init
# clone a githun repository in local
git clone <repository_url>
Staging Changes
- git status
# show changed files in working directory
git status
- git add
# Add changes to the staging area
git add [file_name]
#add all files
git add .
- git diff
# Show changes in code
git diff [file_name]
# all chnages
git diff
# Staged changes but not committed
git diff --staged
- git reset
# Remove changes from the staging area
git reset
Committing Changes
- git commit -m "commit_msg"
# commit msg
git commit -m β[message]β
- git revert
# undo a commit
git revert HEAD
Remote Repositories
# push code Push changes to a remote repository.
git push
# Pull changes from a remote repository.
git pull
- git remote
# List the remote repositories
git remote
# Add a new remote repository
git remote add <name> <url>
# remove a remote repository
git remote remove <name>
# rename a remote repository
git remote rename origin <new_name>
# get a remote repository url
git remote get-url <name>
# change the URL of a remote repository
git remote set-url origin <url>
- git fetch
#fetch changes from a remote repository
git fetch origin
Branch and Merge
- git branch
# List all branches
git branch # -a -> all branch
# create a new branch
git branch newbranch
# switch to a branch
git checkout mybranch
# create a new branch and switch to it
git checkout -b newbranch
# delete a branch
git branch -d mybranch
# rename a branch
git branch -m oldbranch newbranch
git merge
merge changes from one branch into another branch
# merges changes from the branch with the name "mybranch" into the current branch.
#If there are conflicts between the changes in the two branches, Git will prompt you to resolve the conflicts before completing the merge.
git merge mybranch
Reset
Reset the current state of the repository
# unstage changes
git reset [commit]
# unmodify a file
git reset [file_name]
# completely remove commits
git reset --hard [commit]
Logs
- git log
# view the commit history
git log
# view the commit history for a specific branch
git log mybranch
# view the commit history for a specific file
git log path/to/file
# view the commit history for a specific author
git log --author=username
# view the commit history for a specific date range
git log --since="2022-01-01" --until="2022-12-31"
Temporary commits
- git stash
# save changes in your working directory
git stash save "my temporary changes"
# list stashes
git stash list
# apply a stash
git stash apply stash@{0}
# remove a stash
git stash drop stash@{0}
# apply and remove a stash
git stash pop
# show the changes in a stash
git stash show stash@{0}
File changes
# delete the file from project and stage the removal for commit
git rm [file]
# change an existing file path and stage the move
git mv [existing-path] [new-path]
Β