Skip to content

Git

Some notes about git commands

Global local settings

To do once on local
    git config --global user.name "yourname"
    git config --global user.email "youremail"

    # To change or list global configs
    ### List
    git config --global -l
    ## Edit
    git config --global  -e

    # example to open git commit in another editor like gedit, atom instead of vi/vim
    git config core.editor=gedit

Disable SSL Check

To disable git ssl check, verification
 export GIT_SSL_NO_VERIFY=true # temporary
 git config --global http.sslVerify false #Permanent

The Basics

Manage ...
    # Init git in existing folder
    git init
    git remote add origin ${repo_url}
    ## repo_url with token:  https://user:token@gitlab.com/app/toto.git
    ## repo_url with ssh: ssh://git@gitlab.com/app/toto.git

    # Add file
    git status ### will be always useful to check your current status
    git add -A # to add all 
    git add toto.txt # to add only toto.txt

    git status ### will be always useful to check your current status
    # Commit changes
    git commit       # to commit files
    git commit -m "commit message" # to avoid a prompt by adding message directly

    # Push changes
    git push origin ${branch}

    #to create a new branch from master
    git checkout master
    git pull origin master
    git checkout -b ${new branch}

Clone a branch

To clone specific branch
    git clone -b ${branch} ${repo_url}
    git clone ${repo_url} # clone the default branch 'master' more ofter

    # git clone in custom folder name, default is the repo name
    git clone ${repo_url} ${folder_name}

Remove a branch

To remove a branch
    # Local
    git branch -D ${branch}

    # Remote
    git push origin --delete ${branch}
    # OR
    git push origin :mabranche

Change remote url

Remote Url ...
    # Update the remote url (eg: switch from url-based to ssh-based)

    git remote set-url origin ${repo_new_url}

    ## with git config

    git config remote.origin.url ${repo_new_url}

Upstream Remote

Upstream ...
    # Set upstream repo
    git remote add upstream <upstream_remote_url>

    # Define/Change the remote origin if necessary
    git remote set-url origin <myorigin_repo_url>

    # List remote urls: upstream & origin
    git remote -v

    # Fetch upstream & merge it on the local
    git fetch upstream
    git checkout origin <branch: main>
    git merge upstream/<branch: main>

Rebase

Rebase to get the latest changes merged on master after you branch creation

rebase
    git checkout master
    git pull origin master
    git checkout ${branch}
    git rebase master
    git status # to check if there is some conflicts
    ### fix all eventual conflicts and then git add -A
    ### DON'T RUN git commit.
    git push -f origin

    # To rebase your branch in case of existing conflicts which prevent you to merge your pull request/branch
    # (to clean up  also a weird merge commit messages to have a single clear commit)

    git checkout master
    git pull origin master
    git reset --hard HEAD^ #to remove the merge commit
    git rebase master
    git status # to see a status, files with conflicts
    # Fix conflicts and then git add -A
    git rebase --continue
    git push -f origin

To erase/rewrite a previous commit/message, just use git commit --amend

But if you want to rewrite a very far commit or merge/drop the last xxx commits,
Use git rebase -i HEAD~xxx and follow git instruction

Amend Flag

Useful to erase previous commit but does not remove changes.
The previous changes and current will be in the same commit number with same commit message

git commit --amend or git commit --amend -m "New commit"

It can be also used for others stuffs, like change the author of a commit
git commit --amend --author="author <author_email>"

After using --amend you will need -f in push to force it.

Push/Pull Without Password

Git push and git pull without password, credential

Use ssh is the best option
You have to add your public key in your account and then use
ssh://git@${repo_url} instead of https://${repo_url}

Add your username and password/token in your remote url
https://$USER:$TOKEN@{repo_url}

Play with commit or log details

git log/show
    # Count a commit number
    git rev-list --count HEAD
    git rev-list --count ${branch}

    git diff --name-only ${previous_index} ${current_index}  #" list changes between previous index and current one"
    git log --stat  #" all last commit"

    git show current_index # to see commit files

    # Get a  ${commit_number} time/timestamp in specific format
    git show -s --format=%ci ${commit_number}
    git show -s --format=%ct ${commit_number} #timestamp
    git show -s --format=%cd --date=short ${commit_number}

    git log -1 #See the lastest log info: commit/merge
    git log -1 --format=%ci ${commit_number}
    git log -1 --date=iso
    git log -1 --pretty=format:%ct
    # Get the time of all commit with message "Update .gitlab-ci.yml"
    git log --grep="Update .gitlab-ci.yml" --format=%aI

    ### format iso= --format=%aI

Gitman

Gitman is a Gitdependency manager.
It provides advanced options for managing versions of nested Git repositories..