As a software engineer, we need to collaborate on multiple projects with the engineering team. For that, Git is one of the most used and recommended Version controlling system that the industry is using on a daily basis. Therefore, at the early stage of my engineering career, I also needed to know the basic commands of git in order to have a better synchronization with the team and the projects we will be working on.
Today I will be sharing my knowledge on git and its commands to get started with your initial projects.
There are 4 stages:
Workspace : This is the folder/Location/Directory where we start our project.
Staging : Making candidates to be committed to the local repository.
Local Repository : After staging, we need to commit changes to here.
Remote Repository : After committing, we can push our changes to the Remote Repository.
Now, Lets Explore the commands to work on all the above mentioned steps.
To initialize a new local repository in your workspace :
git init
To configure user globally
git config --global user.name "name" git config --global user.email "mail"
To configure user specifically for specific project
git config user.name "name" git config user.email "mail"
- To see the config
git config --list
- For Staging
git add --all git add . git add filename
- For commit
git commit -m "msg"
- To see all commit details shortly
git log --oneline
- Going back to previous Stage
git checkout 'CommitCode'
- See changes of a commit
git show ‘CommitCode’
- See difference between 2 commits
git diff commit1code commit2code
- To remove a file from git
git rm file.txt
- Delete a file from Workspace
git rm filename
- Remove a file from Staging (Untrack)
git reset HEAD filename
- Create New Branch
git branch branchName
- See All Branches
git branch
- Switch branches
git checkout branchname
- Create branch and switch to that branch
git branch -b branchName
- Let Local Repo know that there is a change in Remote repo
git fetch
- Get the changes of Remote in Workspace
git pull
- Merge 2 branch (From present branch)
git merge OtherBranchName
- Save the file to temporary folder, So we don't loose that
git stash
- Get the file from stash
git stash pop/apply
- List of stash
git stash list
- Go to specific Stash
git stash pop stashCode
- Remove untracked Files
git clean -f
- To Ignore a file
Create a .gitignore file touch .gitignore // will create the ignore file.
- Use HTTP Link to connect with remote repository.
git remote add origin httpLink