Git + Github + GitLab
Git + Github + GitLab
1) Please document the real time issues that a DevOps engineer faces with Git,
Github and Gitlab. Please focus on the issues that occur the most.
2) Mini Projects that we can have the freshers do to gain expertise with Git, Github
and Gitlab.
For real time issues – Below is the format. Please make it as detailed as possible so that
freshers can easily understand them
GIT :
Git is a distributed version control system that tracks changes in any set of computer files,
usually used for coordinating work among programmers collaboratively developing source code
during software development. Its goals include speed, data integrity, and support for distributed,
non-linear workflows
GIT-HUB:
GitHub is a code hosting platform for version control and collaboration. It lets you and others
work together on projects from anywhere.
GIT-LAB:
GitLab is an open source code repository and collaborative software development platform for
large DevOps and DevSecOps projects. GitLab is free for individuals. GitLab offers a location
for online code storage and capabilities for issue tracking and CI/CD.
Fix -There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.
The easiest way to resolve a conflicted file is to open it and make any necessary
changes.
After editing the file, we can use the git add a command to stage the new merged
content.
The final step is to create a new commit with the help of the git commit command.
Git will create a new merge commit to finalize the merge.
This will open up your editor and allow you to make a change to that last commit message.
------------------------------------------------------------------------------------------------------------------------------
Fix - We rename this branch in a similar way to how we rename a file with the mv command:
by moving it to a new location with the correct name.git branch -m feature-brunch feature-
branch
If you have already pushed this branch, there are a couple of extra steps required. We need to
delete the old branch from the remote and push up the new one:
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- Accidentally committed all changes to the master branch and need
to revert.
Why does that occur- So you are working on a new feature and in your haste, you forgot to
open a new branch for it. You have already committed a load of files and now those commits
Fix - So we can roll back all those changes to a new branch with the following three commands
Note: Make sure you commit or stash your changes first, or all will be lost!
This creates a new branch, then rolls back the master branch to where it was before you made
changes, before finally checking out your new branch with all your previous changes intact.
you didn’t want to commit? A rogue ENV file, a build directory, a picture of your dog that you
Fix - If all you did was stage the file and you haven’t committed it yet, it’s as simple as resetting
If you have gone as far as committing that change, no need to worry. You just need to run an
This will undo the commit, remove the image, then add a new commit in its place.
------------------------------------------------------------------------------------------------------------------------------
Fix - If you want to undo one or more commits, you can use the “git reset” command. The
Caution: The “hard” mode should be used with a bit of caution. As soon as you execute “git
reset — hard”, the working tree and the index are reset. All changes are then lost forever!
------------------------------------------------------------------------------------------------------------------------------
If the first command above runs successfully, you should get a response that says: Successfully
rebased and updated refs/heads/main.
The second command pushes your local repo's current state to the remote branch.
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- "Fatal: Not a Git Repository" Error
Why does that occur- One of the most common errors Git users encounters is the "fatal: not
a git repository" error. This error occurs when Git cannot find a repository in the current directory
or any of its parent directories. This error typically occurs when you try to run a Git command in
a directory that is not a Git repository.
Fix- To fix this error, make sure you are in the correct directory that contains a Git repository.
You can verify this by running the "git status" command, which should return information about
the current repository. If you are not in a Git repository, you can either create a new repository
using the "git init" command or clone an existing repository using the "git clone" command.
------------------------------------------------------------------------------------------------------------------------------
Why does that occur- If you see a warning that says "Your branch is ahead of
'origin/master' by X commits," it means that you have made changes to your local repository that
have not been pushed to the remote repository yet.
Fix- To fix this warning, you need to push your changes to the remote repository using the "git
push" command. If you are working on a branch other than master, you can use the following
command to push your changes to the remote repository:
git push origin <branch>
Problem Statement- "Failed to Merge" Error
Why does that occur- If you try to merge changes from one branch to another and
encounter a "failed to merge" error, it means that Git was unable to automatically merge the
changes.
Fix- To fix this error, you need to manually resolve the conflicts that Git was unable to merge.
You can do this by using a merge tool, as described in the "Merge Conflicts" section above.
Once you have resolved the conflicts, you can commit the changes and push them to the
remote repository.
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- "Could Not Find Remote Branch" Error
Why does that occur- If you are trying to fetch changes from a remote repository and
encounter a "could not find remote branch" error, it means that Git is unable to locate the branch
you are trying to fetch.
Fix- To fix this error, you need to make sure that the remote repository has the branch you are
trying to fetch. You can do this by running the following command:
git branch -r
This command will list all of the branches in the remote repository. If the branch you are trying to
fetch is not listed, it may not exist in the remote repository. If this is the case, you can create the
branch in the remote repository using the "git remote " command.
------------------------------------------------------------------------------------------------------------------------------
Why does that occur- If you try to create a new branch in Git and encounter a "branch
already exists" error, it means that a branch with the same name already exists in the
repository.
Fix- To fix this error, you need to either delete the existing branch or choose a different name
for your new branch. To delete the existing branch, you can use the following command:
* Replace "<branch>" with the name of the branch you want to delete. Once you have deleted
the branch, you can create your new branch using the "git branch" command.
Problem Statement- How to Fix “error: pathspec ‘…’ did not match any file(s) known to
git”?
Why does that occur- This error message indicates that Git was unable to checkout the
specified branch because it does not exist. This can happen for a few different reasons,
including the following:
Fix- To fix this error, you will need to verify that the branch name is correct and that the branch
exists in the local repository. If the branch name is correct and the branch still does not exist,
you may need to pull or fetch the branch from the remote repository where it exists.
If the branch has already been deleted or is no longer available, you will need to create a new
branch with a different name or switch to a different existing branch.
Note: Use the command for any branch that’s giving the error. For example, if I’m getting error
for the main branch instead, I could do the following:
rm .git/refs/remotes/origin/main
git fetch
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- git add . -> Still “nothing to commit” With New Files
Why does that occur- When the Git users tracked the changes through the “$ git add .”
command, sometimes these changes are not added to the staging area However, they exist in
the repository list of content.
Fix- To solve this conflict, developers need to add changes through the “$ git add –all”
command.
------------------------------------------------------------------------------------------------------------------------------
hint: Updates were rejected because the tip of your current branch is behind
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Why does that occur- This error message indicates that you are trying to push changes to a
Git branch, but the branch has been updated by someone else in the meantime. Git is
preventing you from overwriting the changes that someone else has made.
Fix- To resolve this issue, you should first pull the latest changes from the remote branch into
your local repository, merge them into your local branch, and then push your changes again.
Here are the steps:
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- I was working on a git branch and was ready to commit my changes, so
I made a commit with a useful commit message. I then absentmindedly made minor changes to
the code that are not worth keeping. I now want to change branches, but git gives me,
Why does that occur- The reason why you were not able to switch branch as you have
unsaved work in your current branch.
Fix- To resolve this issue, you must either discard the changes or else stash the changes and
later once the work in other branch is done you can resume from where you have left
git stash save this commnad will stash the unsaved changes
git checkout branch this command will help to switch branch
git stash pop this command will unstash the chnages and you can resume your work
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- The “fatal: refusing to merge unrelated histories” Git error
Why does that occur- error occurs when two unrelated projects are merged (i.e., projects
that are not aware of each other’s existence and have mismatching commit histories).
Fix- The error is resolved by toggling the allow-unrelated-histories switch. After a git pull or git
merge command, add the following tag:
git pull origin master --allow-unrelated-histories
Why does that occur- This error indicates that the “changes” branch has content and
modifications that have been not been merged, therefore these changes would be lost if you
delete the branch.
Fix- The second line informs you that if you wish to proceed with the deletion you can use the -
D option to “force” a deletion.
git branch -D changes
You will see a success message indicating that you deleted the branch:
------------------------------------------------------------------------------------------------------------------------------
Why does that occur- If no other git process is currently running, this probably means a git
process crashed in this repository earlier. Make sure no other git process is running and remove
the file manually to continue.
Why does that occur- Working on a branch for a long time and, in the end, accidentally
deleting it without merging it can sound like the biggest horror for developers. But it is not as big
of a deal as people think. You can recover that branch with just a few commands from the
terminal.
Fix- To perfectly recover the branch, you first need to find the commit from where you switched
to that deleted branch. To do that, you can use the Git reflog command
to get all the journal entries of commits and details. Once you’ve found the commit, you can use
the below command to get your deleted branch back so you can merge and push it to remote
branches.
In the above command, branch-name is the branch name that you have deleted, and commit-id
is the commit that you’ve determined from your Git reflog results.
Fix- There’s nothing worse than joining a new development team and eagerly cloning the
existing source code repo only to run head first into Git’s ‘fatal: repository not found’ error.
For those who struggle with that problem, here are five potential fixes to the frustrating
repository not found error message.
1. You did not authenticate
If you attempt to connect to a private GitHub or Bitbucket repository and fail to authenticate, you
will receive the repository not found error. To ensure you are indeed authenticating, connect to
the repository and include your username and password in the Git URL:
git clone “url”
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- How to resolve this error “404 Error - Repository Not Found”?
Fix- This error typically occurs when you try to access or clone a repository that does
not exist or is not accessible to you. Ensure that you have the correct repository URL
and the necessary permissions to access it.
------------------------------------------------------------------------------------------------------------------------------
Problem Statement- After attempting the cherry pick operation, if you have a merge conflict
you’ll see a message like this on your screen:
Why does that occur- Cherry pick applies the changes introduced by the cherry-picked
commit onto the current branch. If changes introduced by the picked commit conflict with
changes to those files on the current branch, you will see a merge conflict.
Merge conflicts can happen when a commit introduced after the branch split made changes OR
if there are unstaged changes in your local workspace that conflict with the cherry-picked
commit
git status
Note the specific files listed under the “Unmerged paths” header in the resulting message, these
are the files you need to manually fix in the next step.
vim config.yaml
Look for the conflict marker (<<<<<<<) that git added to the file during the failed cherry pick.
This marker indicates the specific places in the file you’ll need to manually edit. On top you’ll
see what that part of the file looked like on main, and on the bottom, what that part of the file
looks like in your cherry-picked commit:
<<<<<<< HEAD
Changes introduced by mainline
======
Changes added by the cherry-picked commit
>>>>>>> d81b1f0
Your goal is to manually replace this entire block, with what that part of the file should look like
after the merge (essentially manually performing the merge that Git couldn’t automatically do). If
this is confusing, watch my video explanation of this part of the process.
Once you’re done editing the file, save it, then repeat this process for any of the files identified in
step 1.
Done!
Fix- When squasing a commit, be sure to use the appropriate HEAD option. Depends on when
was your first commit, you might want to use a higher HEAD option or even –root.
The general syntax to squash a commit using the HEAD option is as follows:
GIT EXERCISES
Create some files and try to push and pull from repository to local.
Make changes in branches and merge with other branches and push it to remote
repo also.
Try to revert changes that you have committed in local and remote repos?.
Explore more on Git rebase and use rebase while pulling change
Use log command and undo particular commit you have previously committed