0% found this document useful (0 votes)
39 views

Git + Github + GitLab

Uploaded by

aznabi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Git + Github + GitLab

Uploaded by

aznabi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Hassensha– As discussed,

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

Problem Statement – Give a detailed description of the problem statement.


Why does that occur – Give a detailed write up of why that will occur
Fix – What is the fix for that issue – Give a detailed write up

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.

Problem Statement- What is a Git Merge Conflict?


Why does that occur- A merge conflict is an event that takes place when Git is unable to
automatically resolve differences in code between two commits. Git can merge the changes
automatically only if the commits are on different lines or branches

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.

Problem Statement- Spelled last commit message wrong


Why does that occur- After a good few hours of coding, it’s easy for a spelling error to

sneak into your commit messages.

Fix - Luckily, there’s a simple fix.


git commit --amend

This will open up your editor and allow you to make a change to that last commit message.

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- Spelling mistake on branch name


Why does that occur-

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:

git push origin --delete feature-brunch

git push origin feature-branch

------------------------------------------------------------------------------------------------------------------------------
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

are all sitting on the master branch.

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!

Git log << to get the commit-id>>


Git revert commit-id
Once the changes reverted you can create a new branch and push changes to new branch
Git checkout –b “branch-name”

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.

Problem Statement- Added a wrong file in the repo


Why does that occur- But what if you do the exact opposite? What if you added a file that

you didn’t want to commit? A rogue ENV file, a build directory, a picture of your dog that you

accidentally saved to the wrong folder? It’s all fixable.

Fix - If all you did was stage the file and you haven’t committed it yet, it’s as simple as resetting

that staged file:

git reset /assets/img/misty-and-pepper.jpg

If you have gone as far as committing that change, no need to worry. You just need to run an

extra step before:

git reset --soft HEAD~1


git reset /assets/img/misty-and-pepper.jpg
rm /assets/img/misty-and-pepper.jpg
git commit

This will undo the commit, remove the image, then add a new commit in its place.

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- Undo local commits


Why does that occur- You have already created four new commits and only now realize that

one of these commits contains a major error.

Fix - If you want to undo one or more commits, you can use the “git reset” command. The

command knows three different modes (soft, hard, mixed):

# Undo the last four commits, keep changes


git reset HEAD ~ 4

# Undo the last four commits, discard changes


git reset --hard HEAD ~ 4

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!

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- How to fix the error below in git?


error: failed to push some refs to
Why does that occur- This error occurs when you attempt to push your local changes to
the remote repo without updating your local repo with new changes made to the remote repo.

Fix- git pull --rebase origin main


git push -u origin main

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.

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- "Your Branch is Ahead of 'origin/master' by X Commits" Warning

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.

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- "Branch Already Exists" Error

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:

git branch -D <branch>

* 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:

 The branch name is misspelled or mistyped.


 The branch has already been deleted or is no longer available.
 The branch exists in a remote repository, but it has not yet been pulled or fetched to the
local repository.

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.

Command to create a branch: git checkout -b <branch-name>


------------------------------------------------------------------------------------------------------------------------------

Problem Statement- Git Error - Unable to resolve reference refs/remotes/origin/master


reference broken
Why does that occur- This error simply means information in /refs are corrupted and Git
cannot continue to create index.

Fix- Execute the following command within your project’s directory.


rm .git/refs/remotes/origin/master
git fetch

Then run git pull and it should work this time.


git pull
> Already up to date.

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.
------------------------------------------------------------------------------------------------------------------------------

Problem Statement- ! [rejected] first_features -> first_features (non-fast-forward

error: failed to push some refs to 'https://github.com/geekelo/Hello-World.git'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

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:

3) Switch to the branch you want to update:


git checkout first_features
2. Pull the latest changes from the remote branch:
git pull origin first_features
Merge the changes from the remote branch into your local branch:
git merge origin/first_features
4. Resolve any conflicts that arise during the merge.
5. Push your changes to the remote branch:
git push origin first_features
By pulling and merging the latest changes from the remote branch before pushing your
changes, you ensure that your changes are based on the latest version of the code, and that
you don't overwrite any changes made by someone else.

------------------------------------------------------------------------------------------------------------------------------
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,

error: You have local changes to "X"; cannot switch branches.

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

If you want to discard the changes,

git checkout -- <file>


git checkout branch

If you want to keep the changes,

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

Problem Statement- error: The branch 'changes' is not fully merged.


