Clone existing repo

git clone https://<path>

Align remote changes

git fetch

Align remote changes inside local repo

git pull

Merge a remote repo inside your local

git checkout <local>
git pull origin <remote>

Create new branch

# Create a new branch starting from the current code
git checkout -b <new_branch_name>
# Create a new branch given a SHA
git checkout -b <branch> <sha>

Check status

git status

Create new branch

git checkout -b <new_branch_name>

Push new branch

git push -u origin <new_branch_name>

Commit changes

git commit -m "change name"

List git branches that contain a given commit

If you need to know which are the branches that contains a certain commit, git is your friend

# local branches
git branch --contains <commit_hash>
# remote branches
git branch -a --contains <commit_hash>

Save git credential locally

# your credential will be stored ~/.git-credential after the first push
git config credential.helper store
git push
# unset saved credential
git config --global --unset credential.helper
# open the git-credentials file located in the home of the current user and delete the proper entry
user@localhost:~$ vim /home/user/.git-credentials

Check the attached remote repo

git remote -v
# origin  https://<path>/<repo>.git (fetch)
# origin  https://<path>/<repo>.git (push)
# #####
# change repo remote url
git remote set-url origin https://<address>.git

Remove git ssl check

# not recommended
git config --global http.sslVerify false

Delete branch local repo

git branch -d <branch_name>
# force delete
git branch -D <branch_name>

Delete all local branches except branch name

git branch | grep -v <branch_name>| xargs git branch -d

Delete all local branches already merged in master

git branch --merged master --no-color | grep -v master | grep -v stable | xargs git branch -d

Delete remote branch

git branch -D <branch_name>
# remote_origin is origin if is in the root
# remote_name most of the times is equal to branch_name
git push <remote_origin> --delete <remote_name>

Rename branch

git checkout <old_branch>
git branch -m <new_branch>
# push the new branch remotely
git push -u origin <new_branch>
# delete the old branch remotely
git push --delete origin <old_branch>

Revert last commit

# reset last commit
git reset HEAD~1
# reset and discard files
git reset --hard HEAD~1
# if it is already pushed on origin
git push -f <origin> <remote_branch_name>

Git show

Useful command when git does not resolve automatically conflicts between files or git does not understand properly the conflict (for example someone has formatted badly the entire file)

# create the merged file
user@localhost: git show :1:./Filename.ext > ./Filename.MERGED.java  
# create the local version of the file (mine)
user@localhost: git show :2:./Filename.ext > ./Filename.LOCAL.java   
# create the remote version of the file (theirs)
user@localhost: git show :3:./Filename.ext > ./Filename.REMOTE.java

Git show useful script

#!/bin/bash
if [ -z "$1" ]; then
    echo "Param is empty"
    exit
fi
if [ -z "$2" ]; then
    `git show :2:./$1 > ./$1.LOCAL`
    `git show :3:./$1 > ./$1.REMOTE`
else
    `git show :2:./$1 > ./$1.LOCAL.$2`
    `git show :3:./$1 > ./$1.REMOTE.$2`
fi

# add the alias git-show if you like in the .bashrc file
# alias git-show='sh ~/<path>/git-show.sh'

To see differences without creating a file it’s possibile to use “git mergetool” command and select your favourite editor (ex: meld)

Cleanup the local references to remote branch

git remote prune origin

Show only modified lines

git diff --unified=0

Push local branch from repo A to repo B

# repoA == origin
# repoB == origin2
git remote add origin2 https://user@github.com/example/new_repo.git
# check results
git remote -v
git push origin2 local_branch:new_repo_branch

Git reflog

git reflog shows a log of all the recent movements of HEAD in your repository.

Every commit, checkout, reset, merge, rebase, etc. It’s your safety net that tracks where HEAD has been, even for commits that aren’t currently reachable through any branch, making it possible to recover “lost” work from operations like hard resets or deleted branches.

git reflog

Interested in Git cherry pic ?

Interested in Git merge branch excluding a file ?

Categories: Bash

0 Comments

Leave a Reply

Avatar placeholder