Skip to content

Instantly share code, notes, and snippets.

@giorgichi
Last active January 15, 2022 04:55
Show Gist options
  • Save giorgichi/ff5af2e8749a884ec4666a78ddf91c85 to your computer and use it in GitHub Desktop.
Save giorgichi/ff5af2e8749a884ec4666a78ddf91c85 to your computer and use it in GitHub Desktop.
Git Commands

GIT Command Line

Commits

  • git commit -am "Add comment here"
    add and commit all changed files in one code by using -am flag

  • git config --global alias.ac "commit -am"
    create new custom commands called ac that adds all changed files and commits (commit -am)

  • git commit --amend -m "New message"
    update comments of the last commit using --amend -m flag

  • git add .
    git commit --amend --no-edit
    update the last commit with new files using --amend flag, while keeping the old comment using --no-edit flag
    NOTE: this only works if commmit has not been pushed to remote repo

  • git revert {commit hash}
    go back to the previous state without removing the last commit

  • git stash
    git stash pop
    remove changes without committing to repo (stash) and add them (changes) back when ready to continue working (stash pop)

Branches

  • git checkout {branch name}
    switch to branch
    NOTE: to go to previous branch you have checked out from use - flag instead of branch name

  • git branch -M new_name
    rename branch with new_name using -M flag

  • git fetch origin
    git reset --hard origin/master
    git clean -df
    when local repo is messed up and want to go back to original state from remote repo:

    • get latest code from remote repo with fetch,
    • overwrite local code with the fetched remote code using --hard flag with reset
    • remove untracked files or build artifacts with clean
  • git rebase master --interactive
    pull up a file with a list of commits on this branch

  • git branch -d {branch}
    delete a branch using -d flag

Log

  • git log --graph --oneline --decorate
    desplay log in more concise fashion

  • git log --pretty=format:"%h - %an, %ar : %s"
    specify your own log output using format specifiers (e.g. %h) Specifier Description of Output %H - Commit hash
    %h - Abbreviated commit hash
    %T - Tree hash
    %t - Abbreviated tree hash
    %P - Parent hashes
    %p - Abbreviated parent hashes
    %an - Author name
    %ae - Author email
    %ad - Author date (format respects the --date=option)
    %ar - Author date, relative
    %cn - Committer name
    %ce - Committer email
    %cd - Committer date
    %cr - Committer date, relative
    %s - Subject

Sources:

Delete a remote branch

$ git push origin --delete <branch> # Git version 1.7.0 or newer
$ git push origin :<branch> # Git versions older than 1.7.0

Delete a local branch

  • $ git branch --delete <branch>
  • $ git branch -d <branch> # Shorter version
  • $ git branch -D <branch> # Force delete un-merged branches

Delete a local remote-tracking branch

$ git branch --delete --remotes <remote>/<branch>

$ git branch -dr <remote>/<branch> # Shorter

$ git fetch <remote> --prune # Delete multiple obsolete tracking branches

$ git fetch <remote> -p # Shorter

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