DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on • Updated on

Git - Is just this.

First install Git. Google will tell you that.

Clone the project that you want to work on using command:

git clone <project_url>
Enter fullscreen mode Exit fullscreen mode

It is recommended to create new branch before you start coding:

git checkout -b <branch_name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

then try checking out to the branch that your colleague shared as

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

it is an optional - git reset // if you want to avoid pushing any file

git commit -m "<ticket_number><message>" // to commit 
Enter fullscreen mode Exit fullscreen mode
git push // to push to your repository.
Enter fullscreen mode Exit fullscreen mode

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. 
Enter fullscreen mode Exit fullscreen mode
git stash apply // to apply stashed changes whenever you need.
Enter fullscreen mode Exit fullscreen mode
git stash apply stash@{1} // to apply first record of stash.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
git stash show -p // shows changes in files
Enter fullscreen mode Exit fullscreen mode

**Question 4: **What are all possible push rejections?

  1. 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.

  1. 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>
Enter fullscreen mode Exit fullscreen mode

Question 6: How can I merge my changes with master branch before creating pull request(PR)?

Several ways are there:

git pull origin master
Enter fullscreen mode Exit fullscreen mode
git merge master
Enter fullscreen mode Exit fullscreen mode
git rebase master
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
lexlohr profile image
Alex Lohr

About question 2: either use git pull --rebase or even set git config --global pull.rebase true so every pull will rebase by default.