#Git Bash: Configure Git (git config)
###Syntax:
git config --global user.name <“User Name”>
git config --global user.email <UserEmailAddress>
###Set the global User.Name value for the current user on the system:
$ git config --global user.name “FirstName LastName”
###Set the global User.Email value for the current user on the system:
$ git config --global user.email UserEmailAddress
###Show me the current values: The following return the current values for the user.name and user.email properties respectively and output the values to the console window:
####Print the current global User.Name value to the console window:
$ git config --global user.name
####Print the current global User.Email value to the console window:
$ git config --global user.email
###Git Bash: Initialize a New Git Repo (git init)
Syntax:
git init
###Create files required in the current directory to perform version control:
$ git init
##Git Bash: Add/Stage for Commit (git add)
###Syntax:
git add [options] [<File_1>] [<File_2>] . . . [<File_n>]
Options:
-A (or --all) = Add all new or changed files to the staged changes, including removed items (deletions)
-u = Add changes to currently tracked files and removals to the next commit. Does not add new files.
. = Adds new or changed files to the staged changes for the next commit, but does not add removals.
Note that git add -A is semantically equivalent to git add . followed by git add –u
-p = Interactive add. Walks through changes in the working directory and prompts for add
####Add all changes in the working directory to the next commit, including new files and deletions:
$ git add -A
####Add all changes to tracked files and all new files to the next commit, but do not add file deletions:
$ git add .
####adds all changes to tracked files and all file removals to the next commit, but does not add new files:
$ git add -u
####Walks through changed files and prompts user for add option. Does not include new files:
$ git add -p
##Git Bash: Unstage from Commit (git reset)
Syntax:
git reset HEAD <File_1>
####Remove the specified file from the next commit:
$ git reset HEAD FileName
##Git Bash: Committing Changes (git commit)
###Syntax:
git commit [options] [<File_1>] [<File_2>] . . . [<File_n>] [-m <“Commit Message”>]
Options:
-a = Commit all changes to tracked files since last commit
-v = Verbose: include the diffs of committed items in the commit message screen
--amend = Edit the commit message associated with the most recent commit
--amend <File_1> <File_2> . . . <File_n> = redo the previous commit and include changes to specified files
####Commits the changes for the specific file(s) and includes the commit message specified:
$ git commit FileName –m “Message Text”
Note that Git requires a commit message. If you do not provide one using the -m option, you will be prompted to do so before the commit is performed.
####Commits all files changed since last commit. Does not include new files.
$ git commit –a –m “Message Text”
####Adds all changes to the previous commit and overwrites the commit message with the new Message Text. Does not include new files:
$ git commit –a –amend –m “Message Text”
##Git Bash: Remote Repositories (git remote)
###Syntax:
git remote add <RemoteName> <RemoteURL>
git remote show <RemoteName>
NOTE: As used here, RemoteName represents a local alias (or nickname) for your remote repository. The name of the remote on the server does not necessarily have to be the same as your local alias.
####Add the specified remote repo to your git config file. The remote can then be pushed to/fetched from:
$ git remote add RemoteName https://RemoteName/Proj.git
####Print information about the specified remote to the console window:
$ git remote show RemoteName
##Git Bash: Branching (git branch)
###Syntax:
git branch [options][<BranchName>][<StartPoint>]
Options: -a = List all local and remote branches
-r = List all remote branches
####List all local branches:
$ git branch
####List all remote branches:
$ git branch -r
####List all local and remote branches:
$ git branch -a
####Create a new branch starting at the some point in history as the current branch:
$ git branch BranchName
Note that this creates the new branch, but does not “check it out” (make it the current working branch).
####Switch from the current branch to the indicated branch:
$ git checkout BranchName
###Create a new branch and switch to it from the current branch:
$ git checkout –b NewBranchName StartPoint
Note that StartPoint refers to a revision number (or the first 6 characters of such) or an appropriate tag.
##Git Bash: Merging Branches
###Syntax:
git merge [<BranchName>][--no-commit]
####Merge the specified branch into the current branch and auto-commit the results:
$ git merge BranchName
####Merge the specified branch into the current branch and do not commit the results:
$ git merge BranchName --no-commit
##Git Bash: Pushing to Remote Repositories (git push)
###Syntax:
git push [<RemoteName> <BranchName>]
####Update the remote server with commits for all existing branches common to both the local system and the server.
Branches on the local system which have never been pushed to the server are not shared.
$ git push
####Updates the remote server with commits for the specific branch named.
This command is required to push a new branch from the local repo to the server if the new branch does not exist on the server.
$ git push RemoteName BranchName
##Git Bash: Fetching from Remote Repositories (git fetch)
###Syntax:
git fetch <RemoteName>
####Retrieve any commits from the server that do not already exist locally:
$ git fetch RemoteName
NOTE: git fetch retrieves information from the remote and records it locally as a branch in your current repository. In order to merge the new changes into your local branch, you need to run git fetch followed by git merge. Since there may be more than one branch on the remote repository, it is necessary to specify the branch you wish to merge into your current branch:
####Merge syntax for post-fetch merge:
git merge <RemoteName/BranchName>
####Merge the newly fetched branch from the remote into your current working branch:
$ git merge RemoteName/BranchName
Using fetch before merging allows you to pull changesets in from the remote, but examine them and/or resolve conflicts before attempting to merge.
##Git Bash: Pulling from Remote Repositories (git pull)
###Syntax:
git pull <RemoteName/BranchName>
####Fetch changes from the specified branch in the remote, and merge them into the current local branch:
$ git pull RemoteName/BranchName
NOTE: git pull is essentially the same as running git fetch immediately followed by git merge.
##Git Bash: Undo (git reset)
###Syntax:
git reset [options]
Options: --hard = undo everything since the last commit
--hard ORIG_HEAD = Undo most recent merge and any changes after.
--soft HEAD^ = undo last commit, keep changes staged
####Undo everything since the last commit:
$ git reset --hard
####Undo most recent successful merge and all changes after:
$ git reset --hard ORIG_HEAD
####Undo most recent commit but retain changes in staging area:
$ git reset --soft HEAD^
##Git Bash: Tags (git tag)
###Syntax:
git tag [options] [<TagName>] [<CommitChecksum>] [<TagMessage?]
Options: -a = Annotated Tag
-m = Annotated Tag Message
####List all tags in the current repository:
$ git tag
####Create a tag at the current revision:
$ git tag TagName
####Create a tag at the commit specified by the partial checksum (six characters is usually plenty):
$ git tag TagName CommitChecksum
####Create an annotated tag:
$ git tag -a TagName -m TagMessage
####Create an annotated tag at the commit specified by the partial checksum:
$ git tag -a TagName CommitChecksum
####Push tags to a remote repository:
$ git push --tags
####Print information about a specific tag to the console window:
$ git show TagName
####To restore all those deleted files in a folder enter the following command.
git ls-files -d | xargs git checkout --