In software development, GIT is currently the most widely used version control tool. It allows us to work together with other developers, incorporating our changes or those of other developers to keep the work “synchronized”, in the same way that it allows us to create versions for each change made.
When we use GIT, we will use some commands on a day-to-day basis, but it offers us a large number of options and functionalities to solve any inconvenience or situation that comes our way. This is why it is very important to have a summary of these commands at hand.
Config
These commands allow us to configure general information across all repositories.
#set a name that is identifiable for credit when review version history git config --global user.name "[firstname lastname]" #set an email address that will be associated with each history marker git config --global user.email "[valid-email]" #set automatic command line coloring for Git for easy reviewing git config --global color.ui auto
Init and starting a repository
When starting we have 2 options, we can create a new repository in our working directory, or we can “clone” a remote repository to the working directory.
#initialize an existing directory as a Git repository git init #retrieve an entire repository from a hosted location via URL git clone [url]
Basic – Stage
Once we have a repository, we can start working on the changes to our files locally.
#Adds the indicated file to the next commit (stage), replace <file> for a <directory> to add all the changes in the directory git add <file> #Unstage a file while retaining the changes in working directory git reset [file] #List which files are staged, unstaged, and untracked. git status #commit your staged content as a new commit snapshot git commit -m "<message>" #show the commit history for the currently active branch git log
Branch and Merging
One of the features of Git is to allow you to create different versions or working copies. We call that branches, with them we can work on different features or changes from the same working directory.
#list your branches. a * will appear next to the currently active branch git branch #create a new branch at the current commit git branch [branch-name] #switch to another branch and check it out into your working directory git checkout -b <branch> #merge the specified branch's history into the current one git merge [branch]
Remote Repositories
Now, the other functionality is allowing us to share our changes with other co-workers, or bring changes from a remote repository to our local environment.
#Create a new connection to a remote repo. git remote add [alias] [url] #fetch down all the branches from that Git remote git fetch [alias] #merge a remote branch into your current branch to bring it up to date git merge [alias]/[branch] #Transmit local branch commits to the remote repository branch git push [alias] [branch] #fetch and merge any commits from the tracking remote branch git pull
Modifying the History
Sometimes we may need to modify some already “committed” change.
#apply any commits of current branch ahead of specified one git rebase [branch] #Reset staging area to match most recent commit, but leave the working directory unchanged. git reset #clear staging area, rewrite working tree from specified commit git reset --hard [commit]
Temporary Changes
On many occasions, we have changes to our local working directory that are not ready to be committed. But, for some reason we have to temporarily remove them, for example to switch to another branch.
#Save modified and staged changes git stash #or git stash push #list stack-order of stashed file changes git stash list #write working from top of stash stack git stash pop #discard the changes from top of stash stack git stash drop #write the working with specific stash commit. Doesn't delete the stash from the list. git stash apply <commit>
References
- https://education.github.com/git-cheat-sheet-education.pdf
- https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet