Git is an open source versioning software.
When editing an existing project on your local machine, you need to clone it first
git clone git@github.com:USERNAME/REPO.git
cd REPOThe link can be coppied from git repository website.
The core git usage loop is: edit -> status -> add -> commit -> push
When you edit your git project the updated files are displayed with command
git statusTo start tracking files you can add them individually:
git add file1.pyor all together
git add .Then with the use of git status you can check which files are staged (added).
You can make use of .gitignore file to make sure some files are not tracked.
Generally you want to omit tracking the cache files and environment files. Usually you can just copy existing .gitignore template to the root of your project and the git will then avoid adding the
listed files to the repository.
An example of such .gitignore file is Python.gitignore.
when you have staged (added) your desired files you can than commit the changes. Commit creates a snapshot of the current state.
git commit -m "Short commit message"And lastly you can push to the repository
git push origin <branch>You almost never want to work directly on master/main/trunk branch. You will want to work on a feature branch. To create a branch:
git checkout master
git pull origin master # update first
git checkout -b new-branch-nameThis activates creates and activates the new branch new-branch-name.
After making changes you then do
git add .
git commit -m "Implemented feature on new branch"
git push -u origin new-branch-namethe -u sets the upstream so that later you can just git push.
When you want to merge the codebase in two branches you can do the following:
git checkout master # swich back to the master branch
git pull origin master # Check if some updates were submitted to the master branch
git merge new-branch-name # Merge the newely created branch into master
git pushThe merging process will let you know if there are some conflicts. If there are they must be fixed manually. If not the merge process will automatically commit the changes.
If there is a merge conflict the git merge will let you know and the conflicts will be
marked like:
<<<<<<< HEAD
your version on current branch
=======
other branch’s version
>>>>>>> new-branch-nameYou must manually remove the git marks and select which version you want to keep or rewrite it completely.
Then again you need to add, comit push
git add conflicted_file.py
git commit
# or
git merge --continueOn Linux systems git is usually installed out of the box.
On Windows git must be installed manually.
In order to use git effectively (and securely) one needs to configure ssh keys. This lets you use git through ssh protocol, where connection is authenticated for you automatically. Without it you must resort to https protocol, where for each api call you must enter a password.
The idea is the following:
- you create a public/private key pair on your machine,
- then you submit the public key to github,
- you give your agent access to the private key.
On Linux systems to generate the key you do the following:
- First check if you already have some keys
ls ~/.sshIf you see something like
id_ed25519
id_ed25519.pubthen you already have the keys
- If not, then you must generate the keys:
ssh-keygen -t ed25519 -C "your.email@example.com"Then press enter to choose the default options and the keys will be generated for you.
- Add SSH keys to your ssh agent
# Start the ssh-agent
eval "$(ssh-agent -s)"
# Add the new private key
ssh-add ~/.ssh/id_ed25519- Add the public key to github
cat ~/.ssh/id_ed25519.pubCopy the whole line and add paste it to the github
Github -> Settings -> SSH and PGP keys -> New SSH key
The process for windows is the same as for linux, but inside of the gitbash
To enable the windows SSH Agent to persist when you work across VS Code or other programs
enable the agent services.msc, OpenSSHE Authenitication Agent, properties, startup type -> Automatic