You can watch the youtube video explaining and demoing this topic here.
Introduction
In a previous blogpost, we learned how to make a Pull Request on bitbucket using the branching method. Sometimes after making a pull request on a code repository, you get feedback that some things in your pull request need to be changed before it will be merged.
It's not good to create a brand new pull request for your changes, because then you'll lose the comments, and it'll keep making the PR numbers go up. You should edit the current pull request. You can do that by overwriting the old commits for that PR and then pushing over the current branch. Let's see how to do that. Editing A Pull Request (And Git Squashing)
If you already have a pull request, and you want to make some changes to the code for it, you can do so by downloading your version of the project, making the changes, squashing the commits together, and then pushing back to the origin, overwriting your old branch.
The list of steps is as follows:
1. On bitbucket, look for the source code for your branch.
2. Clone the project at the commit you want to change.
git clone repolinkhere
Note that you want your repository link to be for the specific commit that you worked on for your PR's branch. (See video for tips and tricks about how to get this URL.)
3. In that local project, checkout onto the branch your Pull Request is for.
git checkout yourbranchname
4. Make the changes to the project (on your local computer).
5. Add and commit those changes. (You can do this as many times as necessary.) 5.5 If you want, you can just push all of those commits straight to the repository with "git push". (But then you might have a lot of excessive commits in your git history. The following steps will allow you to trim the commits down if you think you have too many.)
6. Squash your recent commits with your previous commits that were originally in your PR. The following command will do that.
git reset --soft HEAD~2
NOTE: That "2" will change depending on the number of commits you're squashing together. The number you should use is [the number of commits in your PR] + [the number of commits you've recently made locally]. You can check bitbucket to see the first one, and you can use the "git status" command to see the second one. See the video for an example.
7. Next, commit your new change.
git commit -m "New message for the total PR."
Of course, change the message in the quotation marks to describe your pull request.
8. Push your new commit to the branch.
git push origin +yourbranchname
Change the 'yourbranchname' to the name of the branch for your Pull Request. DO NOT remove that + sign. That + sign is going to overwrite your old branch (that should have the same name).
Since this process overwrites your old commits, the effect is that this updates the code for your existing Pull Request. So now, you can go back to the Pull Request on bitbucket, make a comment about the changes, and edit the Pull Request description to update what your new code actually does.
Conclusion
Using the "branching" technique for Pull Requests and this "squashing" technique, you can edit pull requests and have an easy process making changes to your bitbucket projects.
Bookmark this page so you have the steps and commands ready whenever you need it.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References
1. Squashing git commits.
You can watch the youtube video explaining this topic here.
Introduction
A pull request is a way for you to contribute to a project by making changes to the code of a project, and then asking the repository owner to merge your changes with the current main project.
We've done a blogpost on pull requests for github, so in this post we'll look at pull requests bitbucket. There's a different etiquette for bitbucket pull requests compared to github, so we'll need to use a different way of making the pull requests. The two ways that we can raise a pull request on bitbucket are (1) by forking the repository, or (2) by making a new branch. The Fork Way
On the bitbucket website, you can fork a repository (making a copy of your own), make your changes to your personal version of the repository, and then go back to the original repository and raise the pull request, offering your version of the code to the original repository.
This is very good for open source projects, which github is famous for, but isn't as good for bitbucket. Nevertheless, the process is:
This method works, but has a lot of drawbacks. The next way to make a Pull Request is the preferred method.
The Branch Way (Preferred)
If your project is on bitbucket, then you're likely working on a small-ish team with other people. This means that you have more freedom to edit the actual project, and so forking is unnecessary. (Your boss probably doesn't want random forks of the project all over the place. And even if your code doesn't get merged immediately, it should be part of the main project for other people to see.) For this reason, creating a branch on the project repository and making the pull request from that branch is best practice.
The process for making a pull request via making a new branch is:
1. Clone the bitbucket repository you want to contribute too.
git clone repolinkhere
2. (Make sure you're on the right branch and in the project directory, on your local computer.)
git checkout thebranchnamehere
3. Make the changes to the project, on your local computer.
4. Add and commit the changes on your computer. (Make as many commits as you want.) git add . git commit -m "Added feature XYZ."
5. Create a new branch for your changes, on your computer.
git checkout -b yourbranchname
Note: Make your branch name descriptive. For example, naming it "feature/XYZ" if it's a new feature and is about XYZ is easy to let anyone know what the branch is for.
7. Push your changes to the bitbucket repository.
git push --set-upstream origin yourbranchname
8. In the repository in bitbucket, go to the 'Pull requests' tab, and click "Create pull request".
And your PR is created. Using branches is pretty simple, and much cleaner than forking. All professional teams should be handling their pull requests this way.
Conclusion
Pull Requests on bitbucket are pretty easy. Make sure you use the branching technique when possible, though, as it becomes very relevant when wanting to edit your Pull Request.
In the next blogpost, we'll look at how to make changes to the code in your Pull Request after making it.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
To see the youtube video explaining this topic, click here.
Introduction: The Standard Library Algorithms
The C++ Standard Template Library has a bunch of functions that are useful to know, since expert programmers designed them to be efficient. You should use them every opportunity you get.
I've written a cheat sheet on github for the STL algorithms, but you need a bit of starting information to be able to use them effectively. You need to know about Pairs, Lambda Expressions, and Iterators. In this blog, I'll review them briefly so that you can use the STL algorithms effectively. C++ Pairs
Pairs are just mini containers provided by the standard library to group two variables (of any type) together.
The output of the above program is "5 srcmake". You can initialize a pair with the make_pair function, and you can access the elements by calling the variable name's first and second properties.
C++ Lambda Expressions
A lambda expression is literally just a different way of writing a function. For example, a lambda expression that checks if a number is positive is the following:
The lambda expression is named "srcLambda", it's type is "auto", and it's equal to that thing on the right.
The brackets [] capture any variables you need that exist outside of the lambda expression. The parenthesis accept whatever input the lamba expression is given, like a normal function. The inside of the lambda function does some work, and possibly returns a value. We need to pass lambda expressions to certain STL algorithms to do certain work. For example, to check count how many elements in a vector are greater than 2, we could use a lambda expression and the "count_if" algorithm.
For each element of "v", "count_if" will use call the lambda expression on that element. If the lambda expression returns "true", then count_if will add one to it's counter, and give the final answer to the "count" variable.
It's much simpler to have those lines of code than it would be to write our own loops and other variables. But what are those "v.begin()" and "v.end()" things? C++ Iterators
Iterators are just special variables that point to a certain element in a container. They seem useless for vector's since vectors can be accessed by index, but other container types (like stacks) don't have indices, and so iterators are used to indicate the place in the container.
The type of an iterator looks something like this: vector<int> v{ 1, 2, 3, 4, 5 }; std::vector<int>::iterator it = v.begin();
An iterator that's for a vector<int> container named "it" is equal to v's "begin()".
A vector's "begin()" will point to the first element in the vector (in this case, the element 1 (at index 0)), and a vector's "end()" will point to the element after the last element in a vector (in this case, an invisible element after 5, which would be at index 5). You can increment an iterator (move the position to the next element) with a simple ++ operation. it++;
You can also simply add to a beginning iterator to get the position you want too.
std::vector<int>::iterator it = srcVector.begin() + 4; // Accesses the 5th element in the container.
You can also subtract iterators (which can help you get a position's index).
int index = it - srcVector.begin();
Keep these simple tricks in mind and iterators won't be scary. Remember, iterators just point to the elements in a container.
The STL Cheat Sheet
You're now a C++ pro, and can use basically every STL algorithm. Go to github and look at the C++ Standard Template Library Algorithm Cheat Sheet that I made, and star/bookmark it for later.
While you're at it, glance at the Data Structure cheat sheet that I wrote, as well.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|