Setup Git Repository
You can host your code on GitHub, Bitbucket or can create your own Git repository.
Latest versions Ubuntu (I have tested from 22.04) comes pre-installed with Git, for windows or other systems, install git client - git-scm.com/downloads
You can verify it with git --version if not installed it can be install using
sudo apt update
sudo apt install git
GitHub
-
Create a User account
-
Setup your user for SSH password-less entry by copying your
User > SSH and GPG keys > New SSH key
copy output of more ~/.ssh/id_rsa.pub and paste it as your new key. If ~/.ssh/ is empty then create SSH keys first.
bash
$ ssh-keygen
Create a new repository
- Create a new repository, I am creating a repository called temporary
- Create a local folder called temporary, initialise git, add README.md file, commit locally and push first commit to GitHub.
mkdir temporary
echo "# temporary" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:shahzadqadir/temporary.git
git push -u origin main
Add a file to Repository
- Make changes to file
- Stage changes to be committed later
git add . # stage all files
Commit files
git commit -m "message to use to commit"
Only staged changes will be committed.
If multiple changes are done to a file, some staged and some not staged, only staged changes will be committed.
Viewing History
git log
Branches
A branch can be used for a new feature or bug fix.
Create and checkout a new branch
$ git checkout -b new_branch
Work on an existing branch
Start working on a new branch that is created online but don't exist locally.
# If the branch exist on Github but not locally yet.
$ git pull
From github.com:shahzadqadir/temporary
* [new branch] feature/pytest -> origin/feature/pytest
Already up-to-date.
╭─shahzad@devpc ~/professional/repos/learn_linux/temporary ‹main●›
╰─$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
git pull imported the branch locally but we are still in main. To start using that branch, we need to do a checkout
$ git checkout feature/pytest
error: Your local changes to the following files would be overwritten by checkout:
README.md
Please commit your changes or stash them before you switch branches.
Aborting
╭─shahzad@devpc ~/professional/repos/learn_linux/temporary ‹main●›
╰─$ git add . 1 ↵
╭─shahzad@devpc ~/professional/repos/learn_linux/temporary ‹main●›
╰─$ git commit -m "updated README.md"
[main 4d525c3] updated README.md
1 file changed, 2 insertions(+)
╭─shahzad@devpc ~/professional/repos/learn_linux/temporary ‹main›
╰─$ git checkout feature/pytest
Branch 'feature/pytest' set up to track remote branch 'feature/pytest' from 'origin'.
Switched to a new branch 'feature/pytest'
You will need to commit or stash your changes in current branch before checking out to any other branch.
Create and Checkout a branch using command line
# Checkout to branch from which you want to branch off
$ git checkout main
# Create branch and checkout at the same time
$ git checkout -b bugfix/login-error
# Make some changes and then do a push
$ git push --set-upstream origin bugfix/login-error
[!IMPORTANT]
CI/CD Workflow: work in a feature/bug-fix branch, pipeline is triggered when a merge to main initiated.
Merge/Pull Request
# Checkout to new branch
$ git checkout feature/pytest
# Make changes
$ echo "#NEW feature" > feature_description.md
# Stage and Commit changes
$ git add .
$ git commit -m "created feature description"
# Push changes to github
$ git push
When I login to GitHub I should be able to see this:

I should be able to create a pull request and merge the changes.
Step 1: Compare the changes and create a pull request
**Step 2: ** Someone from the team will review the pull request and will do a merge pull request which will merge the changes to main repo.
Delete a Branch
Once the feature/bug-fix branch is merged into main, it is good idea to delete the branch as it is no longer needed.
# Change to main branch
$ git checkout main
# Pull changes
$ git pull
# Delete branch
$ git branch -d feature/pytest
# Delete it on GitHub
$ git push origin --delete feature/pytest
[!IMPORTANT]
Before deleting a branch, make sure it is merged into main.
What to Ignore
To ignore any files/folders, add .gitignore file
Usually these files/folders along with other files/folders need to be ignored.
- build files
- any files generated by editor
.idea/*
build/*
If a folder was already included and we don't want it in git any more
# we didn't include a .gitignore in the begining, and now our intelliJ .idea folder is also getting tracked, we added it in .gitignore, but will also need to remove it from git.
$ git rm -r --cached .idea
$ git status
# all files in .idea folder should show deleted now
$ git commit -m "removed .idea files"
$ git push
Git Stash - To save work in progress
# Save all my changes before checking out to main
$ git stash
# Check out main
$ git checkout main
# Back in my branch, and bring back my changes
$ git stash pop
For example, I am working on a branch, doing some changes but now I want to checkout to another branch and don't want to commit these changes. If I go to main branch I will get a message I should either stash or commit my changes. I can stash my changes, checkout to main and the branch I wanted to checkout and once I am done, I can come back to my original branch and do a git stash pop and that will bring back my changes.
Revert Changes
You can revert changes by moving the HEAD to the position up to where you want to revert to.
$ git reset --hard HEAD~1
HEAD is now at e07c6b0 Merge pull request #2 from shahzadqadir/feature/pytest
╭─shahzad@devpc ~/professional/repos/learn_linux/temporary ‹main›
╰─$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
I have just discarded one change and moved git head one step back. When you revert changes that were already on GitHub, git will ask you to pull from remote repo, once you do that you will have your changes back again.
To reset remote as well
git push --force
If other people are doing changes to the same branch, moving HEAD and forcing push will cause problems to other developers if they have pulled your branch.
In that case, use git revert git_hash that creates a new commit instead of completely removing last changes and moving back to old commit.