Git - version control system

Last edited: 30th November 2022

What is Git

"Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." Source

First time set up

Minimal set up:

git config --global user.name "Your Name"
git config --global user.email youremail@example.com

Optional git aliases:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

Useful links when working with Git

git init  # For initialising a new project
git add .
git commit -am "Note about what you changed"
git remote add origin git@bitbucket.org:REPO/proj_name.get
git push -u origin main

Remove committed files

git rm file1.txt
git commit -m "remove file1.txt"

Discard uncommitted changes

If changes are made but not committed, they can be discarded.

https://stackoverflow.com/questions/5807137/how-to-revert-uncommitted-changes-including-files-and-folders

# Revert changes to modified files.
git reset --hard

# Revert changes to a specific point in time.
git reset --hard master@{"10 minutes ago"}

# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd

Branches

git branch dev
git checkout dev
# OR for the first time you can combine the commands
git checkout -b dev
# EDIT PROJECT CODE
git status
git add .
git commit -m "A note about the code changes made."
git checkout main
git merge dev
git push -u origin main

git push origin :newfeaturebranch  # Delete remote branch
git branch -d newfeaturebranch  # Delete local branch

Files to ignore

Note: For Django, remove the secret key from settings.py and put it into local_settings.py. Just in case you make your project public in the future you need to minimise security risks.

Add files to be ignored and not stored in version control by typing nano .gitignore in the project root::

*.pyc
*.pyo
db.sqlite3
*.db
.DS_Store
local_settings.py
/static
/proj_name/static/media/
*~
*.kate-swp
Agree
We use cookies to provide you with a better service. Continue browsing if you are happy with this.