All topics (53)

  • Merge my local changes with another branch

    This can be done while a regular merge, and you should keep your merge history by making sure to use --no-ff, which means no fast forward.

    Switch to the branch you're merging against, make sure it's up to date, and merge:

    git merge <the-other-branch> --no-ff

    You should get a commit message with the merge X into Y branch, and then you can safely push your merge.>

  • Undo commit before pushing changes

    If you made a commit that you wish to modify or erase entirely, the git reset command can be of help here.

    git reset HEAD~1 # undo last commit, keep the changes

    git reset --hard HEAD~1 #undo last commit, erase the changes

    Naturally, be careful when using the last option as your local files will be discarded!

    To keep the same commit message:

    git commit -a -c ORIG_HEAD

    You will be prompted with your last commit message.

    Tips from hrbonz

  • Recover a deleted branch

    By typing git reflog, you can grab the commit hash (SHA1) at the top of your deleted branch. Copy this sha, then use:

    git checkout <sha>

    Once you've done that, recover the branch by typing:

    git checkout -b <branchname>

  • Display the commits that have deleted files

    You can quickly check which commits included deleted files by using the following command:

    git log --diff-filter=D --summary

    This will show you the commits in which files were removed.

  • Rebase my branch with master

    To catch-up with the latest changes from master (or any other branch you started from), you can and should rebase against it. Say you're working on the foobar branch:

    git checkout foobar

    Followed by a rebase:

    git rebase master

    This applies the origin commits on top of master. When you're done with solving conflicts, continue by using git rebase --continue. At this point you can continue to work on your branch or merge it against master.

    Learn more about rebases

  • Restore a deleted file

    If you deleted a file by accident, you can get it back by checking it out again:

    git checkout myFile.txt

    If you need to restore a file from a certain point in time in your commit history, grab the hash of that commit and run:

    git checkout $commit~1 myFile.txt

  • Discard local file changes

    The easiest way to get rid of unwanted changes is by resetting a file or a folder to the current commit state. To do so, you can use

    git checkout myFile.txt

    You can also reset a specific path instead:

    git checkout -- myPath

  • Clear all stashed states

    You can clear all stashed states by using:

    git stash clear

  • Remove untracked files and folders

    To remove all untracked files from your working copy:

    git clean -f

    To remove all untracked files and folders:

    git clean -fd

    Tip: If you'd like to see first which files will be untracked before actually removing them, you can do a safe clean test by running:

    git clean -n

  • Undo a commit message before pushing

    You can amend your commit message by typing git commit --amend, which will open your editor and allow you to make changes to the most recent commit message.

    You can also modify the message directly, by using:

    git commit --amend -m "My new awesome message"

  • Display commits by author

    You can filter the commit history by author by using:

    git log --author="AuthorName"

  • Search for a specific commit message in all commits

    You can search for a specific commit log by matching it on a regular expression. Use:

    git log --grep <your-query>

  • Undo a commit message after pushing

    This is a two-step process. You need to amend your commit message by using git commit --amend, and then you re-write your branch commit history by force pushing the commit: git push <remote> <branch> --force

    Warning: by force pushing, you can lose the remote branch commits if your local branch is not up to date, so be careful.

  • Combine two or more commits

    You will need to use an interactive rebase. If you're rebasing against master, start the process by typing git rebase -i master. However, if you're not rebasing against a branch, you'll need to rebase against your HEAD.

    If you want to squash your last 2 commits, you can use:

    git rebase -i HEAD~2.

    You will then be prompted to follow instructions to pick commits. If you wish to combine all your commits with the oldest first commit, leave the first line with pick and change the letter to f on all other commits. Learn more about rebases

  • Remove a file from git but keep the local file

    This will remove the file your your git tracking, but keep your local copy:

    git rm --cached myfile.txt

  • Compare an old revision of a file

    You can easily view the contents of a file at a specific point in time by using:

    git show commitHash:myFile.txt

  • Squash feature branch commits to merge into the release branch

    If you decide to merge and squash your commits, this will create a new commit but only in the release branch, therefore the history of the feature branch will remain intact.

    Here's an example on how to achieve this:

    git fetch origin

    git checkout [release-branch]

    git rebase origin/[release-branch]

    git merge —squash —no-commit [feature-branch]

    git commit -m 'Merge X into Y'

    You'll end up with one commit only in your release branch, while keeping the feature history intact.

    Learn more about feature branches

  • Reset to a certain commit in history

    If you don't care about your local changes, you can reset back to one commit back in time by doing a hard reset:

    git reset --hard HEAD~1

    This will set the HEAD back by one commit. You can also do this using the commit hash instead.

  • Untrack files that already exist

    If you want to untrack a file that already exists in the repo but would still like to keep it locally, commit your changes and run:

    git rm -r --cached

    This will remove changed files from the staging area. Afterwards, just run a normal:

    git add . and commit your changes.

  • Recover a deleted tag

    If you want to recover a tag deleted by accident, start by grabbing it:

    git fsck --unreachable | grep tag

    Once you found it, you can restore it:

    git update-ref refs/tags/NAME KEY

    Source

  • Move uncommited changes to a new branch

    Too often we start changing files on a branch to fix something, only then realising we didn't create a new branch before. Luckily, this is as simple as creating a new branch:

    git checkout -b my-new-branch-name

    This will bring any files from your current branch into the new one, which you can commit then.

  • Push locally created branch to remote

    If you've created a local branch but now want it to be tracked on the remote you can run:

    git push -u origin my-new-branch-name

    Now everyone can checkout to your branch.

  • Recover stashed changes

    If you still have your changes stashed, you can apply them onto your branch by using git stash apply. You can run git diff to compare the differences. To get rid of the stash afterwards run:

    git stash drop

    If you have more than one stash, find the one you need by running:

    git stash list and applying it by referencing its index:

    git stash@{1}

    Keep in mind that this is zero-based (stash@{0} is the first one)

    Read more about stashes

  • Rename my local and remote branch

    Let’s say you have a “fix-bug25” branch, and you want to rename it to “hotfix-users”. First, change the local branch:

    git branch -m fix-bug25 hotfix-users

    Now, the remote branch: you can’t rename the branch directly, so you’ll need to delete it and push it again with the new name. Make sure no one else within your team is working on it! Delete the branch: git push origin --delete fix-bug25

    And now let's push the new one: git push origin hotfix-users

  • Undo a commit after pushing it to master

    This will revert one or more commits that you want to erase from the remote branch. You can either specify one by hash:

    git revert b712c3c

    Revert only the second to last commit:

    git revert HEAD^

    Or undo the last commit without creating a revert commit:

    git revert -n HEAD

    Tip: HEAD~1 is a shortcut to HEAD^, they mean the same thing. Learn more about git revert

  • Unstage a file

    To unstage a file added by mistake, the easiest option would be:

    git reset HEAD unlovedFile.txt

  • Committed to the wrong branch

    Start by switching to the new branch you forgot to create, by typing

    git checkout -b <new-branch>.

    Switch back to the original branch now:

    git checkout <original-branch>

    ...and reset to the last commit you want to keep.

    To do so, you can type git log and save the hash (SHA1) of the last commit that you want to keep. Let's say this is a31a45c.

    Now you have to reset it: git reset --hard a31a45c and push this last forced reset.

    Warning: Make sure no one has committed to that original branch in the meantime, or changes might be lost!

  • Delete remote branch

    If you are positive this branch can be deleted, type:

    git push origin --delete branch-name

  • Reset local branch to match remote

    Assuming that you have no changes that you want to save, this can be done with two easy commands.

    First let's get a fresh fetch of the remote:

    git fetch <remoteBranchName>.

    Then let's tell git that we want to reset our local to the head of our remote branch like so:

    git reset --hard origin/<localBranchName>.

    If you do have a commit that you wish to keep, you should check out a new branch and make a commit before resetting: git commit -m "just in case"

    git branch my-new-branch-name

  • Show number of commits from each contributor

    Want to see how many commits everyone in your team has made?

    git shortlog -s -n

    This will order the authors in decending order by number of commits made.

  • Show all the commits to one file

    Ever wanted to see all the commits made to a single file?

    git log --follow -p -- myfile

    This will show all the commits made to a single file. The --follow argument ensures that we can see commits made to that file even if it was under a different file name at the time.

    Omit the -p to just see the commit messages and not the commit content.

  • Use command alias's in your CLI

    Getting fed up of typing git status? Well we can easily alias that command to something slightly quicker to type within git.

    git config --global alias.st status

    Now all we have to type is git st

    We can take it a step further by aliasing more complicated commands to an alias:

    git config --global alias.logme 'log -p --author=Rob'

    Now we've aliased git logme to show all of our commits.

  • Mark a conflict file as resolved

    To mark a file (or several) conflicted files as resolved so you can push the changes up, normally add these files:

    git add <file>

    You can then type git commit to solve these conflicts and push the changes up.

    Suggested by Robert Wünsch.

  • Display all unpushed commits

    To view all the commits that are yet to be pushed to all branches, use the following command:

    git log --branches --not --remotes

    Alternatively, you can also use:

    git log origin/master..HEAD

  • Rename a git tag

    To rename an existing tag:

    git tag <newTag> <oldTag>

    git tag -d <oldTag>

    git push origin :refs/tags/<oldTag>

    git push --tags

  • Remove old remote git branches

    If a branch is removed on the remote, you can delete it from the local refs by using the git-remote prune <name> feature.

    This will delete the stale remote-tracking branch under <name>, where the stale branch has already been removed from the remote repository referenced by <name>, but are still locally available in remotes/<name>.

  • Update a specific submodule

    To update a specific submodule in your repository, you should append the path to your submodule:

    git submodule update --remote --merge <path>

    Suggested by Wouter Peschier.

  • Stage deleted files

    To stage for commit files or folders that you have deleted locally, you can use:

    git add -u

    If you only want to stage the current path you're in, use:

    git add -u .

  • Move recent commits to a new branch

    Say you want to move the last 2 commits into a new, separate branch. You can do this by creating the branch, rolling back 2 commits and checking out the branch. So:

    git branch <branch-name>

    git reset --hard HEAD~2 # this rolls back 2 commits

    git checkout <branch-name>

    Note that by HARD resetting you will lose any uncommited work. So make sure that everything is committed!

  • Show all remote branches

    To view a list of all tracked and untracked remote branches, use:

    git remote show origin

  • List all files changed in a commit

    To view just the file names, use:

    git show --name-only SHA1

    Also view the status:

    git show --name-status SHA1

  • Checkout a file from a different branch

    You can checkout a single file from a different branch without switching to it. Use:

    git checkout <branch_name> --<file_path>

  • Show contents of a file at a specific commit

    You can get a file’s contents out from a specific commit. Use:

    git show <treeish>:<file>

    Source

  • Clone a repository without getting the entire history

    You're looking for a shallow clone. This will only fetch the latest history and not all the objects in the repository, which might take a long time while not necessary.

    git clone <repository URL> --depth 1

    The depth parameter allows you to specify how deep you want to go:

    git clone <repository URL> --depth 5

  • Add a remote repository

    Before you can push to a remote, you first need to add one. When you clone a repository, git will automatically add the URL you cloned from as the origin remote. To add one yourself, use:

    git remote add <remote eame> <repository URL>

    For example:

    git remote add upstream https://github.com/magalhini/firstaidgit.git

  • List all remote branches

    To list all remotes:

    git remote

    This will only show the names of the remotes. To view more information, use git remote show.

  • Show information about a remote branch

    To show information about a remote branch, such as fetch and push URLs, tracked branches and HEAD, use:

    git remote show origin

    Where origin is your remote.

  • Delete specified tag

    You can delete a specific tag locally using:

    git tag -d [tag name]

    Remove tag from remote using:

    git push [remote name] :refs/tags/[tag name]

  • Rename a remote

    To rename a remote:

    git remote rename <oldRemote> <newRemote>

    You can verify your changes by listing the remotes:

    git remote

    Source

  • Remove a submodule

    To remove a submodule use:

    git rm <submodule_path>

    In case you later want to add a submodule with the same name, you will also need to use:

    rm -rf .git/modules/<submodule_path>

  • Compare commit log difference between 2 branches

    You can compare difference in commit log between 2 branches by:

    git log [branch one]..[branch two]

    Example to compare the staging and development branch logs:

    git log staging..development

  • List untracked files within untracked directories

    To list untracked files within untracked directories:

    git status -u

    . By default, git will not show files within directories that are untracked.

  • List untracked files within untracked directories

    To list untracked files within untracked directories:

    git status -u

    . By default, git will not show files within directories that are untracked.