NOTE: The -d option only deletes the branch if it has already been merged.To delete a local branch, run either of these commands:.git push origin -delete my-branch-name.To delete a remote branch, run this command:.Refer to Handling Merge Conflicts (the next exercise) to learn what to do. NOTE: When you merge, there may be a conflict.Now you can merge another branch into the current branch. NOTE: Replace master with another branch name as needed.If you're not already on the desired branch, run this command: First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch).You'll want to make sure your working tree is clean and see what branch you're on.If your local branch already exists on the remote, run this command:.This saves you from having to type out the exact name of the branch! NOTE: HEAD is a reference to the top of the current branch, so it's an easy way to push to a branch of the same name on the remote. If your local branch does not exist on the remote, run either of these commands:.git checkout -track origin/my-branch-name.Run this command to switch to the branch:.To get a list of all branches from the remote, run this command:.Switch to a Branch That Came From a Remote Repo You're now ready to commit to this branch.Run this command (replacing my-branch-name with whatever name you want):.To see all local and remote branches, run this command:.To see remote branches, run this command:.To see local branches, run this command:.NOTE: The current local branch will be marked with an asterisk (*). The commands below assume you've navigated to the folder for the Git repo. When you're done, you merge the new feature branch into the master branch, and both the new feature and rush change are kept! For All the Commands Below Then you can switch back to your new feature branch and finish your work. You switch back to the master branch, make the change, and push it live. You haven't finished your new feature, but you get a request to make a rush change that needs to go live on the site today. You create a new branch and start working. Let's say you need to work on a new feature for a website. Here's an example of how Git branches are useful. This lets you more easily work with other developers, and gives you a lot of flexibility in your workflow. Our good friend git checkout is the right tool for the job.Git lets you branch out from the original code base. The simplest thing that could possibly workĪs it turns out, we’re trying too hard. Maybe, but I think we might have our Git license revoked if we resort to such a hack. When in doubt, pull out the brute force approach? Surely we can just check out the feature branch, copy the files we need to a directory outside the repo, checkout the master branch, and then paste the files back in place. But we want to be done with this task in ten seconds, not ten minutes. Maybe we can just merge the whole branch using -squash, keep the files we want, and throw away the rest. You’re thinking of git add -interactive (which won’t work for our purposes either). We could hunt down the last commit to each of these files and feed that information to git cherry-pick, but that still seems like more work than ought to be necessary. We just want to grab these files in their current state in the feature branch and drop them into the master branch. We don’t want to have to track down all the commits related to these files. git cherry-pick wants to merge a commit - not a file - from one branch into another branch. The team has made numerous commits to the files in question. Isn’t this exactly what git cherry-pick is made for? Not so fast. This seems like it should be a simple enough task, so we start rummaging through our Git toolbox looking for just the right instrument. The code you need to grab is isolated to a handful of files, and those files don’t yet exist in the master branch. (For this example, we’ll assume mainline development occurs in the master branch.) You’re not ready to merge the entire feature branch into master just yet. Something comes up, and you need to add some of the code from that branch back into your mainline development branch. They’ve been working on the branch for several days now, and they’ve been committing changes every hour or so. Part of your team is hard at work developing a new feature in another branch.
0 Comments
Leave a Reply. |