Demo entry 5861575

Basic Git commands

   

Submitted by anonymous on Jul 25, 2016 at 07:48
Language: Bash. Code size: 11.6 kB.

#-------------------------------
# Setting up a repository
#-------------------------------
  
## git init
git init                            # Transform the current directory into a Git repository.
                                    # This adds a .gitfolder to the current directory and makes it possible to start recording revisions of the project.
git init <directory>                # Create an empty Git repository in the specified directory.
                                    # Running this command will create a new folder called <directory containing nothing but the .git subdirectory.
git init --bare <directory>         # Initialize an empty Git repository, but omit the working directory.
  
## git clone
git clone <repo>                        # Clone the repository located at <repo> onto the local machine.
                                        # The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.
git clone <repo> <directory>            # Clone the repository located at <repo> into the folder called <directory> on the local machine.
  
## git config                                         # Typically, you’ll want to use the --global flag to set configuration options for the current user.
git config --global user.name <name>                  # Define the author name to be used for all commits by the current user.
git config --global user.email <email>                # Define the author email to be used for all commits by the current user.
git config --global alias.<alias-name> <git-command>  # Create a shortcut for a Git command.
git config --system core.editor <editor>              # Define the text editor used by commands like git commit for all users on the current machine.
                                                      # The <editor> argument should be the command that launches the desired editor (e.g., vi).
git config --global --edit                            # Open the global configuration file in a text editor for manual editing.
 
 
#-------------------------------
# Saving changes
#-------------------------------
  
## git add
git add <file>                  # Stage all changes in <file> for the next commit.
git add <directory>             # Stage all changes in <directory> for the next commit.
git add -p                      # Begin an interactive staging session that lets you choose portions of a file to add to the next commit.
  
## git commit
git commit                      # Commit the staged snapshot. This will launch a text editor prompting you for a commit message.
git commit -m "<message>"       # Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.
git commit -a                   # Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files.
  
#-------------------------------
  
# Inspecting a repository
#-------------------------------
  
## git status
git status                          # List which files are staged, unstaged, and untracked.
  
## git log
git log                                 # Display the entire commit history using the default formatting.
                                        # If the output takes up more than one screen, you can use Space to scroll and q to exit.
git log -n <limit>                      # Limit the number of commits by <limit>. For example, git log -n 3will display only 3 commits.
git log --oneline                       # Condense each commit to a single line. This is useful for getting a high-level overview of the project history.
git log --stat                          # Along with the ordinary git log information,
                                        # include which files were altered and the relative number of lines that were added or deleted from each of them.
git log -p                              # Display the patch representing each commit. This shows the full diff of each commit,
                                        # which is the most detailed view you can have of your project history.
git log --author="<pattern>"            # Search for commits by a particular author. The <pattern> argument can be a plain string or a regular expression.
git log --grep="<pattern>"              # Search for commits with a commit message that matches <pattern>, which can be a plain string or a regular expression.
git log <since>..<until>                # Show only commits that occur between <since> and <until>.
                                        # Both arguments can be either a commit ID, a branch name, HEAD, or any other kind of revision reference.
git log <file>                          # Only display commits that include the specified file. This is an easy way to see the history of a particular file.
git log --graph --decorate --oneline    # A few useful options to consider.
                                        # —graph flag that will draw a text based graph of the commits on the left hand side of the commit messages.
                                        # —decorate adds the names of branches or tags of the commits that are shown.
                                        # —oneline shows the commit information on a single line making it easier to browse through commits at-a-glance.
  
#-------------------------------
# Viewing old commits
#-------------------------------
  
## git checkout                 # The git checkout command serves three distinct functions: checking out files, checking out commits, and checking out branches.
                                # In this module, we’re only concerned with the first two configurations.
  
git checkout master             # Return to the master branch. Branches are covered in depth in the next module,
                                # but for now, you can just think of this as a way to get back to the “current” state of the project.
git checkout <commit> <file>    # Check out a previous version of a file.
                                # This turns the <file> that resides in the working directory into an exact copy of the one from <commit> and adds it to the staging area.
git checkout <commit>           # Update all files in the working directory to match the specified commit.
                                # You can use either a commit hash or a tag as the <commit>argument. This will put you in a detached HEAD state.
  
#-------------------------------
# Undoing Changes
#-------------------------------
  
## git checkout
  
## git revert
  
## git reset
  
## git clean
  
#-------------------------------
# Rewriting history
#-------------------------------
  
## git commit --amend
  
## git rebase
  
## git rebase -i
  
## git reflog
  
#-------------------------------
# Syncing
#-------------------------------
  
## git remote                               # The git remote command lets you create, view, and delete connections to other repositories.
  
git remote                                  # List the remote connections you have to other repositories.
git remote -v                               # Same as the above command, but include the URL of each connection.
git remote add <name> <url>                 # Create a new connection to a remote repository.
                                            # After adding a remote, you’ll be able to use <name> as a convenient shortcut for<url> in other Git commands.
git remote rm <name>                        # Remove the connection to the remote repository called <name>.
git remote rename <old-name> <new-name>     # Rename a remote connection from <old-name> to <new-name>.
  
## git fetch                    # The git fetch command imports commits from a remote repository into your local repo. 
  
git fetch <remote>              # Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.
git fetch <remote> <branch>     # Same as the above command, but only fetch the specified branch.
  
## git pull
  
git pull <remote>               # Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy.
                                # This is the same as git fetch <remote> followed by git merge origin/<current-branch>.
git pull --rebase <remote>      # Same as the above command, but instead of using git merge to integrate the remote branch with the local one, use git rebase.
  
## git push
git push <remote> <branch>      # Push the specified branch to <remote>, along with all of the necessary commits and internal objects.
                                # This creates a local branch in the destination repository. To prevent you from overwriting commits,
                                # Git won’t let you push when it results in a non-fast-forward merge in the destination repository.
git push <remote> --force       # Same as the above command, but force the 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> --all         # Push all of your local branches to the specified remote.
git push <remote> --tags        # Tags are not automatically pushed when you push a branch or use the --all option.
                                # The --tags flag sends all of your local tags to the remote repository.
  
#-------------------------------
# Using Branches
#-------------------------------
  
## git branch
git branch                    # List all of the branches in your repository.
git branch <branch>           # Create a new branch called <branch>. This does not check out the new branch.
git branch -d <branch>        # Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.
git branch -D <branch>        # Force delete the specified branch, even if it has unmerged changes.
                              # This is the command to use if you want to permanently throw away all of the commits associated with a particular line of development.
git branch -m <branch>        # Rename the current branch to <branch>.
  
## git checkout
git checkout <existing-branch>                  # Check out the specified branch, which should have already been created with git branch.
                                                # This makes <existing-branch> the current branch, and updates the working directory to match.
git checkout -b <new-branch>                    # Create and check out <new-branch>. The -b option is a convenience flag
                                                # that tells Git to run git branch <new-branch> before running git checkout <new-branch>.
git checkout -b <new-branch> <existing-branch>  # Same as the above invocation, but base the new branch off of<existing-branch> instead of the current branch.
  
## git merge
git merge <branch>                # Merge the specified branch into the current branch. Git will determine the merge algorithm automatically (discussed below).
git merge --no-ff <branch>        # Merge the specified branch into the current branch, but alwaysgenerate a merge commit (even if it was a fast-forward merge).
                                  # This is useful for documenting all merges that occur in your repository.

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).