Git commands every developer should know
What is git ?
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
This is from git's official documentation.
In one words,
git is a version control system of your codes.
Why we should use git ?
We're living in a fast forwarding age. We're now working on agile process and many developers work same project, at the same time. So, there needs a system to combine all of the users code in one or there needs to manage every track of the codes. So that in future, someone can get his older version or make newer version from that.
So now - git is a very important things for the develoepr, for the organization and for personal projects also.
Essential git commands that every developer should learn :
Without any more discussion on git commands, I'd like to start direct coding of any git managed project.
1) First Time Git Bash setup (Only Needs if git credential has been lost) - It is usefull to configure every ones personal git configuration like git email and git name.
git config user.email "firstname.lastname@example.org" git config user.name "UserName"
2) Initialize git in any project (Only First time) - It is usefull to initilize first time git commands. To integrate git version control system in your project, you've to write this.
3) Add local files to git (For every commit) - To add local files to git version controlling system
git add .
4) Git Commit (For every commit) - To save the commit in github or a part of work in github we need to make a commit. In commit, we can pass any message for this update or this version.
git commit -m "My First Upload to git"
5) Add Remote Repository (For every commit) - Any remote git repository is not connected with the project by default. We need to manually connect that repository. We can use follwing commands to connect to the remote git repository.
git remote add origin https://github.com/try-git/test_project.git
6) Final Push to Master branch (For every commit) - After connecting, adding files, committing changes, we need to push those files to the server or remote repository address.
git push -u origin master
Full First time Commands: all first time project git commands will be at a glance:
git init git add . git commit -m "First Push to remote branch" git remote add origin https://github.com/try-git/test_project.git git push -u origin master
7) Get status of your sync with git - To know the status of your local repository branch to the live repository branch, you need to get this status
Commands After published one time : To update anything of your git, just follow this three lines of git command.
git add . git commit -m "Authentication - Login System Integrated" git push
First add the all new codes to local by
git add, then write the message for this update
git commit -m "Authentication - Login System Integrated", Here -
Authentication Login System Integrated is the message we want to store.
8) Create New Branch with git - To Create a new Branch and then move to that branch
git branch BranchName // Only create New Branch called BranchName git checkout BranchName // Move to that Branch called BranchName git checkout -b BranchName // Create and move to that branch called BranchName // Real Example (move to master branch) git checkout master // Create akash branch and move to that from master branch git checkout -b akash
9) Push folders to branch and get that branch with merged - To push changes to any branch
git push https://github.com/RepositoryAddress BranchName // Push changes to BranchName branch
10) what is git fetch or pull - To get only the remote data, we need to call git fetch, if we want both fetch and merge, then use git pull
git fetch origin akash // Fetch data from akash branch git merge origin/akash // Merge data of akash branch // Both fetch and merge of akash branch git pull origin/akash // Only from the current branch remote data git pull
11) delete any git branch - To delete any github branch: -d branchName
git branch -d BranchName git branch -d akash // Remove akash branch
12) Create Tag and Versioning of Codes - To create a code direct versioning of files and direct downloading system of that version we should use git tags
git tag -a TagName -m "Tag Message" // Real Example git tag -a DevsEnvV100 -m "DevsEnv Version 1.0.0"
13) Merge Another Branch - To get other branch data in current branch
git fetch origin akash // Git fetch akash branch git merge origin/akash // Git merge akash branch
14) Full Example of Master Branch Merge - To get the data from other branch and merge from master - here is the full process
git fetch origin akash // akash is the branch name git merge origin/akash // merge akash branch git add . // Add files git commit -m "Merge Akash Branch - Feature 1" // Commit git push // Push
Another Example from
git fetch origin dip // dip is the branch name git merge origin/dip // merge dip branch git add . // Add files git commit -m "Merge Dip Branch - Feature 2" // Commit git push // Push
Note: When any merge happens, it needs to be manually fixed the merge. Like - if two persons work in a project's same file, then their happens of a conflicting that - which parts git will store. And from master or main branch - we've to manully remove that conflict.
To fetch all of the remote branches at single command -
git fetch --all
15) Full Example of other Branch Merge from master- To get the updated data from master branch and merge in that branch
// Fetch and Merge Master Branch Data git fetch origin master git merge origin/master // Sync with Remote Branch git add . git commit -m "Merged with New Services" git push