Git commands you should know about
A version control system allows us to keep track of when and who did what changes to our files. Those can be code, configuration, images, or whatever else we need to track. Lets see the useful commands that is helpful to you, to maintain the code in well form.
1. Basic configuration
Configure your name and email information, this are the details which is used when you track your changes.
git config --global user.name "My Name"
— Usernamegit config --global user.email "email@example.com"
— Email address
Note: --global
used for global configuration. If you want to configure personal detail with single repository, then no need to use --global
.
2. Init, add, status, commit
git init
Initialise the empty git repo. (.git): The git directory acts as a database for all the changes tracked in Git and the working tree acts as a sandbox where we can edit the current versions of the files.git add filename, ...
orgit add .
add the file in staging area: The staging area which is also known as the index is a file maintained by Git that contains all of the information about what files and changes are going to go into your next command.git add -p
it will show the file changed and ask for staging confirmationgit status
is used to show that the file is staged or not, it shows the tracked and untracked filegit commit -m "commit message"
It create the new snapshot in the git repo.git commit -a -m "commit message"
It will stage and commit the changes and it will not work for new files.git commit --amend
Override the last commit
3. Revert commit
git reset --soft HEAD~2
orgit reset --hard HEAD~2
Revert last 2 commit changes.
Note: Here --soft
will revert the commit and keep your changes in staged and --hard
will revert the changes forever and not remain in staged.
4. Log, show
git log
is use to see the commit historygit log --oneline --graph
: is use to see the commit history in graphical manner. Easy to understand the flow of commit .git log -p
It will display commit logs with content changed informationgit show <commit_id>
It will display the detail about particular commitgit log --stat
display basic stat about commit
5. Stash
git stash
is use to stash your changesgit apply stash stash@{0}
is use to apply your recent stash.git stash save "stash message"
allow to enter stash message
6. diff
- The diff tool shows all the differences between any type of file. By highlighting what’s changed, it helps us understand the changes and see how the files have been modified.
diff file_1.py file_2.py
show file differencediff -u file_1.py file_2.py
show the diff in another format (unified format)diff -u old_file.py new_file.py > change.diff
Create the diff file
7. Apply changes (patch)
- While diff is the command that generates the difference between two files, patch is the command that applies those differences to the original file.
patch file_1.py < file_1_changes.diff
To automatically apply changes to a file, we need to run the patch command on the file that we want to modify with the diff file as input.git diff >> my_first_patch.patch
It will create patch file of untracked changes.git apply my_first_patch.patch
is use to apply the patch file changes.
8. git diff
git diff
It will show the changes made in file which is modified (unstaged)git diff --staged
show the changes of staged file
9. Branch
git branch
List all branchgit branch <branch_name>
Create new branchgit checkout <branch_name>
Move to branchgit checkout -b <branch_name>
Creates a new branch and switches to itgit branch -D
Forcefully delete the branchgit branch -r
Lists the remote branches
10. Merge branch
git merge <feature_branch>
If we want to merge feature branch in master branch then move to master branch and use the commandgit merge --abort
it will escape the merge and revert that
11. Clone
git clone <git-web-url>
is use to create the local copy of the remote repo
12. Push
git push
command gathers all the snapshot and sends them to the remote repo.git push -u origin <new_branch_name>
is use to create branch upstream. (-u flag is similar to — set — upstream)git push --delete origin <branch_name>
Delete the remote branch
13. Pull
git pull
is used to fetch the newest updates from a remote repo.
14. Fetch
git fetch
is use to sync the repo, it downloads the remote changes to local
Note: git fetch
fetches remote updates but doesn’t merge; git pull
fetches remote updates and merges.
15. Git remote
git remote -v
to see the remote configuration. With this we can see the url associated with the origin remote.git remote show origin
display more details about remote (origin is the default name of remote)
16. Rebase
git rebase <branch_name>
Move the current branch on top of the <specified_branch_name> branch- Rebasing instead of merging rewrites history and maintains linearity, making for cleaner code.
17. Squash
git rebase -i HEAD~2
is use to Squash last 2 commits- Squashing a commit means, from an idiomatic point of view, to move the changes introduced in said commit into its parent so that you end up with one commit instead of two (or more). If you repeat this process multiple times, you can reduce n commit to a single one.
Thank you for reading :). I hope this is useful to you. If I missed any command which developer uses in regular basis then please add comment.