Git: The Ultimate Guide to Understanding and Using this Essential Tool for Developers | Tips, Tricks and Best Practices
Git is a software using which we can track and manage the changes in any file/folder or project. See below for standard definition and more details.
How to install ?
There are many alternatives to install Git. It is mostly used as a command line interface, but it also provides us graphical user interface to manage workflow visually. Follow below link to download both.
Git Basic Commands :
1. Adding git user : After installing git we need to configure User (name , email ) for git software.
> git config — global user.name “Mahesh Jadhav”
> git config — global user.email “maheshjadhav@example.com”
2. Below command will initialize any folder as a git repository to track further changes
> git init
3. Creating files into git repository –
> touch index.html
> touch text.txt
4. Command to check the status of working directory and staging area
> git status
5. Adding changes to staging area
> git add <filename> {To add changes from specific file}
> git add -A {To add all the changes at a time}
> git add . {To add all the changes at a time}
6. Untracking changes from staged area
> git restore --staged <filename>
7. To commit changes for tracking
> git commit -m “commit_message”
8. Directly commiting files(changes) by skipping staging area
> git commit -a -m “commit_message”
9. Modifying most recent commit message and combining staged changes with the previous commit
> git commit --amend -m “commit_message”
10. To view log of changes (Commits) :
> git log
> git log -p -2 {shows only last two commits}
> git log --stat {shows short status of every commit}
> git log --pretty=oneline {all commmits in one line}
> git log --pretty=short {all commits in very short}
> git log --pretty=full {shows full commit message}
> git log --since=2.days {all commits from last two days}
> git log --since=3.weeks {all commits from last three weeks}
> git log --since=2.months {all commits from last two months}
> git log --since=1.years {all commits from last one year}
> git log --pretty=format: “%h — %ae” {gives details of commits according to given format specifiers, To know more about specifiers click here }
> git branch -v {shows last commit of all branches}
11. View difference between staging area and working directory
> git diff
12. View difference between staging area and last commit
> git diff --staged
13. Remove files from staging area and add to untracked list
> git rm --cached <filename>
> git rm <filename> {Remove files completely}
14. Branching :
> git branch {To view all present branches}
> git branch <branchname> {Create a new branch}
> git checkout <branchname> {Switch to mentioned branch}
> git checkout -b <branchname> {Create and switch to mentioned branch}
> git branch --merged {Show all already merged branches}
> git branch --no-merged {Show all unmerged branches}
> git branch -d <name> {Delete specified branch if it is merged}
15. Merging two branches
first checkout to the branch on which you want to merge the branches then
> git merge <branchname>
16. Cloning remote repository
> git clone <url_of_repository> {clone repository with default name}
> git clone <url_of_repository> name {clone repository with given name}
17. Adding remote repository to upload changes on GitHub
Copy the url of your remote repository
> git remote add origin <url> {Adds remote repository as origin}
> git push -u origin master {Push data from master to origin}
18. Aliasing
Incase you are as lazy as I am and want to change long commands into short
> git config --global alias.st status {now it will consider st as status }
19. Ignoring git files/directories
If you don’t want to track some files or directories(eg. log files) in the commits then just create a file named .ignore in your home folder and mention all files and directories which you want to ignore as shown below.
.log {Ignore files of specific extension}
<folder_name>/ {Ignores everything inside specific folder}
20. UNDO all changes made after last commit and make working tree same as last commit
> git reset HEAD --hard