Advanced git Features
git Branches
For CS course projects it is likely sufficient for you and your partner(s) to clone versions of a single main repository and then push and pull changes to it. For larger and longer existing projects, you may want to use git’s support for creating separate branches of your repository. For example, different branches could be associated with different users or with different features, and users can push, pull, and merge changes from branch to branch.
# create a new branch named mybranch and push it to the main repo:
git checkout -b mybranch
git push -u origin:mybranch
# list the current branch of your copy of the repo:
git branch
git branch -avv #verbose listing of local and remote branches
# switch to different branch: git checkout branchname
git checkout main
git checkout mybranch
# to merge changes from mybranch into the main branch
git checkout main
git merge mybranch
# if merge conflicts
git add any files that needed to be fixed up from the merge conflict
git commit
# if you want to push your new version of main that is merged
# with mybranch to the remote:
git push
# if git push fails try
git push origin main
# if you want to undo a merge
# (and best to undo before making local changes, commits, pushes, etc.)
git reset --merge ORIG_HEAD
git tags
You likely won’t need to use tags for course projects, but they are handy for long-lived projects, particularly for code that you may want to release. A tag is a way to name a snapshot of the repository. It is often useful for code releases and for tagging big version changes to code. A tag is like a static branch (you cannot update the tagged version, but you can check it out just like any branch).
# list all current tags associated with a repo
git tag -l
# create a new tag named v1.0 with a tag message (optional)
# and share a tag: push the tag to the origin to share it:
git tag -a v1.0 -m "initial version"
git push origin v1.0
# checking out a specific tagged version of the code
# (checkout v1.0 of code into a local repo named version1)"
git checkout -b version1 v1.0
# to list other data along with the commit for a specific tag
# (this will show the commit number, date and who created the tag):
git show v1.0