Skip to content

git basic

Michael Kramer edited this page Feb 8, 2019 · 7 revisions

Git commands that I keep forgetting

Creating a branch

  1. git checkout -b branch-name This will checkout a new branch with that name.
  2. git add ./ This stages all files that you touched for the next commit. The ./ means all
  3. git commit -m 'Your amazing message' This is the commit command.
  4. git push --set-upstream origin branch-name This pushes your branch to the remote.

Remove local branches that don't exist in the remote.

  1. git remote prune origin
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'/`; do git branch -D $branch; done

This website gives a good explanation of what this does. http://erikaybar.name/git-deleting-old-local-branches/

Moving files around

Lets say you created a group of files it started off small but has grown, so you want to move all of them to a new folder to keep things organized. Yet you can't just move it because other people on your team are editing those same files in their branches.

git mv file new_location/file

Why would you not just move it in the folder structure?

So, without git rm, one might do (this is Unix/OSX but the steps are similar in windows unless using cygwin terminal emulator and then u probably can):

$ mv newfile movedfile
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    newfile

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        movedfile

no changes added to commit (use "git add" and/or "git commit -a")
$ git rm newfile
rm 'newfile'
$ git add movedfile
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    newfile -> movedfile

$ git commit -m"renamed"
[master 46332e4] renamed
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename newfile => movedfile (100%)
$ git status
On branch master
nothing to commit, working directory clean

whereas with git rm one can just do:

$ git mv newfile2 renamed_newfile2
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    newfile2 -> renamed_newfile2

$ git commit -m"renamed"
[master 8d1e296] renamed
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename newfile2 => renamed_newfile2 (100%)
$ git status
On branch master
nothing to commit, working directory clean

Revert a single file

git checkout 97815003a489d9c66a5d5866c1eba25de0575e9e -- crons.js