Introduction
Maintaining different versions of code within the same project can be essential for several reasons, such as experimenting with new features, creating separate development tracks, or archiving older versions of your software or website. GitHub, with its powerful version control features, makes this remarkably easy. Let's learn how to push two separate code versions within your GitHub repository.
Prerequisites
A basic understanding of Git concepts (add, commit, push)
An existing GitHub repository
Git installed on your local machine
Step-by-Step Guide
Clone Your Repository
Find your project's GitHub repository page and copy the HTTPS URL.
Open a terminal window and use the
git clone
command:git clone https://github.com/your-username/your-repo-name.git
Navigate to Your Project
Change your directory to the newly created project folder:
cd your-repo-name
Create a New Branch
Create a separate branch to house your new code version:
git checkout -b new-code-version
Replace
new-code-version
with a descriptive name (e.g.,experiment-feature
,template-update
).The easiest way to verify your current Git branch is using the terminal (or command prompt). Simply type
git branch
. This command will list all your local branches and mark your current active branch with an asterisk (*
). Additionally, thegit status
command often includes the current branch name in its output, providing another quick way to check.
Update the Code
- Replace your old code with your new code version within this new branch. Feel free to delete the old code and copy in the new files or make changes directly.
Stage Your Changes
Prepare your changes for committing:
git add .
Commit Your Work
Record your changes with a clear commit message:
git commit -m "Added new website template"
- Adapt the commit message to accurately describe your changes.
Push to GitHub
Send your new branch to your remote GitHub repository:
git push origin new-code-version
Important Notes
Remote Names: The
origin
remote is the default, but make sure to adjust the command if you use a different name.Verification: Visit your GitHub repository to ensure the new branch and your code are present and correct.
Why Branches are Your Best Friend
Branches provide a clean and efficient way to maintain separate lines of development. Some key benefits:
Isolation: Changes in one branch won't interfere with your other versions.
Experimentation: Safely try out new ideas without breaking your main codebase.
History: Clear records of how each version evolved.