If you are sure you want to delete it, run 'git branch -D changes'.

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

Which is equivalent to:


git branch --delete --force changes

You will see a success message indicating that you deleted the branch:

Deleted branch changes (was 3527e57).

------------------------------------------------------------------------------------------------------------------------------

Problem Statement- fatal: Unable to create 'your_project_path/.git/index.lock': File exists.

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.

Fix- What should be the resolution to get rid of this error:


 Open terminal & go to the path where your project is.
 Type ls command and check if there is a .git folder. This is a hidden folder. Make sure
you have enabled flag to see hidden files. If not check this blog
 Type cd .git it will enter into the .git directory.
 Type ls command to check the files.
 You will see there is a file index.lock
 Type rm -f index.lock command to delete the file. This will delete the file. Make sure
you have the ownership. Otherwise Type sudo rm -f index.lock Enter password.
 Type cd ..
 Type git commit to commit again

Problem Statement- Accidentally Deleting an Entire Git Branch

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.

git checkout -b <branch-name> <commit-id>

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.

Problem Statement- Git’s repository not found error

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”

2. Your password has changed


Have you changed your password lately? If you connect from a Microsoft-based workstation,
the Windows Credentials Manager may transparently submit an old password every time you
clone, pull or fetch. Make sure your computer doesn’t cache any old, out of date passwords and
cause your connection to be rejected.
3. You are not a collaborator
You may authenticate successfully against GitHub or GitLab, but if you haven’t been made a
collaborator on the project, you won’t be able to see the repository and will again trigger the
fatal: repository not found exception. If you’re not a collaborator on the project, contact one of
the GitHub or Bitbucket repository administrators and have them add you to that role.
4. Incorrect case or a word misspelled
If your source code management tool is hosted on a Linux distribution, the repository name may
be case sensitive. Also watch out for creative repository spellings, such as a zero instead of the
letter O, or a one in place of the letter L. If you can copy and paste the git clone command from
provided documentation, do that.
5. The git repository has been deleted
If the repository was deleted or renamed, you’ll obviously hit a Git repository not found error
when you attempt to clone or fetch from it. If all else fails, check with the team lead to ensure
that the remote repository does indeed still exist. One way to fix that problem is to log into your
DVCS tool as an administrator and actually GitHub or Bitbucket repository.

------------------------------------------------------------------------------------------------------------------------------

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:

CONFLICT (content): Merge conflict


error: could not apply d81b1f0

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

Fix- How to resolve a Git Cherry Pick merge conflict

1. Find the files that failed the Cherry Pick


Run git status to get more information about which specific files within the commit failed to
merge during cherry-pick.

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.

Example: Unmerged paths:


(Use "git add <file>..." to mark resolution)
both modified: config.yaml
In the example above, I need to manually resolve conflicts in config.yaml; however, you may
have more than one file to fix.
2. Open each file and manually resolve the merge conflict
Open the file in the text editor of your choice (in this example I’ll use vim):

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.

3. Stage each file


Stage each file that you manually edited. This indicates to git you’ve resolved the conflicts in
these files:

git add config.yaml


4. Continue the Cherry Pick operation
git cherry-pick continue
Git will then kick you into your default text editor to specify a commit message for the new
commit. Add a commit message and save the file (if you’re default editor is vim the command to
save & quit is ESQ :wq)

Done!

How to cancel a Git Cherry Pick?


If you run the cherry-pick command and find yourself with more merge conflicts than you
bargained for (or just want to abort the cherry pick operation), simply run:

git cherry-pick –abort


Problem Statement- Fix "cannot 'squash' without a previous commit" for Git
Why does that occur- The error can be triggered by a number of problems including:

 Incorrect HEAD option.


 Incorrect target commit
 Improper logs in your Git commit history.

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 rebase -i HEAD~X


X in the aforementioned command is the HEAD number your commit requires.

GIT EXERCISES

 Create and setup git in your local machine.

 Create a GitHub account and configure it ?

 Create some files and try to push and pull from repository to local.

 Create multiple branches and play around all the branches.

 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?.

 Git stash and use different stash commands.

 Explore more on Git rebase and use rebase while pulling change

 Try to integrate commits from one branch to other using cherry-pick

 Crate a webhook in git to trigger a Jenkins build whenever code is committed to


repository

 Use log command and undo particular commit you have previously committed

 Create a merge conflict and resolve it

You might also like