This week I came across a few instances where I needed to perform some action with Git or GitHub and needed to google a solution. I have used Git and GitHub many times before but it’s easy to forget the commands to use for every action. I am sure others come across similar issues, so I will chronicle some of my problems and solutions here.
Problem 1: I need to create a new branch and switch to it. I have done this many times before but often forget the fastest way to do it.
Solution 1:
The slow way: git branch <branch-name> && git checkout <branch-name>
The fast way: git checkout -b <branch-name>
Problem 2: I made some changes and committed some files to a branch. Some of the committed files pertained to the feature that branch was dedicated to developing, but others were core files I needed to modify to tinker with the feature, but shouldn’t be pulled into the more production-ready branch. To fix this I decided to revert the files back to their condition on the production-ready branch.
Solution: git checkout origin/<production-ready-branch> <filename>
Problem 3: I needed to create a pull request. I had been working with Git on the command line and couldn’t remember how to make a pull request.
Solution: Pull requests are made on GitHub, not the command line. Viewing the repository on GitHub got to “Pull requests”>”New Pull Request” and choose the branch you want to want to merge into and the feature branch. Then click : “Create pull request”.
Problem 4: Get a remote branch and checkout to it.
Solution: git switch <branch-name>