Some userful GIT commands — 02.03.2019

Here there are some examples of git commands that I use often.

Not all commands written here are git commands, but all of them are related to git. Please refer to the documentation for more details.

Set your details

Use --global to set the configuration for all projects. If git config is used without --global and run inside a project directory, the settings are set for the specific project.

Make git ignore file modes

This option is useful if the file permissions are not important to us, for example when we are on Windows.

See your settings

Initialize a git repository for existing code

Clone a remote repository

This creates a new directory with the name of the repository.

Clone a remote repository in the current directory

Get help for a specific git command

Update and merge your current branch with a remote

Where origin is the remote repository, and master the remote branch.
If you don’t want to merge your changes, use git fetch

View remote urls

Change origin url

Add remote

See non-staged (non-added) changes to existing files

Note that this does not track new files.

See staged, non-commited changes

See differences between local changes and master

Note that origin/master is one local branch, a shorthand for refs/remotes/origin/master, which is the full name of the remote-tracking branch.

See differences between two commits

See the files that changed between two commits

See the files changed in a specific commit

or

Source: http://stackoverflow.com/a/424142/1391963

See diff before push

See diff with only the changed lines (no context)

See details (log message, text diff) of a commit

Count the number of commits

git rev-list COMMIT_ID --count

Check the status of the working tree (current branch, changed files…)

Make some changes, commit them

Rename/move and remove files

Change message of last commit

Push local commits to remote branch

Push commits to all remotes, in a single command

Git does not do that, but see https://stackoverflow.com/a/18674313/1391963

See recent commit history

See commit history for the last two commits

See commit history for the last two commits, with diff

See commit history printed in single lines

Revert one commit, push it

Revert to the moment before one commit

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"# Update working copy to reflect the new commit
git reset --hard

Source: http://stackoverflow.com/q/1895059/1391963

Undo last commit, preserving local changes

Undo last commit, without preserving local changes

Undo last commit, preserving local changes in index

Or git reset HEAD~1
See also http://stackoverflow.com/q/927358/1391963

Undo non-pushed commits

Reset to remote state

See local branches

See all branches

Make some changes, create a patch

Add a file and create a patch

Add a file, make some changes, and create a patch

Make a patch for a commit

Make patches for the last two commits

Make patches for all non-pushed commits

Create patches that contain binary content

Apply a patch

Apply a patch created using format-patch

Break up multiple changes into separate commits (or commit only part of a changed file)

Sources: https://stackoverflow.com/q/4948494/1391963, https://stackoverflow.com/q/1085162/1391963

Create a tag

Push a tag

Create a branch

Here master is the starting point for the new branch. Note that with these 2 commands we don't move to the new branch, as we are still in master and we would need to run git checkout new-branch-name. The same can be achieved using one single command: git checkout -b new-branch-name

Create a branch from a previous commit

or using a symbolic reference (e.g. last commit):

You can also use

Source: http://stackoverflow.com/a/2816728/1391963

Checkout a branch

See commit history for just the current branch

(master is the branch you want to compare)

Merge branch commits

Here we are merging all commits of branch-name to master.

Merge a branch without committing

See differences between the current state and a branch

See differences in a file, between the current state and a branch

Delete a branch

Push the new branch

Get all branches

Get the git root directory

Source: http://stackoverflow.com/q/957928/1391963

Remove from repository all locally deleted files

Source: http://stackoverflow.com/a/5147119/1391963

Delete all untracked files

Including directories:

Preventing sudden cardiac arrest:

Source: http://stackoverflow.com/q/61212/1391963

Delete all files from a git repository that have already been deleted from disk:

Source (and alternatives): https://stackoverflow.com/a/5147119/1391963

Show total file size difference between two commits

Short answer: Git does not do that.
Long answer: See http://stackoverflow.com/a/10847242/1391963

Unstage (undo add) files:

See closest tag

Source: http://stackoverflow.com/q/1404796/1391963. See also git-describe.

Debug SSH connection issues

Have git pull running every X seconds, with GNU Screen

Use Ctrl+a Ctrl+d to detach the screen.

See previous git commands executed

or

See recently used branches (i.e. branches ordered by most recent commit)

Source: http://stackoverflow.com/q/5188320/1391963

Tar project files, excluding .git directory

Tar all locally modified files

Look for conflicts in your current files

There’s also git-grep.

Apply a patch not using git:

Web Artisan. Human — Engineering.