Git Tips & TrickssteemCreated with Sketch.

in #programming7 years ago
Do you have a tip you'd like added ? Post in the comments below or send me an email.

What is Git?

By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments).

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's working copy of the code is also a repository that can contain the full history of all changes.

In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

On to the Tips...

Basics
  • Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
    git init [directory]
  • Clone repo located at [repo] onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.
    git clone [repo]
  • Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user.
    git config user.name [name]
  • Stage all changes in [directory] for the next commit. Replace [directory] with a [file] to change a specific file.
    git add [directory]
  • Commit the staged snapshot, but instead of launching a text editor, use [message] as the commit message.
    git commit -m "[message]"
  • List which files are staged, unstaged, and untracked.
    git status
  • Display the entire commit history using the default format. For customization see additional options.
    git log
  • Show unstaged changes between your index and working directory.
    git diff
Undoing Changes
  • Create new commit that undoes all of the changes made in [commit], then apply it to the current branch.
    git revert [commit]
  • Remove [file] from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
    git reset [file]
  • Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
    git clean -n
Rewriting History
  • Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
    git commit --amend
  • Rebase the current branch onto [base]. [base] can be a commit ID, a branch name, a tag, or a relative reference to HEAD.
    git rebase [base]
  • Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.
    git reflog
Branches
  • List all of the branches in your repo. Add a [branch] argument to create a new branch with the name [branch].
    git branch
  • Create and check out a new branch named [branch]. Drop the -b flag to checkout an existing branch.
git checkout -b
[branch]
  • Merge [branch] into the current branch.
git merge [branch]
Remote Repositories
  • Create a new connection to a remote repo. After adding a remote, you can use [name] as a shortcut for [url] in other commands.
git remote add [name] [url]
  • Fetches a specific [branch], from the repo. Leave off [branch] to fetch all remote refs.
git fetch [remote] [branch]
  • Fetch the specified remote’s copy of current branch and immediately merge it into the local copy
git pull [remote]
  • Push the branch to [remote], along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git push [remote] [branch]
Config
  • Define the author name to be used for all commits by the current user
git config --global user.name [name]
  • Define the author email to be used for all commits by the current user
git config --global user.email [email]
  • Create shortcut for a Git command. E.g. alias.glog log --graph --oneline will set git glog equivalent to git log --graph --oneline.
git config --global alias. [alias-name] [git-command]
  • Set text editor used by commands for all users on the machine. [editor] arg should be the command that launches the desired editor (e.g., vi).
git config --system core.editor [editor]
  • Open the global configuration file in a text editor for manual editing.
git config --global --edit
Log
  • Limit number of commits by [limit]. E.g. git log -5 will limit to 5 commits
git log -[limit]
  • Condense each commit to a single line.
git log --oneline
  • Display the full diff of each commit.
git log -p
  • Include which files were altered and the relative number of lines that were added or deleted from each of them.
git log --stat
  • Search for commits by a particular author.
git log --author= ”[pattern]”
  • Search for commits with a commit message that matches [pattern].
git log --grep=”[pattern]”
  • Show commits that occur between [since] and [until]. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
git log [since]..[until]
  • Only display commits that have the specified file.
git log -- [file]
  • --graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.
git log --graph --decorate
Diff
  • Show difference between working directory and last commit.
git diff HEAD
  • Show difference between staged changes and last commit
git diff --cached
Reset
  • Reset staging area to match most recent commit, but leave the working directory unchanged.
git reset
  • Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset --hard
  • Move the current branch tip backward to [commit], reset the staging area to match, but leave the working directory alone.
git reset [commit]
  • Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after [commit].
git reset --hard [commit]
Rebase
  • Interactively rebase current branch onto [base]. Launches editor to enter commands for how each commit will be transferred to the new base.
git rebase -i [base]
Pull
  • Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.
git pull --rebase [remote]
Push
  • Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.
git push [remote] --force
  • Push all of your local branches to the specified remote
git push [remote] --all
  • Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo
git push [remote] --tags
Tags
  • List the local repository's tags.
git tag
  • List the local repository's tags that match a pattern. Use a * as a wildcard.
git tag -l [pattern]
  • Creates a lightweight tag named [example] as a reference in .git/refs/tags/[example], which points to the current commit.
git tag [example]
  • Creates an (unsigned) annotated tag named [example] as a reference in .git/refs/tags/[example] with the attached message [message], which points to the current commit.
git tag -a [example] -m [message]
  • Delete all tags in a repo on origin.
git tag -l | xargs -n 1 git push --delete origin
  • Delete all tags in a repo locally.
git tag | xargs git tag -d
Thanks Atlassian for many of these, from their awesome Cheat Sheet
Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.atlassian.com/dam/jcr:8132028b-024f-4b6b-953e-e68fcce0c5fa/atlassian-git-cheatsheet.pdf

Your link to send you an email is all kinds of borked.

Weird... it was fine when it was written, I guess steemit tried to be helpful and failed...

[email protected]?Subject=Git%20Tip%20Suggestion

Just tried again, yup, steemit messes with the URL when put into a link. Dumb. There is the proper email. Thanks for pointing that out.

My favorite Git alias is this in my ~/.gitconfig file:

[alias]
    log4 = log -n4 --stat

Also, I blogged a little about GitHub triggering automation at https://steemit.com/cicd/@markhu/gitops-as-faster-cicd

Weird... steemit messes with mailto links and breaks them completely... it was fine when it was written. Sorry about that.

Here is the proper email; [email protected]?Subject=Git%20Tip%20Suggestion