$ git log --oneline
A branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched.
You can later choose to combine these changes in whole or part with the another branch or the mainline of development, or not.
Text
Text
image source: Atlassian git branching tutorial
$ git switch -c favorite-component
On the main branch:
Make sure your local main branch is up to date with the remote main branch by pulling down:
$ git pull origin main
Now you're ready to create a new branch. Name it something that reflects the work you intend to do on this branch. Let's say you're building a component that allows you to favorite a post; you might name your branch favorite-component.
☝️ This command will do 2 things:
This is the shared branch that you deploy to production. Feature branches are merged in via pull requests.
This is where you will do your development work. You'll push changes to this branch, and open a pull request to main when you're ready to let other see your work.
branch name: MAIN or PRODUCTION
branch name: <something-descriptive-of-what-you-are-building>
branch name: MAIN or PRODUCTION
It makes sense to have a branch that is deployable at all times. Merge the development branch here when it's ready to deploy.
Don't introduce broken code to the production branch!
This is where you will do your development work. Push this up and open a pull request to the shared development branch.
branch name: DEVELOPMENT
Merge separate feature branches to this branch for testing. It gives you a place to push changes and share work.
(similar to the GitLab Flow workflow)
* This is basically the GitHub Flow.
$ git pull origin main
☝️ Fetches the latest commits from remote main and merges them into your local main
Get the latest code from GitHub (remote) into your local repo (on your computer).
$ git switch -c this-rad-feature
☝️ Creates the new branch called this-rad-feature
and switches you to it.
Your prompt should change to show that you are on the new branch.
Create a new branch, branching off from main.
You should be on the main branch when you run this command.
You should be on the main branch when you run this command.
$ git pull origin main
This process is cyclic, so we'll jump in assuming that you are already working on a feature branch.
☝️ Fetches the latest commits from remote main and merges them into your local main
Get the latest code from GitHub (remote) into your local repo (on your computer).
⚠️ There should never be any commits on your local main that don't exist on the remote main, because no one is ever working directly on the main branch. Therefore, you should never have a merge on the main branch.
You should be on the main branch when you run this command.
$ git merge main
👆This command merges your local main into the feature branch.
You want to develop your feature in the context of the latest code, so you will have to keep merging the commits that are on main into your feature branch.
You should be on your feature branch when you run this command.
⚠️ The only way commits from other branches get onto your branch is via main. You'll need to be strategic about when you merge the commits from main -- it's up to you to decide when you need that work.
Don't pull remote main directly into your feature branch.
Don't merge other branches directly into your feature branch.
If there are merge conflicts, resolve them here on the feature branch.
Why? This lets other people see your branch and even pull it down to run for themselves.
$ git push origin feature-branch
👆When you run this command the first time, it creates a copy of the branch on GitHub. When you run it again, any commits that you have on your local branch will be transmitted to the remote copy of the branch. You can run it multiple times without messing anything up.
No one else can see what you have on your branch until you create a remote copy of your branch.
You should be on your feature branch when you run this command.
When? At any time in order to share what you're working on, but you have to do it before you can open a Pull Request.
✅ Finished all the code needed for this feature?
✅ Merged the most recent commits from main into my feature branch?
✅ Tested that the code works before and after merging the main branch?
✅ Need feedback from someone else?
When you've finished writing the code for a feature, you will want to propose merging your code into the main branch. A Pull Request allows teammates to review and agree to merge your code.
Merging your branch means that your branch and all its commits have been included in the main branch on GitHub.
Then, you can create a new branch for the work you intend to do next.
Once a branch has been merged, you should delete that branch. GitHub will give you a button to delete the remote branch, and to delete the branch locally you can run:
git branch -d name-of-branch
⚠️ Don't continue working on a branch once it has been merged.
Merging brings two lines of development (branches) together, still preserving the history of each commit
$ git checkout main $ git pull origin main
$ git checkout feature-branch $ git merge main
When new code has been merged to remote main but you don't have it locally yet.
Once local main is up-to-date you can use it to update feature branches.
When you are ready to incorporate your feature into the main line
Conflicts occur when changes to the same line or group of lines occurred in each branch, and Git doesn't know which to choose.
Here are lines that are either unchanged from the common ancestor,
or cleanly resolved because only one side changed.
<<<<<<< HEAD
Your changes are reflected here in this section.
=======
Their changes are here in this section, in conflict with yours.
>>>>>>> other-branch
And here is another line that is cleanly resolved or unmodified.
<<<<<<<
indicates the start of a specific conflict
======= separates the two sets of changes that are in conflict
>>>>>>> indicates the end of a specific conflict
The part above the ======= shows your changes,
and the part below shows the changes from the branch you're merging in.
Git marks where the conflicts are in the file with special characters.
All you need to do is edit the file that the conflict is in so that the file is the way you want it to be.
Delete everything that should not be there when the code runs, including the conflict markers Git put in the file.
Then, save the file and go back to the command line.
Add (stage) the file(s) that you just edited to fix the conflict. Use git status
to help you.
Finish by committing.
Create a pull request when you want a collaborator to look at and approve changes on your branch.
GitHub: How to create a pull request
REVIEWERS:
I have not seen this syntax before. What do these three dots do here?
I didn't know you could do this in Python. Nice!
There's a method in the helpers file that might do what you want here (link-to-method).
I notice your template isn't using this variable, so I think you can safely remove it from the view context.
...and the file has gone back to its state at the previous commit 🎉
If you haven't added (staged) files or committed yet.
👉🏾 Your changes are gone from the working tree (and your editor)
Replace this path with the path to the file that has the changes you want to get rid of.
$ git restore books/models.py
👉 The file is removed from staging, but your working copy will be unchanged.
$ git reset books/models.py
When you want to keep the changes in your working tree
$ git restore books/views.py
Your changes are gone from the working tree.
Reset the working tree to its state at the last commit
You can reset a file first and then discard all changes:
$ git restore books/views.py
$ git reset books/views.py
Or, if you want to discard everything, use the --hard option:
$ git reset --hard
Rewind, but learn to live with your regrets.
$ git revert 53d23c4
👆🏽Notice that the original bad commit is still there, but now you also have another commit that undoes the changes introduced by the original one.
$ git log --oneline
👆🏽Your default text editor will open; you can just save it as is.
$ git log --oneline
PULL OFTEN so you will have your colleagues' work.
PUSH OFTEN to keep your remote branch up to date.
COMMIT OFTEN to collaborate effectively.
PULL REQUEST so team members can review your work.
MERGE feature branches to main on GitHub
It's your job to keep your feature branch up to date with main.
Tell your team when you are pushing up changes or have opened a pull request that needs to be reviewed.
Say why a change is needed or what it does.
Make sure your local and remote branches are in sync.