First install Git. Google will tell you that.
Clone the project that you want to work on using command:
git clone <project_url>
It is recommended to create new branch before you start coding:
git checkout -b <branch_name>
Question 1: My colleague created a branch and pushed but couldn't get that in my local. How can I do that?
Use below command to fetch changes from remote.
git fetch
then try checking out to the branch that your colleague shared as
git checkout <branch_name>
You have done some changes and want to push them to your branch then follow below steps:
git add -A // to add all changes called staging
git add <specific_file_name> // to add file by file.
it is an optional - git reset // if you want to avoid pushing any file
git commit -m "<ticket_number><message>" // to commit
git push // to push to your repository.
Question 2: My colleague and I are working on same branch. We are pushing our changes as soon as we are done. How we can manage to not to miss any of our changes while pushing?
Best way: Avoid.
Approach 1: Always pull latest changes from branch (which have been pushed by your colleague) and resolve any conflicts and push all of your changes.
Question 3: I did couple of changes but I don't want to push them as of now for some reason but need these details to be present somewhere. How can I achieve it?
Best Way: Push your changes to separate branch and remember that branch name.
Approach 1: You can use stashing feature of git to temporarily stash the changes and apply whenever required.
Below commands are useful for same purpose:
git stash // whatever changes you did so far are stashed.
git stash apply // to apply stashed changes whenever you need.
git stash apply stash@{1} // to apply first record of stash.
stash stores all the stashed files like a stack. By default 0th item is applied as soon as you hit git stash apply
but if you want specific record of stash then specify that number and apply like shown above.
I stashed some changes and forgot what changes I did then how can see them without applying those changes.
You can use below command:
git stash show // shows the list of changed files from recent stash
git stash show -p // shows changes in files
**Question 4: **What are all possible push rejections?
- You remote branch has new changes that your local doesn't have that means your colleague has pushed few changes to your branch.
so, as mentioned - pull his changes first and then apply changes and push all changes together.
- You might not have permission to push. Check admin to get access.
Question 5: I want specific changes of one commit of some other branch in my current branch. How Can I achieve that?
Using cherry-pick as:
git cherry-pick <commit_id>
Question 6: How can I merge my changes with master branch before creating pull request(PR)?
Several ways are there:
git pull origin master
git merge master
git rebase master
Resolve any merge conflicts and push. Done.
There were several other features that git provides. Just read carefully the error or exception you see while using git. Search the same error in Google. Done.
I covered mostly used commands in this article and is pretty much enough to do day to day activities of Dev.
Please do comment your questions. I am always happy to help.
Thanks.
Latest comments (1)
About question 2: either use
git pull --rebase
or even setgit config --global pull.rebase true
so every pull will rebase by default.