git returns ” is a merge but no -m option was given.”

Error:

git revert 75a690a642b71768dad36ade65839a6da62d8d6a
error: commit 75a690a642b71768dad36ade65839a6da62d8d6a is a merge but no -m option was given.
fatal: revert failed

Solution:

run the following command in the terminal:

git cat-file -p 75a690a642b71768dad36ade65839a6da62d8d6a

Output:

git cat-file -p 75a690a642b71768dad36ade65839a6da62d8d6a
tree 6ee9397d91ed42ad3d130b93c5c5587d1ad6c67c
parent 95beda2bfbfa1523cb14c1285d80a5faa9b7f6f0
parent a3e9ddabca322e52b141a001dc5eb9c2c789ff22

run

git revert 75a690a642b71768dad36ade65839a6da62d8d6a -m 1 

if you want to use the first parent as mainline, choose 1, else choose 2

Git: Revert commit and see what was change in a commit

Revert is usually a safe operation as it create a new commit.

git revert <SHA>

Before you revert a commit, you might want to check what was change in the commit as you might want to keep changes that were lumped in the commit:

git show SHA

If you made a commit in the wrong branch:

Go to proper branch and run: 

git cherry-pick <HASH> 

go to old branch, run: 

git revert <HASH>

In case of error see: https://blog.seobytes.eu/development/git-returns-is-a-merge-but-no-m-option-was-given/

Create remote repository from local (bitbucket/github)

Command to use:

Set up bitbucket ssh access.

Open a terminal in local directory and run:

git init
git checkout -b feature-branch

Create repository in Bitbucket and get the ssh url by clicking on the clone button.

In yout terminal run:

git remote add origin git@bitbucket.org:nicolashoquet/arcade.git

In case “origin” already exists, you can rename origin by running this command:

git remote rename origin https-origin

or use another name for origin (e.g. ssh-origin).

Set git ssh access for github or bitbucket

https://docs.github.com/en/github/getting-started-with-github/setting-your-username-in-git

Open a terminal

cd in the folder were you want to clone the repo:

$ cd directory/you/clone
$ git init
$ git config user.name “Your-Username” #can be different from your github username
$ git config user.email “your.email@example.com
$ ssh-keygen -t rsa -C “your.email@example.com

Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username.ssh/id_rsa

Copy the content of the id_rsa.pub in your Github settings in your profile.

Set Github local authentication to be able to clone and push:

http://kbroman.org/github_tutorial/pages/first_time.html

How to solve merge conflict in git cli?

$  git merge TEST_2
 Auto-merging test.txt
 CONFLICT (content): Merge conflict in test.txt
 Automatic merge failed; fix conflicts and then commit the result.
$git mergetool --tool-help
'git mergetool --tool=' may be set to one of the following:
araxis
vimdiff
vimdiff2
vimdiff3
...
$ git mergetool --tool=vimdiff

Git and Github cheatsheet

Getting started:

1. Installing Git

sudo apt-get install git

2. Configuring Git

$ git config --global user.name "My Name"

3. Creating a new repository – git init

$ cd Desktop/git_exercise/
$ git init

4. Checking the status – git status

This command will return the current branch as well as it’s status compared to the head.

$ git status

5. Staging

The following command add the file hello.txt to git. This file will mbe included in the next commit.

$ git add hello.txt

or

$ git add -A

equivalent to

$ git add .

The two commands above add all files to git, except the files that are excluded due to git ignore rules.

6. Committing

$ git commit -m "Initial commit."

7. Modify the last commit message:

$ git commit --ammend

Start your Git repo anew

To remove the git file and create anew in the repo:

cd /path/to/your/repo
rm -rf .git
git init

Add a remote repository

In general:

$ git remote add origin https://github.com/user/repo.git

See https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories for more details.

Push to remote:

If it’s a commit on the master branch:

$ git push origin master

If it’s a commit on some branch named “some-branch”:

$ git push origin some-branch

Pull request

In our workflow we shall send code to CR (code review) before it goes to QA.

Create a branch

For that you create a branch, then push the code to your branch and the pull request is done in the master branch dashboard.

$ git branch some-feature 
$ git checkout some-feature

You can create the branch and go in the branch in command:

$ git checkout -b some-feature

# Edit some code

$ git commit -a -m "Add first draft of some feature"
$ git push origin some-branch

“After Bitbucket has her feature branch, Mary can create the pull request through her Bitbucket account by navigating to her forked repository and” clicking on Pull requests in the side bar and then “clicking the Pull request button in the top-right corner.“

Source: https://www.atlassian.com/git/tutorials/making-a-pull-request

More reading: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests

Set gitignore

https://www.atlassian.com/git/tutorials/saving-changes/gitignore

https://git-scm.com/docs/gitignore

touch ~/.gitignore
git config --global core.excludesFile ~/.gitignore
sudo vim ~/.gitignore

edit file with:

*.css.map

to exclude all files ending with .css.map

for a local approach simply create and edit the .gitignore file in the affected local repository.

If the file has already been staged, remove it by using:

$ git restore --staged <file>.

All those worked:

/source
*/.sass-cache
*/codeStyles
.gitignore

the directory tree was:

|

|-source

|-.sass-cache

|-.idea

|-codeStyles

If you accidentally sync and can’t push anymore:

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.

create a force push:

git push -f origin branchname

Set git Locally

https://docs.github.com/en/github/getting-started-with-github/setting-your-username-in-git

cd in the folder were you want to clone the repo:

$ cd directory/you/clone
$ git init
$ git config user.name “Your-Username” #can be different from your github username
$ git config user.email “your.email@example.com
$ ssh-keygen -t rsa -C “your.email@example.com

Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username.ssh/id_rsa

copy the content of the id_rsa.pub in your github settings in your profile.

Set github local authentication to be able to clone and push:

http://kbroman.org/github_tutorial/pages/first_time.html

List branches

List local branches

$git branch

List remote branches

$git branch -r

List all remote and local branches

$git branch -a

See what branch you are on:

$git status

Set local branch to be exactly as remote branch

git fetch origin 
git reset --hard origin/master

git clone a specific branch

git clone -b <branch> <remote_repo>

Delete a local branch:

git branch -d branch-name

or

git branch -D branch-name

in case the branch to be deleted is not fully merged.