Skip to content

Instantly share code, notes, and snippets.

@albertpark
Last active May 14, 2020 01:34
Show Gist options
  • Save albertpark/320bac81f312d589597d229f612c2e0e to your computer and use it in GitHub Desktop.
Save albertpark/320bac81f312d589597d229f612c2e0e to your computer and use it in GitHub Desktop.

Must know Git commands

Updated Apr 29, 2020

Show differences

If you want see the differences between local and remote files:

$ git diff master origin/master

Delete a file or folder

If you want to remove the file from the Git repository and the filesystem, use:

$ git rm file1.txt
$ git commit -m "remove file1.txt"

But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

$ git rm --cached file1.txt
$ git commit -m "remove file1.txt"

And to push changes to remote repo

git push origin branch_name  

Recover files after git rm

You need to do two commands, the first will "unstage" the file (removes it from the list of files that are ready to be committed). Then, you undo the delete.

If you read the output of the git status command (after the using git rm), it actually tells you how to undo the changes (do a git status after each step to see this).

Unstage the file:

$ git reset HEAD <filename>

Restore it (undo the delete):

$ git checkout -- <filename>

Undo a git push

To undo a git push we need to force an update:

git push -f origin HEAD^:master

Get to previous commit (preserves working tree):

git reset --soft HEAD

Get back to previous commit (you'll lose working tree):

git reset --hard HEAD^

Adding files

Long-form flags:

  • git add -A is equivalent to git add --all
  • git add -u is equivalent to git add --update
New Files Modified Files Deleted Files
git add -A ✔️ ✔️ ✔️ Stage All (new, modified, deleted) files
git add . ✔️ ✔️ ✔️ Stage All (new, modified, deleted) files
git add --ignore-removal . ✔️ ✔️ Stage New and Modified files
git add -u ✔️ ✔️ Stage Modified and Deleted files only

Rename a tag

Here is how I rename a tag old to new:

$ git tag <new> <old>
$ git tag -d <old>
$ git push --delete origin <old>
$ git push origin :refs/tags/<old>
$ git push --tags

The colon in the push command removes the tag from the remote repository. If you don't do this, Git will create the old tag on your machine when you pull.

Finally, make sure that the other users remove the deleted tag. Please tell them (co-workers) to run the following command:

$ git pull --prune --tags

References

@helciodasilva
Copy link

Tanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment