Git Product home page Git Product logo

learn-git's Introduction

Basic git

git status is your friend!

It is not important to memorize every single git command. Who could? Fortunately the amount of commands to remember for a basic git workflow is quite small. Just in case, git is to help you always:

$ git status

tells you the status of the files in your working tree, but also provides you hints for the next step.

For example, if you have modified a file in you working tree, then

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

is telling you that now you may consider to run

$ git add <file>

or

$ git restore <file>

Both possibilities here are accompanied by a short descriptions. If you get stuck for some reason or do not remember a command, try git status first.

Modified, staged, committed

Yet to come...

Staging files

Elevate modified files to the status of staged.

$ git add FILE1 FILE2 ...

Have you staged something you shouldn't have?

For example, say you have run:

$ git add this-file.txt

Do not worry, ask git for help:

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   this-file.txt

To brink a staged file back to the status of unstaged it is suggested the following command:

$ git restore --staged <file>

Thus, in our case, it is sufficient to issue:

$ git restore --staged this-file.txt

Committing your changes

Take staged files to be tracked in .git.

$ git commit -m 'commit message'

If you have forgot to insert another staged file, say foo.txt, in your commit, then you can do this:

$ git add foo.txt
$ git commit --amend

You will be prompted a vim display: if you want you can edit the commit text too. When finished, just exit vim.

If you do not want to mess with vim up, then you may prefer

$ git commit --amend -C main

With this command we are saying to git to reuse the previous commit text.

Removing files

If you delete some tracked file, say baz.txt, with

$ rm baz.txt

in .git it still exists and could be recovered (read below). If you want to get rid of it definitely from your disk and your .git, then

$ git rm baz.txt

If you want a staged file to be removed from the status of staged and keep it in your working tree, then

$ git rm --cached baz.txt

Undo a git push

Assume you have just run

$ git push

but afterwards you notice there is a small problem with the commit you have pushed. You can correct this via

$ git revert <SHA>

The string to be put instead of <SHA> can be determined via

$ git log --oneline

You have to look at the recent commit and take just the initial string of seven alphanumeric characters. Example: if we have

$ git log --oneline
8cfd3ec (HEAD -> main, origin/main) added section "Undo a gith push"
e56a8c7 deleted LICENSE.txt
4fb4172 Corrected title of README.md
[...]

then copy 8cfd3ec.

learn-git's People

Contributors

indrjo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.