{"id":15309014,"url":"https://github.com/marcelod/git-commands","last_synced_at":"2025-10-08T16:31:08.715Z","repository":{"id":29869353,"uuid":"33414514","full_name":"marcelod/git-commands","owner":"marcelod","description":"List of useful git commands","archived":false,"fork":true,"pushed_at":"2015-04-04T08:09:45.000Z","size":104,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-02T08:19:35.422Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"bpassos-zz/git-commands","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcelod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-04T17:50:19.000Z","updated_at":"2015-04-04T17:15:31.000Z","dependencies_parsed_at":"2023-07-10T12:34:28.612Z","dependency_job_id":null,"html_url":"https://github.com/marcelod/git-commands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelod%2Fgit-commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelod%2Fgit-commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelod%2Fgit-commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcelod%2Fgit-commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcelod","download_url":"https://codeload.github.com/marcelod/git-commands/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235731831,"owners_count":19036815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T08:19:50.901Z","updated_at":"2025-10-08T16:31:03.408Z","avatar_url":"https://github.com/marcelod.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg\n  src=\"/img/git.png\"\n  width=\"70\"\n  align=\"right\"\n/\u003e\n#Useful Git Commands\n\n##About it\n\u003e Have you recently started using Git? This should give you the base commands you need to perform the most common actions in Git. If you find a command that is not here, or could be explained better, please dont hesitate in * [Contributing](#contributing). Cheers!\n\n## Table of contents\n\n* [Install git](#install-git)\n* [Setting up git](#setting-up-git)\n* [Applying colour to git ](#applying-colour-to-git)\n* [Initializing a repository in an existing directory](#initializing-a-repository-in-an-existing-directory)\n* [Checking the status of your files](#checking-the-status-of-your-files)\n* [Staging files](#staging-files)\n* [Stashing files](#stashing-files)\n* [Committing files](#committing-files)\n* [Branching and merging](#branching-and-merging)\n* [Resetting](#resetting)\n* [Git remote](#git-remote)\n* [Git grep](#git-grep)\n* [Git blame](#git-blame)\n* [Checking what you are commiting](#checking-what-you-are-commiting)\n* [Useful Commands](#useful-commands)\n* [Useful Alias](#useful-alias)\n* [Contributing](#contributing)\n\n#### Git \n\nGit is a distributed version control system, very easy to learn and supper fast!\n\n#### Install Git \n\nThere are a few different ways to install git (from source or for Lynux) but the pupose of this page is to focus on git commands, so I am going to assume you are installing git on a Mac.\n\nTo view other ways of installing it visit the [Git official site](http://git-scm.com/book/en/Getting-Started-Installing-Git)\n\nClick [here](http://git-scm.com/download/mac) to download and install Git\n\n##### Setting up git\n\n```sh\n$ git config --global user.name \"User Name\"\n\n$ git config --global user.email \"email\"\n```\n\n##### Applying colour to git \n\n```sh\n$ git config --global color.ui true\n```\n\n##### Initializing a repository in an existing directory\n\nIf you’re starting to track an existing project in Git, you need to go to the project’s directory and type:\n\n```sh\n$ git init\n```\nThis creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. \n\nTo start version-controlling existing files you should start by tracking those files and do an innitial commit. To accomplish that you should start with a few  `$ git add` that specifies the files you want to track followed by a commit.\n\n```sh\n$ git add \u003cfile\u003e\n$ git add README\n$ git commit -m 'Initial project version'\n```\n#### Checking the status of your files\n\nThe main tool you use to determine which files are in which state is the `$ git status` command. If you run this command directly after a clone, you should see something like this:\n\n```sh\n$ git status\n# On branch master\nnothing to commit (working directory clean)\n```\n\nIf you add a new file to your project, and the file didn't exist before, when you run a `$ git status` you should see your untracked file like this:\n\n```sh\n$ git status\n# On branch master\n# Untracked files:\n#   (use \"git add \u003cfile\u003e...\" to include in what will be committed)\n#\n#   README\nnothing added to commit but untracked files present (use \"git add\" to track)\n```\n\n#### Staging files\n\nAfter initializing a git repository in the chosen directory, all files will now be tracked. Any changes made to any file will be shown after a $ git status as changes not staged for commit.\n\nTo stage changes for commit you need to add the file(s) - or in other words, stage file(s).\n\n```sh\n# Adding a file\n$ git add filename\n\n# Adding all files\n$ git add -A\n\n# Adding a all files changes in a directory\n$ git add .\n\n# Choosing what changes to add (this will got through all your changes and you can 'Y' or 'N' the changes)\n$ git add -p\n```\n\n#### Stashing files\n\nGit stash is a very useful command, where git will 'hide' the changes on a dirty directory - but no worries you can reapply them later. The command will save your local changes away and revert the working directory to match the HEAD commit.\n\n```sh\n# Stash local changes\n$ git stash\n\n# Stash local changes with a custom message\n$ git stash save \"this is your custom message\"\n\n# Re-aplly the changes you saved in your latest stash\n$ git stash apply\n\n# Re-aplly the changes you saved in a given stash number\n$ git stash apply stash@{stash_number}\n\n# Drops any stash by its number\n$ git stash drop stash@{0}\n\n# Apply the stash and then immediately drop it from your stack\n$ git stash pop\n\n# 'Release' a particular stash from your list of stashes\n$ git stash pop stash@{stash_number}\n\n# List all stashes\n$ git stash list\n\n# Show the latest stash changes\n$ git stash show\n\n# See diff details of a given stash number\n$ git diff stash@{0}\n```\n\n#### Committing files\n\nAfter adding/staging a file, the next step is to commit staged file(s)\n\n```sh\n# Commit staged file(s)\n$ git commit -m 'commit message'\n\n# Add file and commit\n$ git commit filename -m ''\n\n# Add file and commit staged file\n$ git commit -am 'insert commit message'\n\n# Amending a commit\n$ git commit --amend 'new commit message' or no message to maintain previous message\n\n# Squashing commits together\n$ git rebase -i\nThis will give you an interface on your core editor:\n# Commands:\n#  p, pick = use commit\n#  r, reword = use commit, but edit the commit message\n#  e, edit = use commit, but stop for amending\n#  s, squash = use commit, but meld into previous commit\n#  f, fixup = like \"squash\", but discard this commit's log message\n#  x, exec = run command (the rest of the line) using shell\n```\n\n#### Branching and merging\n\n```sh\n#Creating a local branch\n$ git checkout -b branchname\n\n#Switching between 2 branches (in fact, this would work on terminal as well to switch between 2 directories - $ cd -)\n$ git checkout -\n\n#Pushing local branch to remote\n$ git push -u origin branchname\n\n#Deleting a local branch - this won't let you delete a branch that hasn't been merged yet\n$ git branch -d branchname\n\n#Deleting a local branch - this WILL delete a branch even if it hasn't been merged yet!\n$ git branch -D branchname\n\n#Viewing all branches, including local and remote branches\n$ git branch -a\n\n#Viewing local branches\n$ git branch\n\n#Viewing remote branches\n$ git branch -r\n```\n\n#### Fetching and checking out remote branches\n\n```sh\n#This will fetch all of the remote branches for you.\n$ git fetch origin\n\n#With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:\n$ git checkout -b test origin/test\n\n#Deleting a remote branch\n$ git branch -rd origin/branchname\n$ git push origin --delete branchname  or  $ git push origin:branchname\n```\n\n#### Merging branch to trunk/master\n\n```sh\n# First checkout trunk/master\n$ git checkout trunk/master\n\n# Now merge branch to trunk/master\n$ git merge branchname\n\n# To cancel a merge\n$ git merge --abort\n```\n\n#### Tracking existing branch\n\n```sh\n$ git branch --set-upstream-to=origin/foo foo\n```\n\n#### Resetting\n\n```sh\n# Mixes your head with a give sha\n# This lets you do things like split a commit\n$ git reset --mixed [sha]\n\n# Upstream master\n$ git reset HEAD origin/master -- filename\n\n# The version from the most recent commit\n$ git reset HEAD -- filename\n\n# The version before the most recent commit\n$ git reset HEAD^ -- filename\n\n# Move head to specific commit\n$ git reset --hard sha\n\n# Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too.\n$ git reset --hard\n```\n\n#### Git remote \n\n```sh\n# Show where 'origin' is pointing to and also tracked branches\n$ git remote show origin\n\n# Show where 'origin' is pointing to\n$ git remote -v\n\n# Change the 'origin' remote's URL\n$ git remote set-url origin https://github.com/user/repo.git\n```\n\n#### Git grep \n\n```sh\n# 'Searches' for parts of strings in a directory\n$ git grep 'something'\n\n# 'Searches' for parts of strings in a directory and the -n prints out the line numbers where git has found matches\n$ git grep -n 'something'\n\n# 'Searches' for parts of string in a context (some lines before and some after the grepped term)\n$ git grep -C\u003cnumber of lines\u003e 'something'\n\n# 'Searches' for parts of string and also shows lines BEFORE the grepped term\n$ git grep -B\u003cnumber of lines\u003e 'something'\n\n# 'Searches' for parts of string and also shows lines AFTER the grepped term\n$ git grep -A\u003cnumber of lines\u003e 'something'\n```\n\n#### Git blame\n\n```sh\n# Show alteration history of a file with the name of the author\n$ git blame [filename]\n\n# Show alteration history of a file with the name of the author \u0026\u0026 SHA\n$ git blame [filename] -l\n```\n\n#### Checking what you are commiting \n\n```sh\n# See all (non-staged) changes done to a local repo \n$ git diff\n\n# See all (staged) changes done to a local repo \n$ git diff --cached\n\n# Check what the changes between the files you've commited and the live repo\n$ git diff --stat origin/master\n\n```\n\n#### Useful commands \n\n```sh\n# Check if a sha is in production\n$ git tag --contains [sha]\n\n# Number of commits by author\n$ git shortlog -s --author 'Author Name'\n\n# List of authors and commits to a repository sorted alphabetically\n$ git shortlog -s -n\n\n# List of commits showing commit messages and changes\n$ git log -p\n\n# List of commits with the particular expression you are looking for\n$ git log -S 'something'\n\n# List of commits by author\n$ git log --author 'Author Name'\n\n# Shows a list of commits with a graph illustrating the history\n$ git log --graph\n\n# List of commit comments by author\n$ git shortlog -n --author 'Author Name'\n#This also shows the total number of commits by the author\n\n# Number of commits by contributors\n$ git shortlog -s -n\n\n# Undo local changes to a File\n$ git checkout -- filename\n\n# Shows more detailed info about a commit\n$ git cat-file sha -p\n\n# Shows log by author and searching for specific term inisde the commit message\n$ git log --grep \"term\" --author \"name\"\n\n```\n\n#### Useful alias\nTo add an alias simply open your .gitconfig file on your home directory and include the alias code\n\n```sh\n# shows the log in a more consised way with the graph for branching and merging\nlg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)\u003c%an\u003e%Creset' --abbrev-commit\n\n```\n\n### Contributing\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -m 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request - cheers!\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelod%2Fgit-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcelod%2Fgit-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelod%2Fgit-commands/lists"}