I needed this, I was like every hour searching in Google one of the following commands… and having this page cached looks more efficient.
Commands
git clone URL folder
This clones a repo to a given folder. If you don’t write the folder, it will try to clone it in a folder whose name be the repo name. If there is already a folder with that name, the clone process will fail.
git init
It creates a git structure in the current folder
git add file
It stages the given file. If you put a dot instead, it stages everything.
git commit -m “Your commit message”
It creates a commit with all the files that are staged.
git commit –amend –no-edit (be cautious*)
It adds the staged files to the last commit. If you’ve already pushed it, you won’t be able to do it.
git reset HEAD~1 (be cautious*)
It undoes the last commit, in the sense that puts those changes back to the working state. If you modify a file, commit it and then use this command the file will stay modified but there will be no commit. The number at the end sets how many commits you will go back.
git status
It prints a sum up of the state of your git: your local branch, the remote branch, how synchronized you are, files that are staged for the next commit…
git log
It lists the commit history of the current branch. You can scroll through it with the arrow keys and press the ‘q’ key to exit.
git shortlog
It lists the commit history grouped by user. You can scroll through it with the arrow keys and press the ‘q’ key to exit.
git fetch
It checks the commits that are in the remote repo.
git pull
It merged the commits that are in the remote repo into your local branch.
git push
It sends your local commits to the remote repo.
git branch
It lists all the local branches and highlights the one you are currently.
git branch -a
It lists all the local and remote branches, and highlights the one you are currently.
git branch branchname
It creates a branch with the given name.
git checkout branchname
It puts your working directory in the state it was in the last commit of the given branchname, which has to exist.
git checkout -b branchname
It puts your working directory in the state it was in the last commit of the given branchname, which will be created automatically.
git checkout hash
It puts your working directory in the state it was in the commit referenced by the given hash.
git checkout .
It puts your working directory in the state of the last commit of the current branch.
Be cautious
Some of the above commands change the last commit(s). If those commits are already pushed, Git will think both remote commits and local commits are different and will try to pull and merge the changes, messing up the history.
Be aware that if you already pushed the commits, someone could have already pulled those commits. If you decide to overwrite what is on the remote branch (git push -f), talk to them before; otherwise you’ll mess up with their local history.
Alias
Create an alias is as simple as git config –global alias.fetcha fetch –all. With this you only have to type ‘git fetcha’ instead of ‘git fetch –all’.
amend
commit –amend –no-edit. It’s too long to type, just to add the files to the previous commit.
fetcha
fetch –all. It’s helpful when you are working with a few branches and need to be updated.
As a last tip, I have all my projects under the same root folder (then, they’re grouped by its main technology in its own folder) and I’ve added it to a System Variable so I can open the CLI and type “cd %repo%” and then “cd tabbing” to where the project folder is located.
I often use the git commands when the IDE I’m coding is not working as it should. For instance, in Visual Studio there are sometimes ghost changes in files: you open it, see nothing changed, click in “Undo changes” but nothing happens. So I type quickly “git checkout .” and undo everything.