Skip to content

Instantly share code, notes, and snippets.

@thelbouffi
Last active September 13, 2022 14:23
Show Gist options
  • Save thelbouffi/c3ce475217a6e52fd4801b0217e4eaf9 to your computer and use it in GitHub Desktop.
Save thelbouffi/c3ce475217a6e52fd4801b0217e4eaf9 to your computer and use it in GitHub Desktop.

Git Author Identity

Set Committer Name & Email Globally

$ git config --global user.name "Taha El"
$ git config --global user.email "taha@email.com"

Set Committer Name & Email per Repository

avoid using --global

$ git config user.name "Taha El"
$ git config user.email "taha@email.com"

Set Committer Name & Email per commit

$ git commit -m "some commit message" --author="Taha El <taha@email.com>"

Set Committer Name & Email for the very last commit

$ git commit --amend --author="Taha El <taha@email.com>"

Show commit content

git show <commit_id>

Show files impacted by the commit

git diff-tree --no-commit-id --name-only -r f6d5b44

Add only some changes of a specific file (-p patch)

git add p <fileName>

Undo merge conflict

git merge --abort
git rebase --abort

Get branches

git branch -vv

To add a remote branch into your local repo

If you are trying to "checkout" a new remote branch (that exists only on the remote, but not locally), here's what you'll need:

git fetch origin
git checkout --track origin/<remote_branch_name>

This assumes you want to fetch from origin. If not, replace origin by your remote name.
On newer version of git (works well):

git fetch -a
git checkout branch-name

Create a branche locally

git branch branch_name
git checkout branch_name

or

git checkout -b branch_name

this will create the branch and check it out directly to make it active

Create a remote branch from a local branch

git push --set-upstream origin local_branch_name

Create a new branch from a specific existent branch

git checkout -b new-brch origin/existent-brch

delete a local branch

git branch -d branch_to_delete

delete a remote branch

git push origin --delete branch_name

Undo git add before commit

git reset <file>

Undo uncommited changes

git checkout -- path/to/file

checkout without -- also work: git checkout path/to/file

Undo commited changes

git revert commit_id

it will revert and commit at the same time the revert that we have made

git revert -n commit_id 
git commit -m "revert of commit"

-n stands for options, it will help us revert then we can commit that we have done

Reset changes

git reset --hard commit_id_of_last_commit_we_wanna_keep

For example we have made a revert, and we want to undo this revert, the solution is to reset hard this commit to the point where we want to go back to.

Undo git pull

First look at the point you want to undo to

git reflog

this will log :
1ef034f HEAD@{0}: reset: moving to HEAD
1ef034f HEAD@{1}: reset: moving to HEAD
1ef034f HEAD@{2}: pull origin release1: Fast-forward
f227761 HEAD@{3}: clone: from http://192.168.223.10/wakanda-tnc/backend.git

if we want to go to the ref 227761, we only need to reset hard to that ref

git reset --hard f227761

Pull from a branch that exists on remote and not on local

First fetch all the remote branches

git fetch

Then you can see all remote fetched btranches by calling git branch -r or git branch -rv
Then checkout the branch you want

git checkout branch-name

git rebase

when we want to pull modifs from another branch to our branch without loosing our local commits, we do a rebase

git checkout <branch with feature>
git pull 
git checkout <our branch>
git rebase <name of the branch with feature> # or git rebase -i branch-name (-i for interactive)

when we do that, the commits of feature are pulled and our commits are kept. However git rebase make us face a problem when we push the whole commits hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. solution is use git merge like below

alternative to git rebase

git checkout <branch with feature>
git pull
git checkout <our local branch>
git merge <branch with feature>

now if we do git push there is no problem or conflict

Merge conflicts

