Most Used Git Commands
What is Git!
Git is a powerful and popular tool for managing your code and tracking changes. Git allows you to collaboratively work with other developers, create branches for different features, and revert to previous versions if something goes wrong. In this article, I will explain some of the common Git commands that you can use to work with Git.
git config
Once you decided to use Git, you need to configure some basic settings, such as your username and email address. These settings will be associated with your commits, so that other developers can identify who made the changes. You can use the `git config` command to set these settings globally or locally. For example:
# Set your global username
git config — global user.name “Ozgur Kolukisa”# Set your global email address
git config — global user.email “ozgur@kolukisa.tld”# Set your local username for a specific repository
git config — local user.name “Kuzey Kolukisa”# Set your local email address for a specific repository
git config — local user.email “kuzey@kolukisa.tld”
You can also use the `git config` command to view or change other settings, such as your default editor, merge tool, color scheme, etc. To see a list of all the settings, use:
# List all the global settings
git config — global — list# List all the local settings
git config — local — list
git init
To start using Git, you need to create a Git repository, which is a folder that contains your code and some hidden files that store the history of your changes. You can use the “git init” command to initialize a new local repository in the current directory. For example:
# Create a new folder for your project
mkdir my-project# Change to the project folder
cd my-project# Initialize a new Git repository
git init
This will create a “.git” folder inside your project folder, which contains the repository files. You can now start adding and committing files to your repository.
git clone
If you want to work with an existing repository that is hosted on a remote server, such as GitHub or Bitbucket, you can use the “git clone” command to copy the repository to your local machine. For example:
# Clone a repository from GitHub to your current directory
git clone https://github.com/user/repo.git# Clone a repository from Bitbucket to a specific directory
git clone https://bitbucket.org/user/repo.git my-repo
This will create a new folder with the same name as the repository (or the name you specify) and copy all the files and history from the remote repository. You can now make changes to the local repository and push them back to the remote repository.
git add
When you make changes to your files in your working directory, you need to tell Git which files you want to include in your next commit. You can use the “git add” command to add files or changes to the staging area, which is a temporary area where you can prepare your commits. For example:
# Add a new file to the staging area
git add file.txt# Add all the files in the current directory to the staging area
git add .# Add only the modified files in the current directory to the staging area
git add -u# Add all the files that match a pattern to the staging area
git add *.txt
You can use the `git status` command to see which files are staged, modified, or untracked.
git commit
Once you staged your files or changes, you can use the `git commit` command to record them in your local repository with a message describing what you did. For example:
# Commit the staged files with a message
git commit -m “Added file.txt”# Commit all the modified files with a message
git commit -am “Updated file.txt”
This will create a new commit with a unique ID and message. You can use the “git log” command to see the history of commits in your repository. Also, you can create awesome report from git logs.
git push
After you have committed your changes locally, you may want to share them with other developers or update your remote repository. You can use the `git push` command to send your local commits to a remote repository, such as GitHub or Bitbucket. For example:
# Push your master branch to the origin remote repository
git push origin master# Push your feature branch to the origin remote repository
git push origin feature# Push all your branches to the origin remote repository
git push — all origin
This will upload your commits and update the remote branches. You may need to enter your username and password or set up SSH keys for authentication.
git pull
If you are working with other software developers or working with multiple machines, you may want to get the latest changes from a remote repository. You can use the `git pull` command to fetch and merge changes from a remote repository to your local repository. For example:
# Pull and merge the master branch from the origin remote repository
git pull origin master# Pull and merge the feature branch from the origin remote repository
git pull origin feature# Pull and merge all the branches from the origin remote repository
git pull — all origin
This will download and apply any new commits from the remote branches. You may need to resolve any conflicts if there are diverging changes.
git branch
One of the advantages of using Git is that it allows you to create branches for different features or tasks. Branches are pointers to different versions of your code that you can switch between or merge. You can use the `git branch` command to create, list, rename, or delete branches in your repository. For example:
# Create a new branch called feature1
git branch feature1# List all the branches in your repository
git branch# Rename a branch from feature1 to feature2
git branch -m feature1 feature2# Delete a branch called feature1
git branch -d feature1
You can also use the `git branch` command with some options to see more information about your branches, such as their last commit message or their upstream branch.
git checkout
To work on different branches or restore files in your working directory, you need to switch between them or check them out. You can use the `git checkout` command for this purpose. For example:
# Switch to an existing branch called feature
git checkout feature# Create a new branch called feature and switch to it
git checkout -b feature# Restore a file in your working directory from a previous commit
git checkout <commit-id> file.txt# Discard any changes in your working directory
git checkout — .
You can use the `git status` command to see which branch you are on, and which files are modified or restored.
git merge
After you finished working on a branch or want to include changes from another branch, you need to merge them. You can use the `git merge` command for this purpose. For example:
# Merge the feature branch into the current branch
git merge feature# Merge the master branch into the current branch using fast-forward mode
git merge — ff-only master# Merge two branches into a new branch called merged
git merge -s ours master feature -m “Merged master and feature” merged
This will apply any new commits from one branch onto another branch. You may need to resolve any conflicts if there are diverging changes.
git diff
To compare changes between files or branches, you need to use a diff tool. You can use the `git diff` command for this purpose. For example:
# Show differences between working directory and staging area
git diff# Show differences between staging area and last commit
git diff — staged# Show differences between two commits
git diff <commit-id-1> <commit-id-2># Show differences between two branches
git diff master feature
This will output any added, deleted, or modified lines of code between two versions of files or branches.
Conclusion
These are some of the common Git commands that you can use to work in your daily tasks with your Git repositories. There are many more Git commands that you can use for various purposes, but these are some of the most frequently used ones that you will encounter when working with Git.
I hope this post has helped you learn some basic Git commands with examples. If you have any questions or feedback, just fire a comment below.
Happy releases!