- Git is a free and open source distributed version control system.
- Git’s purpose is to keep track of projects and files as they change over time with changes happening from different users.
List of some important git commands
git init
- Initialize a new git repositoryUsage:
$ git init Initialized empty Git repository in [path].
git config
- Configure git settingsUsage:
$ git config --global user.name "Your Name" $ git config --global user.email "name@example.com"
Flags (Frequently used) :
--global
- Configure settings for all the repo--local
- Configure settings for the current repo--list
- List all the configurations
You can learn more about it here
git add
- Add files to the staging areaStaging Area:
- Preview the changes before committing.
- You can verify the tracked and untracked files.
Usage:
$ git add file1 file2 #Add the mentioned files $ git add . # Add all files
git status
- Show the status of the repositoryUsage:
$ git status
You may get a similar output like this. It shows which are the tracked and untracked files.
Note: You can always remove tracked files using
git rm --cached file_name
git commit
- Commit changes to the repository- It captures a snapshot of a current modified files.
Usage:
$ git commit -m "commit message" #-am flag is used to add and commit only the modified changes $ git commit -am "commit message"
git clone
- Clone a repositoryIt clones a remote repository into your local machine.
Usage:
$ git clone https://github.com/username/repository_name
git remote
- Manage remote repositoriesUsage:
#Add a origin for a remote branch $ git remote add origin https://github.com/username/repository_name #Remove a remote branch $ git remote remove origin
- You can learn more about it here
git push
- Push changes to a remote branchpush
command is used to push the changes to a remote branch.Usage:
$ git push origin branch_name #Setting an upstream so that you can use git push next time. $ git push origin -u branch_name
git fetch
- Fetch changes from a remote branchIt fetches the changes from the remote branch , but doesn't merge them.
Usage:
$ git fetch origin branch_name
git pull
- Pull changes from a remote branchpull
command is used to pull the changes from the remote branch and merge them.Usage:
$ git pull origin branch_name
git branch
- Manage branchesUsage:
#Create a new branch $ git branch branch_name #Delete a branch $ git branch -d branch_name
git merge
- Merge branchesIt merges the changes from one branch to another.
Usage:
$ git merge branch_name
- Usecase Scenario:
- When a lot of developers are working on various branches. We can merge the changes from one branch to another.
Merge conflicts
When there is a conflict in the merge,you need to resolve it in your text editor and commit the changes and push it.
Example Usage
$ git merge branch_name #Conflict occurs , resolve it , add and commit $ git commit -am "commit message"
git log
,git reflog
- View the commit historyUsage:
#View the commit history of a specific branch $ git log #View the commit history of all the branches $ git reflog
git checkout
,git switch
- Switch branchesUsage:
#Switch to a branch $ git checkout branch_name $ git switch branch_name #Create a branch $ git checkout -b branch_name #Delete a branch $ git checkout -d branch_name
git reset
,git revert
- Go back to previous state- Both
reset
andrevert
are used to go back to previous state butrevert
creates a new commit with the previous state andreset
go back to previous state without creating a new commit. Usage:
$ git reset HEAD^ $ git revert commit_hash
Frequently used:
#reset to previous state also force deletes $ git reset --hard HEAD^ #reset to previous state without deleting the staged files $ git reset --soft HEAD~n
- Both
git rebase
- Rebase a branch- A pretty useful commands which acts like
git merge
. - The main difference is that
rebase
have a straight line of commits. It doesn't create a new commit for conflicts.
Usage:
$ git rebase branch_name ##After fixing the conflicts use $ git rebase --continue
Interactive rebase
You can specify the order of commits you want to rebase.
$ git rebase -i
- A pretty useful commands which acts like
git stash
- Stash changes- It is like a temporary storage for a commit.You store those commit and pop them later.
Usage:
$ git stash
Frequently used:
#pop the last stash $ git stash pop #list all the stash $ git stash list
- You can learn more about it here
git cherry-pick
- Pick a specific commit- Used to pick a specific commit on other branches and use them to our current branch.
Usage:
$ git cherry-pick commit_hash
- You can learn more about it here
git diff
- View changes in a file- Last but the most used and important command.
It helps to view all the changes in the file.
Usage:
$ git diff
Conclusion
- These are the most common and frequently used comment.
- I hope you like this article.