WAY ONE: merge feature/xxx branch into develop branch

  1. on feature/xxx branch push commits
  2. then checkout to develop (or master) : git merge feature/xxx
  3. some files will be merged correctely, the others would have a merge conflict
  4. go to vscode on the git tool, you will find all the files tracked by the vscode git tool, go to the ones with conflict and fix them (keep both, keep yours or keep others changes)
  5. click on + button of git git tool to add file (one by one). PS: for the package-lock.json when it has conflict, remove it, click npm install and git add the new package-lock.json generated
  6. commit the added files: git commit -m "fix merge conflict"
  7. push the commit into develop (actually it's the branch where you are now": git push origin develop:feature/xxx
  8. this git push will generate on terminal a link that you have to click on it so you can ask for a merge request
  9. now you will be redirected to gitlab (or wthever) on the page where you have to click on merge (assign it to you & then merge)
  10. everething ok, (develop is up to date)go to your project on terminal: git chekout feature/xxx
  11. git pull so your local branch feature/xxx will have all the addition tha was made

WAY TOW: merge develop branch into feature/xxx branch

  1. switch to develop branch: git checkout develop
  2. git pull
  3. switch to feature/xxx: git checkout develop
  4. git merge develop
  5. now everything on develop has come along side with your commits on feature/xxx

Undo a pushed merge with git revert

How to undo a merge that has been pushed to origin? Use Revert. After merging my develop branch into the master branch i realized i didn’t want to release the code i just merged. But it was already pushed to origin. What do do?

first checkout the master branch:

git checkout master

then run a git log and get the id of the merge commit.

git log

then revert to that commit:

git revert -m 1 <merge-commit>

With ‘-m 1’ we tell git to revert to the first parent of the mergecommit on the master branch. -m 2 would specify to revert to the first parent on the develop branch where the merge came from initially.

Now commit the revert and push changes to the remote repo and you are done.

Getting back the reverted changes This changes the data to look like before the merge, but not the history. So it’s not exactly like an undo. If we would merge develop into master again the changes we reverted in master wont be applied. So if we would like these changes back again we could revert our first revert(!).

git revert <commit-of-first-revert>

Git - how to revert multiple recent commits

Let's assume we have a history like this:
G1 - G2 - G3 - B1 - B2 - B3
Where G1-G3 are 'good' commits and B1-B3 are 'bad' commits and we want to revert them.

G1 - G2 - G3 - B1 - B2 - B3 \ \ \ -- HEAD \ \ ------ HEAD1 \ ---------- HEAD2 -------------- HEAD~3

Here is how we can use git revert:

$ git revert --no-commit HEAD~2^..HEAD

Or:

$ git revert --no-commit HEAD~3..HEAD

We need to revert a range of revisions from B1 to B3. The range specified with two dots like .. includes only commits reachable from , but not reachable from (see man -7 gitrevisions).

Since we need to include B1 (represented by HEAD2) we use HEAD2^ (its parent) or HEAD3 (also parent of HEAD2). The HEAD~2^ syntax is more convenient if commit SHAs are used to name commits.

The --no-commit option tells git to do the revert, but do not commit it automatically.

So now we can review the repository state and commit it. After that we will get the history like this:

G1 - G2 - G3 - B1 - B2 - B3 - R`

Where R' is a revert commit which will return repository state to the commit G3. Run git diff to check this (output should be empty):

$ git diff HEAD~4 HEAD

Another way to run revert is to specify commits one by one from newest to oldest:

$ git revert --no-commit HEAD HEAD~1 HEAD~2

In this case there is no need to specify HEAD~3 since it is a good commit we do not want to revert. This is very useful if we want to revert some specific commits, for example, revert B3 and B1, but keep B2:

$ git revert --no-commit HEAD HEAD~2

Revert a merge commit that's already pushed to remote branch

git revert -m 1 <commit-hash> 
git push origin master

cherry pick

Cherry picking in Git means to choose a commit from one branch and apply it onto another.
This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch. Make sure you are on the branch you want to apply the commit to.

git checkout master

Execute the following:

git cherry-pick <commit-hash>

to see remote url

git remote show origin

or

git config --get remote.origin.url

or simply

git remote -v

add multiple remotes

by convention efter git initwe associate the repo to a remote url

# Syntax to add a git remote
git remote add REMOTE-ID REMOTE-URL

By convention, the original/primary remote repo is has origin as REMOTE-ID and to add another remote url we use as REMOTE-ID upstream

# Add remote 1: GitHub.
git remote add origin git@github.com:jigarius/toggl2redmine.git
# Add remote 2: BitBucket.
git remote add upstream git@bitbucket.org:jigarius/toggl2redmine.git

On push.

# push to origin remote (fst remote)
git push origin feature/feature1

#push to upstream remote (nd remote)
git push upstream feature/feature1

Remote: fetching a branch from the a specific remote

To fetch a specific branch from a specific remote, run the following command:

git fetch <remote> <branch>

Running the command will fetch <remote>’s branch and save them locally in <remote>/<branch>. What’s going on here is that remote branches you fetch are available using <remote-name>/<branch-name>. To merge remote’s changes into your branch, you have to git checkout your branch and then merge the branch you just fetched:

git merge <remote>/<branch>

<remote> branch is now merged into your branch

Creating co-authored commits

Just like for a regular commit message and after adding the meaningful description we need to add two empty new lines instead of adding a closing quotation ".

git commit -m "my co-authred commit,
>
>
> Co-authored-by: taha <taha@email.com>
> Co-authored-by: ahmed <ahmed@email.com>"

On the next line of the commit message, we type Co-authored-by: author-name <name@xxx.com> with specific information for each co-author. After the co-author information, add a closing quotation mark ".

Amend: changing the commit after committing

To change, or rather update, our latest commit before we push it, we use --amend.
Using —amend doesn’t change the commit. Instead, it replaces the commit with a brand new one. As such it is incredibly important you don’t amend public commits. Your local commits are perfectly fine.
There are two parts to a commit that we may want to change :

  • the first one is the commit message
  • the second one is a file. Maybe we realize that we needed to add one small change, like removing a little console.log(), or maybe we forgot to add a new file.

Changing the commit message

git commit --amend

This will open our configured code editor for Git and allow us to change the commit message. we can, however, change the commit message without opening the editor:

git commit --amend -m "Your updated commit message"

Adding the -m flag, followed by the updated message in quotes, allows you to change the message without opening the editor.

Amending a change or file

Let’s say we notice a rogue console.log() after we’ve committed and we want to remove it. Edit the file and stage it like you normally would for a commit. Then run the following command:

git commit --amend --no-edit

This will amend the file to the commit. The —no-edit flag allows us to do this without changing the commit message.

git clean

To cleanup untracked files.It deletes files permanently
Always add -n or --dry-run options to preview the damage you'll do!

git clean -fd --dry-run

If you just clean untracked files, run git clean -f
If you want to also remove directories, run git clean -f -d If you just want to remove ignored files, run git clean -f -X
If you want to remove ignored as well as non-ignored files, run git clean -f -x

squash

git rebase -i HEAD~3 #where 3 number of commits to stash

we will choose s instead of pick and modify the commit msg

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

feat: add hat wobble
^--^  ^------------^
|     |
|     +-> Summary in present tense.
|
+-------> Type: chore, docs, feat, fix, refactor, style, or test.

More Examples:

  • feat: (new feature for the user, not a new feature for build script)
  • fix: (bug fix for the user, not a fix to a build script)
  • docs: (changes to the documentation)
  • style: (formatting, missing semi colons, etc; no production code change)
  • refactor: (refactoring production code, eg. renaming a variable)
  • test: (adding missing tests, refactoring tests; no production code change)
  • chore: (updating grunt tasks etc; no production code change)

git stash

To stash also untracked files

git stash -u

To create a stash with a specific name

git stash push -m "stach msg"           

To create a branch from a stash

git stash branch branch/name 0 #where 0 stands for the stash ref stash@{0}        

To list the stashed modifications

git stash list

To show files changed in the last stash

git stash show

So, to view the content of the most recent stash, run

git stash show -p

To view the content of an arbitrary stash, run something like

git stash show 1 #or git stash show stash@{1}

or with -p for showing the changes in the full tree view

git stash show -p 1 #or git stash show -p stash@{1}

To pop a specific stash

git stash list
git stash apply n #or git stash apply stash@{n}

To clear all stash list

git stash clear 

Clear a file or folder from git local cache

git rm --cached ./path/file_name

if you are in big mess

git rm -r --cached .
git add .
git commit -am 'git cache cleared'
git push

git & case senstivity

git config core.ignorecase false

to rename a file where ignorecase is false

git mv -f yOuRfIlEnAmE yourfilename

You have not concluded your merge (MERGE_HEAD exists)

git merge --abort [Since git version 1.7.4]

git reset --merge [prior git versions]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment