Home

Git Complete

Resources

  1. Git aliases

Git Config

General setup for Git can be found at ~/.gitconfig.

Basic Commands

CommandWhat it does
git initInitialise a git project
git cloneClone a git project
git addAdd a file to git
git commitCommit the current file list
git pullPull from a Git repository
git pushPush to a remote Git repository
git statusSee current status for Git repo
git commit --amend [commit]Amend a commit

Tracked Files

Files that have been added using git add can be tracked for staging and committing.

Add Files Recursively

git add . will add all files from this directory recursively.

Back Out Changes

If you decide that you do not want someGit changes, you can run git reset HEAD <file>. This will unstage a particular file.

If you do not want any of the changes to the file at all, you can run git checkout -- <file>. This will remove all the changes. If you run git checkout ., it will remove all Git changes.

Renaming And Moving Files

# renaming the file git mv <old-name> <new-name> # git will rename the file and ls will confirm

If you mv the file, you will find that Git considers this as the old file being deleted while a new one being untracked. Adding the file using git add -A will have Git realise that we are just renaming the file.

If we want to rename the file back, we can just use git mv <old-file> <new-file>.

Git History

git help log will show help for the commit history.

git log will show you the basic Git history from the most recent to the oldest.

git log --oneline --graph --decorate will show one line for each commit, plus a graph history and will decorate the log.

git log --since="3 days ago" will show the commits from the last three days.

git log -- <file-name> will show the log for a specific file name.

Adding git log --follow -- <file-name> will follow a file back through changes in the commit name.

git show <commit-SHA> will show the commit ID, author, diff + more.

Git Alias

Can has the ability to add aliases for other commands.

We could add an alias by running git config --global alias.hist = "log --all --graph --decorate --oneline".

Now we could run git hist as an alias for that lengthy command.

Git Comparisons

  • git diff will show you the diff between the last commit and current files.
  • git diff HEAD will compare working directory and last commit.
  • git diff --staged HEAD will compare staging with last commit.
  • To limit comparisons to one file, you can git diff -- <file-name>.
  • To compare to a particular command, you can run git diff <commit-id-one> <commit-id-two> to compare from HEAD to the specified commit.
  • git diff HEAD HEAD^ will compare HEAD and HEAD - 1.
  • git diff master origin/master where origin/master is the master branch on remote.

Branching and Merging

CommandDefinition
git branch -aList all branches
git branch my-new-branchCreates new local branch
git checkout my-new-branchCheckout local branch
git branch -m my-new-branch branchRename my-new-branch to branch
git branch -d branchDelete branch

Note that the first commit for each new branch has several labels associated with it. Those labels are just pointers and there won't be any new commits on the new branch until we make one.

Fast Forward Merges

CommandDefinition
git checkout -b title-changeChange into new branch title-change
git merge title-changeMerge title-change into current branch

Fast-forward commits basically move the commits into the current branch as if there was no branch changes made at all.

To disable fast-forward merges:

CommandDefinition
git merge branch --no-ffThis will result with a single merge commit with the branch line being preserved

Automatic Merges

git merge simple-changes -m "Merging changes from simple changes branch".

Handling Conflicts

When you run merge and run into a conflict, you will be in an "in between" merging state.

To resolve a conflict, you will need to open the conflicted files and see the updates Git has made to show the HEAD vs the branch conflicts separated by ======.

Once the conflicts are resolved, you need to now commit the file. Git will generally create a .orig file during resolutions that you can ignore or remove post-fix.

Rebase

CommandDefinition
git rebase [source-branch]Rebase source branch into current branch

Stashing

CommandDefinition
git stashStash current changes
git stash applyApply stash to working directory
git stash listList all stashes
git stash dropDrop the last stash
git stash -uInclude untracked files in stash (not Git ignored)
git stash popEssentially runs git stash apply && git stash drop
git stash show stash@{1}Show reflog of what is included in the stash
git stash apply stash@{ref}Apply specific stash
git stash drop stash@{ref}Drop specific stash reference
git stash branch newchangesCreates newchanges branch, switches to branch, applies stash and then drops stash
git stash push -m "Message"Create stash associated with a message

By default, git stash will only stash tracked files.

Tagging

Tags are nothing more than labels. git tag myTag will create a tag - it's known as a lightweight tag.

CommandDefinition
git tags myTagCreate tag
git tag --listShow tag
git tag --delete myTagDelete tag "myTag"
git tag -a v-1.0-a Createds an annotated tag - this will allow you to write release notes etc
git show v-1.0This will show tag, tagger, date and tag message with commit ID etc
git diff v-tag1 v-tag2Diff between two different tags
git tag -a [tag-name][commit]Annotate a specific commit
git tag -a [tag-name] -f [commit]Edit a tag annotation

Reset and Reflog

git reflog shows a log of everything that we have done. It's the reference log of the previous 60 days.

We can then run git reset <ref> to actually go back and forward through history. We can also reset back to the top if required post-reset.

git reset --hard

Hard | Removes all changes from staging and working directory Soft | Points header back to new location

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/git/git-complete

Sections


Related